Codiqo

Learn Python · lesson 2 of 60 · about 6 minutes

Numbers and Arithmetic

Let your assistant do the sums: add, subtract, multiply, divide and use brackets.

What you'll learn

  • Calculate with + - * and /
  • Know what's worked out first
  • Know why / always gives a decimal

Let Python work it out

You're saving 15 a week for a bike that costs 120. How many weeks is that? You could reach for a calculator, but your assistant can work it out, and work it out again whenever the numbers change.

Numbers don't need quotes. +, -, * and / add, subtract, multiply and divide.

print() shows the answer, not the sum

print("Weeks to save:", 120 / 15)

Output

Weeks to save: 8.0

The four everyday operators

print(7 + 3)
print(10 - 4)
print(6 * 7)
print(7 / 2)

Output

10
6
42
3.5

What's worked out first

Like in maths, * and / happen before + and -. Brackets go first of all, so use them to say what you mean.

/ always gives a decimal number, a float, even when it divides exactly: 120 / 15 is 8.0.

Brackets change the answer

print(2 + 3 * 4)
print((2 + 3) * 4)

Output

14
20

Tip

A comma inside print() puts a space between the things it shows: print("Total:", 12) shows Total: 12.

Now try it yourself

This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.