Page Contents
Datatype is the classification of data items. Depending on the type of data item, different operations can be performed on that data. So we need to have a thorough knowledge about datatype.
In Python we can find out the datatype of any data through type()
function.
Below are all the built-in datatypes of Python –
- Numeric type :
int
,float
,complex
- Text type :
str
- Sequence type :
list
,tuple
,range
- Mapping type :
dict
- Set type :
set
,frozenset
- Boolean type :
bool
- Binary type :
bytes
,bytearray
,memoryview
Numeric type datatype
int datatype
In Python, int datatype refers to any whole number, whether it is a positive whole number or a negative whole number.
Example 1:
x=1
y=-1
print(type(x))
print(type(y))
<class 'int'> <class 'int'>
float datatype
In Python, float data type refers to a decimal number.
Example 1:
x=45678.764
y=-45.78
print(type(x))
print(type(y))
<class 'float'> <class 'float'>
complex datatype
In Python, complex data type refers to any complex number.
Example 1:
x=-345+89j
print(type(x))
<class 'complex'>
Text type datatype
str datatype
A string is a collection of one or more characters. In Python, a string is created using single quote, double quote or triple quote, or using the str()
function.
Example 1:
x="Rajkumar"
print(type(x))
<class 'str'>
Sequence type datatype
list datatype
In Python ,list is ordered collection of various datatypes. In Python we can create a list using a square bracket ([]
) or the list()
function.
Example 1:
x=[1,6.89,"rajkumar"]
print(type(x))
<class 'list'>
tuple datatype
In Python, tuple is ordered collection of various datatypes. In Python we can create a tuple using parenthesis (()
) or the tuple()
function.
Example 1:
x=(1,6.89,"rajkumar")
print(type(x))
<class 'tuple'>
Note :
The main difference between a list and a tuple in Python is that we can change the element inside the list(mutable) but we cannot change the element inside the tuple(immutable).
range datatype
In Python, the range datatype is the sequence of numbers. The range()
function is used to create this datatype.
Example 1:
x=range(10)
print(type(x))
<class 'range'>
Mapping type datatype
dict datatype
In Python, the dictionary is an unordered collection of key: value pair. We can create a dictionary using dict()
function or curly braces ({}
).
Example 1:
x={"python":3,"java":8}
print(type(x))
<class 'dict'>
Set type datatype
set datatype
In Python, set is an unordered collection of various datatypes. We can create a set using the set()
function or curly braces ({}
).
Example 1:
x={"python","java"}
print(type(x))
<class 'set'>
frozenset datatype
In Python, frozenset is an unordered collection of various datatypes. We can create a frozenset using the frozenset ()
function.
Example 1:
x=frozenset({"python","java"})
print(type(x))
<class 'frozenset'>
Note :
The main difference between set and frozenset in Python is that we can change the element inside the set but we cannot change the element inside frozenset.
Boolean type datatype
bool datatype
In Python, bool datatype refers only to True and False. Also with the bool()
function we can determine the boolean equivalent of other data values.
Example 1:
x=True
y=False
print(type(x))
print(type(y))
<class 'bool'> <class 'bool'>
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .