Data Type represents a type of data present in a variable. In this tutorial, we will see what are the different types of Python data types.
Python Data Types :
As we seen in Advantages of Python, in Python, we are not required to specify a type explicitly to a variable. Based on the value provided to the variable, the type will be assigned automatically. Hence the Python is consider to be a dynamically typed language.
Python supports following inbuilt data types :
- int
- float
- complex
- bool
- str
- bytes
- bytearray
- range
- list
- tuple
- set
- frozenset
- dict
- None
The above mentioned data types are inbuilt data types. Based on the value present in side the variable Python internally assigns these data types to a variable.
Though these are assigned by the python internally, if we really want to check the type of a variable, python given us an inbuilt function called type() it gives us the type of a variable.
Python int datatype :
We can use the int data type to represent numbers.
x = 10
type(a)
<class 'int'>
float datatype :
float data type represents the decimal/floating point numbers.
>>>x=10.5
>>>type(x)
<class 'float'>
Complex data type :
Python complex datatype holds complex numbers, its basically a combination of real and imaginary numbers.
>>>x = 10+5j
>>>type(c)
<class 'complex'>
>>>c.real
10.0
>>>c.imag
5.0
Bool datatype :
Bool datatype represents logical values, it only holds either True or False.
>>>flag=True
>>>flag=False
>>>type(flag)
<class 'bool'>
Str datatype :
Python Str datatype represents a sequence of characters.
>>>s='chandra'
>>>type(s)
<class 'str'>
>>>s='chandra shekhar goka'
>>>type(s)
<class 'str'>
bytes datatype :
bytes datatype represents a sequence of byte values with in the range of 0-255.
>>> list=[20,30,40,50]
>>> b=bytes(list)
>>> type(b)
<class 'bytes'>
bytearray datatype :
bytes datatype represents a sequence of byte values with in the range of 0-255. Difference between byte and bytearray is : byte data type is immutable (once it is defined we can’t change it) where as bytearray datatype mutable (we can change the values after defining).
>>>list=[50,60,70]
>>>barray= bytearray(list)
>>>type(barray)
<class 'bytearray'>
range datatype :
range datatype is used to define a range of values.
>>>r=range(10)
>>>r1=range(0,50)
>>>r2=range(0,50,10)
list datatype :
list datatype represents an ordered collection of objects.
>>>l=[1,5,6,7,8,9]
>>>type(l)
<class 'list'>
tuple datatype :
tuple datatype represents an ordered collection of objects. The only difference between list vs tuple is : list is an mutable ordered collection where as tuple is immutable ordered collection.
>>>t=(1,5,6,7,8,9)
>>>type(t)
<class 'tuple'>
set datatype :
Set represents an un-ordered collection of unique objects. it doesn’t allowed duplicate values inside it. Set is an mutable collection.
>>>s={1,2,4,3,5,7,6,9}
>>>type(s)
<class 'set'>
frozenset datatype :
frozenset represents an un-ordered collection of unique objects. it doesn’t allowed duplicate values inside it. frozenset is an immutable collection.
>>>s={10,20,'chandra',50,'shekhar'}
>>>fs = frozenset(s)
>>>type(fs)
<class 'frozenset'>
dict datatype :
dict represents a group of key->value pairs.
>>>d={100:'JAVA',101:'Python',102:'PHP'}
>>>type(d)
<class 'dict'>
None datatype :
If a variable or def, doesn’t having any value init, the default datatype is None.
>>>def sample():
>>>a=20
>>>print(sample())
None
Happy Learning 🙂