Python Operators
Beginner Python Basics

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.

GoalBy the end you can do arithmetic including //, % and **, predict the order Python works things out in, compare values, combine conditions with and/or/not, and update variables with +=.

Before this lesson: Python Variables, Python Data Types

2Python Operators: diagram

Python Operatorsoperatormeaningexampleresult+ - *add, subtract, multiply7 * 214/divide7 / 23.5//floor divide7 // 23%remainder7 % 21**power7 ** 249== != < >compare7 > 2Trueand or notcombine True/False7 > 2 and 2 > 7False+= -=update a variablen = 7; n += 2n is 9/ always gives a float; // and % give the whole part and the remainder
The main operator families with one example each. Arithmetic produces numbers, comparisons and logical operators produce True or False, and assignment operators update a variable in place.

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 / 2 is 4.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 ** 3 is 8.
Python
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.

Python
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 b is True only when both sides are True.
  • a or b is True when at least one side is True.
  • not a flips True to False and False to True.
Python
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

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.

Interactive example
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.

  1. Line 1-3The inputs: 13 guests, 8 slices in a pizza and 2 slices for each guest.
  2. Line 5* multiplies: 13 × 2 = 26 slices are needed.
  3. Line 6// divides and rounds down: 26 // 8 = 3 whole pizzas. That's only 24 slices — not enough.
  4. Line 7-8% gives the remainder: 26 % 8 = 2. Because it isn't 0, two slices are missing, so pizzas += 1 adds one more pizza: 4.
  5. Line 9-11The results are printed. In line 11 * happens before -, so it's 4 × 8 − 26 = 6 spare slices.
  6. Line 13An int times a float gives a float: 4 × 11.5 = 46.0.
  7. Line 14/ gives 3.538…, and round(..., 2) keeps two decimal places: 3.54.
  8. Line 16-17<= compares the cost with the budget and produces a bool: True.
  9. Line 18and is True only if both comparisons are True. 13 > 10 and 4 >= 3, so the result is True.

8Python Operators: common mistakes

Forgetting the order of operations

✗ Wrong

average = 80 + 90 / 2
print(average)
No error, but it prints 125.0 instead of 85.0

✓ 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!")
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

✓ 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)
No error, but it prints Sunday instead of False

✓ 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

Using 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 — True if width and height are equal, otherwise False. Use a comparison operator rather than typing True or False.

Then print all three.

Your code
width = 7
height = 4

# create area, perimeter and is_square

2. Predict the results

Predict the output Medium

Read the code and type exactly what it prints. Think about the type each operator returns and the order Python works things out in.

Read this code
print(10 / 4)
print(10 // 4, 10 % 4)
print(2 + 3 * 2 ** 2)
print(not 5 > 3 or 1 == 1.0)

2 more exercises → 4 more examples

Finished this lesson?
Progress is saved in this browser — create a free account to keep it across devices.

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