Numbers in Python [Explained]
Number types in Python
In Python, there are three number types:
int
: integer, is a whole number, without decimals (positive or negative).float
: is a number, containing one or more decimals (positive or negative).complex
: Complex numbers are written with a “j” as the imaginary part.
x = 5 # int
y = 10.1 # float
z = 3 + 1j # complex
print(type(x))
print(type(y))
print(type(z))
Output:
<class 'int'>
<class 'float'>
<class 'complex'>
Number Data Type Conversion
x = 8 # int
y = 10.5 # float
z = 6+2j # complex
#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
Output:
8.0
10
(8+0j)
print(type(a))
print(type(b))
print(type(c))
Output:
<class 'float'>
<class 'int'>
<class 'complex'>