Python Data Types:
In programming world data types are important concept.
The variables are stored different types of data to make the type is defined.
So, python has following in-built data-types are as,
Text Type: | str |
Numeric Types: | int , float , complex |
Sequence Types: | list , tuple , range |
Mapping Type: | dict |
Set Types: | set , frozenset |
Boolean Type: | bool |
Binary Types: | bytes , bytearray , memoryview |
How To Know The Data Type Of Variable?
Using type() function, we can get the data type of any object.
Example:
#print the type of variable x x = 5 print(type(x))
The output of above code is,
<class 'int'>
In python, when we assigning a value the data type is set.
Below Examples is Return Type When Value is Assign:
#Example-1 x = "Hello World" #display x: print(x) #display the data type of x: print(type(x))
#Output Hello World <class 'str'>
#Example-2 x = 20 #display x: print(x) #display the data type of x: print(type(x)) #output 20 <class 'int'>
#Example-3 x = 20.5 #display x: print(x) #display the data type of x: print(type(x)) #Output 20.5 <class 'float'>
#Example-4 x = 1j #display x: print(x) #display the data type of x: print(type(x)) #Output 1j <class 'complex'>
#Example-5 x = ["apple", "banana", "cherry"] #display x: print(x) #display the data type of x: print(type(x)) #Output ["apple", "banana", "cherry"] <class 'list'>
#Example-6 x = ("apple", "banana", "cherry") #display x: print(x) #display the data type of x: print(type(x)) #Output ("apple", "banana", "cherry") <class 'tuple'>
#Example-7 x = range(6) #display x: print(x) #display the data type of x: print(type(x)) #Output range(0, 6) <class 'range'>
#Example-8 x = {"name" : "Rahul", "age" : 36} #display x: print(x) #display the data type of x: print(type(x)) #Output {"name" : "Rahul", "age" : 36} <class 'dict'>
#Example-9 x = {"ram", "gopal", "shyam"} #display x: print(x) #display the data type of x: print(type(x)) #Output {"ram", "gopal", "shyam"} <class 'set'>
#Example-10 x = frozenset({"ram", "gopal", "shyam"} ) #display x: print(x) #display the data type of x: print(type(x)) #Output frozenset({'apple', 'cherry', 'banana'}) <class 'frozenset'>
#Example-11 x = True #display x: print(x) #display the data type of x: print(type(x)) #Output True <class 'bool'>
#Example-12 x = b"Hello" #display x: print(x) #display the data type of x: print(type(x)) #Output b'Hello' <class 'bytes'>
#Example-13 x = bytearray(5) #display x: print(x) #display the data type of x: print(type(x)) #Output bytearray(b'\x00\x00\x00\x00\x00') <class 'bytearray'>
#Example-14 x = memoryview(bytes(5)) #display x: print(x) #display the data type of x: print(type(x)) #Output <memory at 0x00B08FA0> <class 'memoryview'>
Below Examples is Set the Type And Print The Data Type:
If you want to specify the data type then you have to use following constructor.
#Example-1 x = str("Hello World") #display x: print(x) #display the data type of x: print(type(x)) #Output Hello World <class 'str'>
#Example-2 x = int(45) #display x: print(x) #display the data type of x: print(type(x)) #Output 45 <class 'int'>
#Example-3 x = float(20.5) #display x: print(x) #display the data type of x: print(type(x)) #Output: 20.5 <class 'float'>
#Example-4 x = complex(1j) #display x: print(x) #display the data type of x: print(type(x)) #Output: 1j <class 'complex'>
#Example-5 x = list(("ram", "gopal", "verma")) #display x: print(x) #display the data type of x: print(type(x)) #Output: ["ram", "gopal", "verma"] <class 'list'>
#Example-6 x = tuple(("ram", "gopal", "verma")) #display x: print(x) #display the data type of x: print(type(x)) #Output: ("ram", "gopal", "verma") <class 'tuple'>
#Example-7 x = range(6) #display x: print(x) #display the data type of x: print(type(x)) #Output range(0, 6) <class 'range'>
#Example-8 x = dict(name="John", age=36) #display x: print(x) #display the data type of x: print(type(x)) #Output: {'name': 'John', 'age': 36} <class 'dict'>
#Example-9 x = set(("ram", "gopal", "verma")) #display x: print(x) #display the data type of x: print(type(x)) #Output {"ram", "gopal", "verma"} <class 'set'>
#Example-10 x = frozenset(("apple", "banana", "cherry")) #display x: print(x) #display the data type of x: print(type(x)) #Output: frozenset({'apple', 'cherry', 'banana'}) <class 'frozenset'>
#Example-11 x = bool(5) #display x: print(x) #display the data type of x: print(type(x)) #Output True <class 'bool'>
#Example-12 x = bytes(5) #display x: print(x) #display the data type of x: print(type(x)) #Output: b'\x00\x00\x00\x00\x00' <class 'bytes'>
#Example-13 x = bytearray(5) #display x: print(x) #display the data type of x: print(type(x)) #Output bytearray(b'\x00\x00\x00\x00\x00') <class 'bytearray'>
#Example-14 x = memoryview(bytes(5)) #display x: print(x) #display the data type of x: print(type(x)) #Output: <memory at 0x00D58FA0> <class 'memoryview'>
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.