Python program to demonstrate basic data-type in python
Data type defines the kind of a value i.e the type of value whether it is int, string, float etc.
Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.
We don't need to define data types in python because python is dynamically typed language, it automatically detects data types.
Python enables us to check the type of the variable used in the program. Python provides us the type() function, which returns the type of the variable passed.
What are the basic data types in Python? What are the supported data type in Python? We are having the following inbuilt data types in Python:
Numbers: Represent numeral values like int, float, complex
- Int : It contains positive or negative whole numbers. eg: 1,2,3,-1.....
- Float : Contains real floating point representation . eg: 2.0,1.0,8.02....
- Complex : Numbers of form Ai+Bj . eg: 2i+3j
x = 20 #int
print(x)
print(type(x))
x = 20.5 #float
print(x)
print(type(x))
x = 1j #complex
print(x)
print(type(x))
20
<class 'int'>
20.5
<class 'float'>
1j
<class 'complex'>
Sequence Type:
- String : A string is defined as a collection of one or more characters. It is putted in single quotes or double quotes or triple quotes i.e (' ') , (" ") or (' ' ' ' ' ') . for eg: s="GoEduHub.com"
#String
x = "GoEduHub.com"
print(x)
print(type(x))
GoEduHub.com
<class 'str'>
Note – To know more about Strings, refer Python Strings
- List : It is as same as arrays, it contains heterogeneous datatypes. eg: a=[1,2,"a"]
x = ["Go", "Edu", "Hub"] #list
print(x)
print(type(x))
['Go', 'Edu', 'Hub']
<class 'list'>
Note – To know more about Lists, refer Python List.
- Tuple : Tuples are created by ( ) . it contains heterogeneous datatypes . eg: t=(1,2,3)
x = ("Python", "from", "GoEduHub.com") #tuple
print(x)
print(type(x))
('Python', 'from', 'GoEduHub.com')
<class 'tuple'>
Note – To know more about Tuple, refer Python Tuple.
Dictionary: Dictionary is an unordered collection of data. It has key:value pair which is separated by a colon :, and all keys are separated by a ‘,’. eg : d={'a':1, 'b':2, 'c':3}
x = {"name" : "anu", "age" : 36} #dict
print(x)
print(type(x))
{'name': 'anu', 'age': 36}
<class 'dict'>
Note – To know more about Dictionary, refer Python Dictionary.
Boolean: Booleans represent one of two values: True or False.
x = True #bool
print(x)
print(type(x))
x = b"Hello" #bytes
print(x)
print(type(x))
True
<class 'bool'>
Set : Set is an unordered and unique collection of data types that mutable and has distinct elements. eg: s={1,2,3,4,5}
- Frozen set : Frozen sets are the sets that are immutable .
x = {"dl", "python", "ml"} #set
print(x)
print(type(x))
x = frozenset({"opencv", "pandas", "numpy"}) #frozenset
print(x)
print(type(x))
{'dl', 'ml', 'python'}
<class 'set'>
frozenset({'opencv', 'numpy', 'pandas'})
<class 'frozenset'>
Note – To know more about Sets, refer Python Set.
For more Rajasthan Technical University CSE VI Sem Python Lab Experiments Click here