Data Types
Data Types
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:
Example
x=5
print(type(x))
Setting the Data Type
In Python, the data type is set when you assign a value to a variable:
x = 20 int »
x = 20.5 float »
x = 1j complex »
x = range(6) range »
x = True bool »
x = b"Hello" bytes »
x = bytearray(5) bytearray »
x = memoryview(bytes(5)) memoryview »
If you want to specify the data type, you can use the following constructor functions:
x = int(20) int »
x = float(20.5) float »
x = complex(1j) complex »
x = range(6) range »
x = bool(5) bool »
x = bytes(5) bytes »
x = bytearray(5) bytearray »
x = memoryview(bytes(5)) memoryview
Test Yourself With Exercises
Type Conversion
You can convert from one type to another with the int(), float(), and complex() methods:
Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
1. Number In Python
It is used to store numeric values
a= 100
b= -100
c= 1*20
print(a)
print(b)
print(c)
Output :-
100
-100
200