Skip to the content

Operations

Python · Unit 1: First steps

+, -, * and / work like on a calculator.

Watch out: / always gives a decimal (float), even when the division is exact.

print(3 * 4)
print(8 / 2)

Prints

12
4.0

// divides and keeps the whole part.

% gives what's left over from the division, and ** raises to a power.

print(7 // 2)
print(7 % 2)
print(2 ** 3)

Prints

3
1
8

Python follows the same order as at school: first **, then *, /, // and %, and + and - at the end.

Whatever goes inside parentheses is worked out before anything else.

print(10 - 2 * 3)
print((10 - 2) * 3)

Prints

4
24

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

    pizza = 8
    people = 4
    print(pizza / people)
  2. 2. Put the lines in order

    Order the lines to work out a bill with a 20 % tip.

  3. 3. Find the bug

    This program should print how many minutes there are in 3 hours. Which line has the error?

    hours = 3
    minutes = hours * 6
    print(minutes)
  4. 4. Predict the output

    What does this code print?

    print(10 % 3)
  5. 5. Multiple choice

    What is 9 // 2 worth?

  6. 6. Complete the code

    Fill in so it prints 8 and then 1.

    print(17 ___ 2)
    print(17 ___ 2)
  7. 7. Complete the code

    A number is even if dividing it by 2 leaves 0. Fill in to see what's left over.

    number = 14
    print(number ___ 2)
  8. 8. Predict the output

    Python multiplies before it adds. What does it print?

    print(2 + 3 * 4)
  9. 9. Find the bug

    This program should print the average of 4 and 6. Which line has the error?

    a = 4
    b = 6
    average = a + b / 2
    print(average)
  10. 10. Put the lines in order

    Order the lines to work out the area of a square with side 5 (side times side).

  11. 11. Predict the output

    How many hours and minutes are 135 minutes?

    minutes = 135
    hours = minutes // 60
    rest = minutes % 60
    print(hours, "h", rest, "min")
Do this lesson

It opens in your browser. It is free and you do not need an account to start.

See all lessons →