Module 2 · Beginner · Python basics

Python numbers and operators, with examples

Python numbers and arithmetic operators for beginners: int and float, + - * / // % **, operator precedence, round(), the math module and float surprises.

By theprogrammers team · · 5 min read

Computers are very good at maths, and Python makes it easy to use that power. Splitting a bill, working out GST, converting units, calculating marks: it all starts with numbers and operators, the symbols that do the calculating.

This guide covers Python's number types, the seven arithmetic operators, the order Python calculates in, rounding, the math module and a few surprises that catch out beginners.

Two kinds of numbers: int and float

students = 60        # int: a whole number
price = 49.99        # float: a number with a decimal point
population = 1_400_000_000    # underscores make big numbers readable
  • An int (integer) is a whole number: 7, 0, -15. Python ints can be as big as you like: 2 ** 100 works fine.
  • A float is a number with a decimal point: 3.14, 0.5, -2.0. 2e3 is scientific notation for 2000.0.

If you mix them, the result is a float: 5 + 2.0 gives 7.0.

The arithmetic operators

OperatorNameExampleResult
+Addition7 + 29
-Subtraction7 - 25
*Multiplication7 * 214
/Division7 / 23.5
//Floor division7 // 23
%Remainder (modulo)7 % 21
**Power7 ** 249

Three of these deserve a closer look.

/ always gives a float

10 / 2     # 5.0, not 5
10 / 3     # 3.3333333333333335

// divides and rounds down

Floor division throws away the decimal part by rounding down to the nearest whole number:

17 // 5    # 3
-7 // 2    # -4: rounding down from -3.5 gives -4, not -3

% gives the remainder

17 % 5 is 2, because 17 = 5 × 3 + 2. The remainder is surprisingly useful:

n % 2 == 0       # True if n is even
1234 % 10        # 4: the last digit
minutes = 135
print(minutes // 60, "h", minutes % 60, "min")    # 2 h 15 min

divmod gives both at once: divmod(135, 60) returns (2, 15).

** raises to a power

2 ** 10      # 1024
9 ** 0.5     # 3.0: a power of 0.5 is a square root

Order of operations

Python follows the same order as school maths (BODMAS or PEMDAS):

  1. Brackets ( )
  2. Powers **
  3. *, /, // and %, from left to right
  4. + and -, from left to right
2 + 3 * 4       # 14, not 20
(2 + 3) * 4     # 20
-3 ** 2         # -9: the power happens before the minus sign

When in doubt, add brackets. They cost nothing and make your intention clear.

Shortcut assignment operators

Updating a variable with its own value is so common that Python has shortcuts:

score = 10
score += 5     # same as score = score + 5  → 15
score -= 3     # 12
score *= 2     # 24
score /= 4     # 6.0

Python has no ++ or -- operators. Use count += 1.

Useful built-in functions

FunctionWhat it doesExampleResult
abs(x)Distance from zeroabs(-4)4
round(x, n)Round to n decimal placesround(3.14159, 2)3.14
min(...), max(...)Smallest or largestmax(4, 9, 2)9
sum(list)Add up a listsum([72, 88, 95])255
pow(x, y)Same as x ** ypow(2, 3)8
int(x)Cut off the decimalsint(7.9)7

The math module

For square roots, rounding up and down, and constants like π, import the math module:

import math

math.sqrt(16)       # 4.0
math.pi             # 3.141592653589793
math.ceil(4.1)      # 5: round up
math.floor(4.9)     # 4: round down
math.factorial(5)   # 120

radius = 7
area = math.pi * radius ** 2
print(round(area, 2))    # 153.94

Surprises to know about

Floats aren't exact

0.1 + 0.2    # 0.30000000000000004

Computers store floats in binary, and most decimals can't be stored exactly, just as 1/3 can't be written exactly as 0.333... This happens in every programming language. For display, round the result. For money, where every paisa matters, use the decimal module or store amounts as whole paise in an int.

round() rounds halves to even

round(2.5)    # 2
round(3.5)    # 4

Python rounds exact halves to the nearest even number ("banker's rounding"), which avoids always rounding up and biasing totals. Use math.floor(x + 0.5) if you need school-style rounding for positive numbers.

Key takeaways

  • int is for whole numbers and float for decimals; mixing them gives a float.
  • / always returns a float, // rounds down, and % gives the remainder.
  • Python follows BODMAS: brackets, powers, then * / // %, then + -.
  • Floats are approximate, so round them for display and don't use them for exact money.

Practice

  1. A restaurant bill is ₹1,850, split equally between 4 friends with a 10% tip. How much does each person pay?
Show the answer
bill = 1850
each = bill * 1.10 / 4
print(round(each, 2))    # 508.75
  1. Convert 3,725 seconds into minutes and seconds.
Show the answer
seconds = 3725
minutes, remaining = divmod(seconds, 60)
print(minutes, "min", remaining, "s")    # 62 min 5 s
  1. Calculate the simple interest on ₹50,000 at 7% a year for 3 years, using SI = P × R × T / 100.
Show the answer
p, r, t = 50000, 7, 3
si = p * r * t / 100
print(si)    # 10500.0
  1. Print the sum of the digits of the number 482 using % and //.
Show the answer
n = 482
total = n % 10 + (n // 10) % 10 + n // 100
print(total)    # 14

With a loop you can handle any number of digits; see Python loops.

  1. What do these print? print(2 + 3 * 2 ** 2) and print(10 - 4 / 2)
Show the answer

14 (powers first: 2 ** 2 = 4, then 3 * 4 = 12, then 2 + 12) and 8.0 (4 / 2 = 2.0, a float, then 10 - 2.0).

Interview questions

What is the difference between / and // in Python?

/ is true division and always returns a float: 7 / 2 is 3.5. // is floor division and rounds down to the nearest whole number: 7 // 2 is 3 and -7 // 2 is -4. With two ints, // returns an int.

Why does 0.1 + 0.2 not equal 0.3 in Python?

Floats are stored in binary using the IEEE 754 standard, and 0.1 and 0.2 can't be represented exactly in binary, so a tiny error appears in the result. Compare floats with math.isclose(a, b) rather than ==, and use decimal.Decimal when exact decimal results matter.

What does round(2.5) return in Python 3?

2. Python 3 rounds exact halves to the nearest even number, so round(2.5) is 2 and round(3.5) is 4.

Is there an integer overflow in Python?

No. Python's int has arbitrary precision: it grows to hold any whole number, limited only by memory. Floats do have limits, and very large float calculations can give inf.

What to learn next

  1. Work with text in Python strings and string methods.
  2. Format numbers nicely, with commas and decimal places, using Python f-strings.