Python Basics
Python Numbers
Working with numbers
Python Numbers
There are three numeric types in Python:
- int - Integer (whole numbers, positive or negative, without decimals)
- float - Floating point number (contains decimals)
- complex - Complex numbers
Integer (int)
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length:
x = 1
y = 35656222554887711
z = -3255522
print(type(x)) # <class 'int'>
print(type(y)) # <class 'int'>
print(type(z)) # <class 'int'>
Float
Float, or "floating point number" is a number, positive or negative, containing one or more decimals:
x = 1.10
y = 1.0
z = -35.59
print(type(x)) # <class 'float'>
Complex
Complex numbers are written with a "j" as the imaginary part:
x = 3+5j
y = 5j
z = -5j
print(type(x)) # <class 'complex'>
Type Conversion
You can convert from one type to another with the int(), float(), and complex() methods:
x = 1 # int
y = 2.8 # float
z = 1j # 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) # 1.0
print(b) # 2
print(c) # (1+0j)