Python Data Types
The data stored in the memory can be of many types. For example, a person’s name is stored as an alphabetic value, his age is stored in the numeric and his address is stored as an alphanumeric value. Sometimes, we also need to store answers only ‘yes’ or ‘no’, i.e., true or false. This type of data is known as Boolean data.
Python data types which are as follows:
- Numeric
- Text
- Sequence
- Mapped
- Binary
- Boolean
- Set
- NoneType
Numeric Data Type | int, float, complex |
Text Data Type | string |
Sequence DataType | list, tuple, range |
Mapped Data Type | dict |
Binary Data Type | bytes, bytearray, memoryview |
Boolean Data Type | bool |
Set DataType | set, frozenset |
None Data Type | NoneType |
How do we get the Data Type?
We can get the data type of any object in Python by using the type()
function.
a = 5
print(type(x))
OUTPUT
<class 'int'>
Numeric Data Type
a = 5
b = 1.23
c = 5j
#display the Value
print(a)
print(b)
print(c)
#print the data type
print(type(a))
print(type(b))
print(type(c))
OUTPUT
5
1.23
5j
<class 'int'>
<class 'float'>
<class 'complex'>
Text Data Type
a = "Hello Wolrd"
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
Hello World
<class 'str'>
Sequence Data Type
list
a = ["red", "blue", "black"]
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
['red', 'blue', 'black']
<class 'list'>
tuple
a = ("red", "blue", "black")
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
('red', 'blue', 'black')
<class 'list'>
range
a = range(8)
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
range(0, 8)
<class 'range'>
Mapped Data Type
dict
a = {"name" : "Alex", "age" : 35}
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
{'name': 'Alex', 'age': 35}
<class 'dict'>
Binary Data Type
bytes
a = x"PythonCodeverse"
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
x'PythonCodeverse'
<class 'bytes'>
bytearray
a = bytearray(4)
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
bytearray(b'\x00\x00\x00\x00')
<class 'bytearray'>
memoryview
a = memoryview(bytes(5))
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
bytearray(b'\x00\x00\x00\x00\x00')
<class 'bytearray'>
Boolean Data Type
a = True
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
True
<class 'bool'>
Set DataType
set
a = {'red', 'blue', 'green', 'black'}
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
{'red', 'blue', 'green', 'black'}
<class 'set'>
frozenset
a = frozenset({'red', 'blue', 'green', 'black'})
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
frozenset({'red', 'blue', 'green', 'black'})
<class 'frozenset'>
None Data Type
a = None
#display the Value
print(a)
#print the data type
print(type(a))
OUTPUT
None
<class 'NoneType'>