Numbers
Python has integers, floats and complex numbers.
Syntax
n = 42
pi = 3.14159Python has three numeric types:
- int — whole numbers of any size:
42,-7. - float — numbers with a decimal point:
3.14,9.99. - complex — numbers with an imaginary part, written with
j:2 + 3j.
Unlike many languages, Python integers never overflow — they can be as large as your memory allows.
Example
Loading editor…
Press Run to execute the code.
When to use it
- An e-commerce site calculates the total price as a float after applying a percentage discount.
- A scientific simulation uses large integers to count molecules without overflow errors.
- A graphics program converts a float pixel coordinate to an integer index for array access.
More examples
Integer and float arithmetic
Multiplying an int by a float produces a float result, which type() confirms.
items = 4
price = 2.50
total = items * price
print(type(total), total)
# Output:
# <class 'float'> 10.0Integer division and remainder
Uses floor division // and modulo % to split minutes into hours and leftover minutes.
minutes = 137
hours = minutes // 60
remaining = minutes % 60
print(f'{hours}h {remaining}m')
# Output:
# 2h 17mLarge int and scientific float
Demonstrates underscore separators in large ints and scientific notation floats.
population = 8_000_000_000 # underscore for readability
light_speed = 3e8 # scientific notation
distance = 1.5e11
travel_time = distance / light_speed
print(f'~{travel_time:.0f} seconds')
# Output:
# ~500 seconds
Discussion