Python Operators
1Python Operators: overview
Operators are symbols such as +, == and and that combine values into a new value — a number from arithmetic, or True/False from a comparison or a logical test.
Before this lesson: Python Variables, Python Data Types
2Python Operators: diagram
3Python Operators: explained step by step
An operator is a symbol that takes one or two values and produces a new one. 7 + 2 gives 9; 7 > 2 gives True. Python's operators come in a few families.
Arithmetic operators
+, - and * work as you'd expect. Division has some relatives worth knowing:
/is ordinary division and always returns a float:8 / 2is4.0.//is floor division: it divides, then rounds down to a whole number.%(modulo) gives the remainder left over after floor division.**raises to a power:2 ** 3is8.
print(17 + 5, 17 - 5, 17 * 5)
print(17 / 5)
print(17 // 5, 17 % 5)
print(2 ** 10)// and % work as a pair
17 // 5 is 3 and 17 % 5 is 2, because 17 = 3 × 5 + 2. Together they split an amount into whole groups plus what's left over — minutes into hours and minutes, or sweets shared between children. % also tells you whether one number divides another evenly: n % 2 == 0 is the classic test for an even number.
Order of operations
Python follows the usual maths rules: ** first, then *, /, // and %, then + and -. Operators with the same priority are worked out from left to right. Parentheses override everything, and adding them even when they aren't required often makes code clearer.
print(2 + 3 * 4)
print((2 + 3) * 4)
print(10 - 4 - 3)Comparison operators
== (equal), != (not equal), <, >, <= and >= compare two values and always produce a bool, True or False. Don't confuse ==, which asks a question, with =, which assigns a value. You can also chain comparisons the way you would in maths: 0 <= x <= 10 checks that x is between 0 and 10.
Logical operators
and, or and not combine True/False values:
a and bis True only when both sides are True.a or bis True when at least one side is True.not aflips True to False and False to True.
age = 25
has_ticket = True
print(age >= 18 and has_ticket)
print(age < 12 or age > 65)
print(not has_ticket)Python stops as soon as it knows the answer: in False and ..., the right side is never even looked at.
Assignment operators
Updating a variable from its current value is so common that Python has shortcuts: count += 1 means count = count + 1. The same pattern works with -=, *=, /=, //=, %= and **=. Python has no ++ operator — write += 1 instead.
Quick reference: Python Operators
- Python is vs == Operator: What Is the Difference? — `==` checks whether two objects have **equal values**, while `is` checks whether they are the **exact same ob…
- Python Division (/) vs Floor Division (//) — `/` is **true division** and always returns a float (`7 / 2` is `3.5`), while `//` is **floor division** and …
- Python Program to Check Even or Odd Number — To check if a number is even or odd in Python, test `number % 2 == 0`: a remainder of 0 means the number is e…
- Python Program to Find the Sum of Digits of a Number — To find the sum of the digits of a number in Python, either convert it to a string and add each digit with `s…
- Python abs() Function — `abs(x)` returns the absolute value of a number: its distance from zero, so `abs(-7)` is `7`.
- Python divmod() Function — `divmod(a, b)` returns the tuple `(a // b, a % b)`: the floor quotient and the remainder from a single divisi…
- Python pow() Function — `pow(base, exp, mod=None)` returns `base` raised to the power `exp`; with `mod` it returns `(base ** exp) % m…
4Python Operators: syntax
a + b a - b a * b a / b # arithmetic (/ always gives a float)
a // b a % b a ** b # floor division, remainder, power
a == b a != b a < b a >= b # comparison -> True or False
x and y x or y not x # logical
n += 1 n -= 2 n *= 3 # update a variable in place
5Python Operators: run the example
Press Run (or Ctrl + Enter). The output (step 6) appears directly below the code. Change anything and run it again.
guests = 13
slices_per_pizza = 8
slices_each = 2
slices_needed = guests * slices_each
pizzas = slices_needed // slices_per_pizza
if slices_needed % slices_per_pizza != 0:
pizzas += 1
print("Slices needed:", slices_needed)
print("Pizzas to order:", pizzas)
print("Leftover slices:", pizzas * slices_per_pizza - slices_needed)
cost = pizzas * 11.5
print("Cost per guest:", round(cost / guests, 2))
budget = 60
print("Within budget?", cost <= budget)
print("Big party?", guests > 10 and pizzas >= 3)Expected outputSlices needed: 26
Pizzas to order: 4
Leftover slices: 6
Cost per guest: 3.54
Within budget? True
Big party? True
7Python Operators: how the example works
Hover or tap a step to highlight its lines in the example above.
- Line 1-3The inputs: 13 guests, 8 slices in a pizza and 2 slices for each guest.
- Line 5
*multiplies: 13 × 2 = 26 slices are needed. - Line 6
//divides and rounds down: 26 // 8 = 3 whole pizzas. That's only 24 slices — not enough. - Line 7-8
%gives the remainder: 26 % 8 = 2. Because it isn't 0, two slices are missing, sopizzas += 1adds one more pizza: 4. - Line 9-11The results are printed. In line 11
*happens before-, so it's 4 × 8 − 26 = 6 spare slices. - Line 13An int times a float gives a float: 4 × 11.5 = 46.0.
- Line 14
/gives 3.538…, andround(..., 2)keeps two decimal places: 3.54. - Line 16-17
<=compares the cost with the budget and produces a bool:True. - Line 18
andis True only if both comparisons are True. 13 > 10 and 4 >= 3, so the result isTrue.
8Python Operators: common mistakes
Forgetting the order of operations
✗ Wrong
average = 80 + 90 / 2
print(average)✓ Right
average = (80 + 90) / 2
print(average)Division happens before addition, so Python worked out 80 + 45. Put the sum in parentheses so it happens first.
Using = instead of == to compare
✗ Wrong
score = 100
if score = 100:
print("Perfect!")✓ Right
score = 100
if score == 100:
print("Perfect!")A single = assigns a value; it can't ask a question. Use == to test whether two values are equal.
Comparing with several values using a bare or
✗ Wrong
day = "Tuesday"
is_weekend = day == "Saturday" or "Sunday"
print(is_weekend)✓ Right
day = "Tuesday"
is_weekend = day == "Saturday" or day == "Sunday"
print(is_weekend)Each side of or is a separate test. "Sunday" on its own is non-empty text, which counts as true, so it becomes the result. Repeat the comparison on both sides.
9Python Operators: exercises
Write your answer, then press Check. The checks run in your browser.
1. Rectangle calculator
Write code Easy ✓ CompletedUsing the given width and height, create three variables:
area— width times height,perimeter— the distance all the way round (2 × width + 2 × height),is_square—Trueif width and height are equal, otherwiseFalse. Use a comparison operator rather than typing True or False.
Then print all three.
width = 7
height = 4
# create area, perimeter and is_squarewidth = 7
height = 4
area = width * height
perimeter = 2 * width + 2 * height
is_square = width == height
print(area, perimeter, is_square)
2. Predict the results
Predict the output Medium ✓ CompletedRead the code and type exactly what it prints. Think about the type each operator returns and the order Python works things out in.
print(10 / 4)
print(10 // 4, 10 % 4)
print(2 + 3 * 2 ** 2)
print(not 5 > 3 or 1 == 1.0)2.5
2 2
14
True
2 more exercises → 4 more examples
Questions learners ask
What is the difference between / and // in Python?
/ is true division and always returns a float: 7 / 2 is 3.5 and 8 / 2 is 4.0. // is floor division: it rounds the result down to a whole number, so 7 // 2 is 3. With negative numbers "down" means towards minus infinity, so -7 // 2 is -4.
What does % do in Python?
% gives the remainder after division: 17 % 5 is 2. It's handy for checking whether a number is even (n % 2 == 0), getting the last digit of a number (n % 10) and wrapping values around, like hours on a clock.
Does Python have ++ or -- like other languages?
No. Use count += 1 or count -= 1. Writing ++count isn't an error, but it only means +(+count), so the value doesn't change — a sneaky bug to watch out for.
Further reading
10Next lesson
Up nextPython StringsA string is a sequence of characters — text — written inside quotes, which you can index, slice, combine and transform …Related:
Python StringsPython if, elif and elsePython Calculator Project
← Python Booleans · All Python Basics lessons · Practise in the IDE