Last updated

Python Numbers

Definition: Python has two main number types — int for whole numbers and float for numbers with a decimal point.

x = 10      # int
y = 3.14    # float
print(type(x), type(y))

Maths operators

+add-subtract
*multiply/divide (gives a float)
//floor divide (whole)%modulus (remainder)
**power

Example 1 — basic maths

print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3)

Example 2 — division types

/ always gives a decimal; // throws away the remainder; % gives only the remainder:

print(10 / 3)    # 3.333...
print(10 // 3)   # 3
print(10 % 3)    # 1
print(2 ** 5)    # 32 (2 to the power 5)

Example 3 — order of operations

Python follows normal maths order. Use brackets to control it:

print(2 + 3 * 4)     # 14
print((2 + 3) * 4)   # 20

💡 Real use: % is handy for "is this even?" — n % 2 == 0 is True for even numbers.

Try it Yourself
Output

          
Ad · responsive