Python - Datatypes
Data types are the classification or categorization of data items. Data types represent a kind of value which determines what operations can be performed on that data. Numeric, non-numeric and Boolean (true/false) data are the most used data types.
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
You can get the data type of any object by using the type() function.
x = 5.5
print(type(x))
=====O/P=====
<'class float'>
In Python, the data type is set when you assign a value to a variable:
x=5 # int
print(x)
print(type(x))
x="apple" # string
print(x)
print(type(x))
x="a" # string
print(x)
print(type(x))
x=1.5 # float
print(x)
print(type(x))
x=1+5j # complex
print(x)
print(type(x))
x=["apple","banana","mango"] # list
print(x)
print(type(x))
x=("apple","banana","mango") # tuple
print(x)
print(type(x))
x={"apple","banana","mango"} # set
print(x)
print(type(x))
x={"age":20,"name":"sanvi"} # dictionary
print(x)
print(type(x))
x=range(6) # range
print(x)
print(type(x))
x=frozenset({"apple","banana","mango"}) # frozenset
print(x)
print(type(x))
x=bytearray(6) #bytearray
print(x)
print(type(x))
x= True # bool
print(x)
print(type(x))
x=b"hello" # bytes
print(x)
print(type(x))
x=memoryview(bytes(5)) # memoryview
print(x)
print(type(x))
========O/P==========
5
<class 'int'>
apple
<class 'str'>
a
<class 'str'>
1.5
<class 'float'>
(1+5j)
<class 'complex'>
['apple', 'banana', 'mango']
<class 'list'>
('apple', 'banana', 'mango')
<class 'tuple'>
{'mango', 'banana', 'apple'}
<class 'set'>
{'age': 20, 'name': 'sanvi'}
<class 'dict'>
range(0, 6)
<class 'range'>
frozenset({'mango', 'banana', 'apple'})
<class 'frozenset'>
bytearray(b'\x00\x00\x00\x00\x00\x00')
<class 'bytearray'>
True
<class 'bool'>
b'hello'
<class 'bytes'>
<memory at 0x000001D3E6E7F7C8>
<class 'memoryview'>
If you want to specify the data type, you can use the following constructor functions.
x=str("hello world")
print(type(x))
x=int(225)
print(type(x))
x=float(2.5)
print(type(x))
x=complex(4j)
print(type(x))
x = list(("appy", "bannu", "cherry"))
print(type(x))
x = tuple(("kushi", "happy", "cherry"))
print(type(x))
x = range(6)
print(type(x))
x = dict(name="John", age=36)
print(type(x))
x = set(("appie", "bunna", "berry"))
print(type(x))
x = frozenset(("seeta", "bunny", "cherry"))
print(type(x))
x = bool(5)
print(type(x))
x = bytes(5)
print(type(x))
x = bytearray(5)
print(type(x))
x = memoryview(bytes(5))
print(type(x))
========O/P=======
<class 'str'>
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'list'>
<class 'tuple'>
<class 'range'>
<class 'dict'>
<class 'set'>
<class 'frozenset'>
<class 'bool'>
<class 'bytes'>
<class 'bytearray'>
<class 'memoryview'>