Python / Getting started

Calculations and text

Use every Python arithmetic operator, know the order they run in, tell numbers from text, and join them in one sentence. Baccalaureate

A computer is a fast, exact calculator. Put a calculation inside print() without quotes and it prints the result, even for huge numbers:

Python
print(278 + 422)
print(3000 - 850)
print(7 * 6)
print(48 / 6)
print(300000000000 * 200000000000)
Console
700
2150
42
8.0
60000000000000000000000

Sometimes you don't want a decimal answer, but how many whole times a number fits and what is left over. For example: 17 sweets shared by 5 children.

Python
print(17 / 5)
print(17 // 5)
print(17 % 5)
print(2 ** 3)
Console
3.4
3
2
8
OperationSymbolExampleResult
Add+278 + 422700
Subtract-3000 - 8502150
Multiply*7 * 642
Divide/17 / 53.4
Whole division (no fraction)//17 // 53
Remainder%17 % 52
Power**2 ** 38

Order of operations

Just like in maths: power first, then multiply and divide, then add and subtract. Brackets come before everything:

Python
print(2 + 3 * 4)
print((2 + 3) * 4)
Console
14
20

Numbers and text

Any value in quotes is text (str), even if it is digits. A whole number is an int and a decimal is a float. type() tells you the type:

Python
print(type(100))
print(type("100"))
print(type(8.0))
print(10 + 5)
print("10" + "5")
Console
<class 'int'>
<class 'str'>
<class 'float'>
15
105

+ between two texts glues them. You cannot glue text to a number directly, so turn the number into text with str(). The other way round, int() turns digit text into a number:

Python
print("2 bottles of juice " + str(200 * 2) + " pounds")
print(int("25") + 5)
Console
2 bottles of juice 400 pounds
30
Mind the spaces inside the quotes before and after the number; without them the words stick to it: juice400pounds.

Practice

1Write the programCurriculum practice

Make the computer calculate 3000 - 850 and print the result.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Write the programCurriculum practice

Make the computer calculate 300000000000 × 200000000000 and print the result.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

A bottle of juice costs 200 pounds. Fill in the blank to print 2 bottles of juice 400 pounds, letting the computer calculate 200 × 2.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
print("2 bottles of juice " + ___ + " pounds")
Console
Press Run to see the result.
4Edit the programCurriculum practice

The program prints The answer is 2 * 5. Change it to print The answer is 10., with the computer calculating 2 × 5.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
print("The answer is 2 * 5.")
Console
Press Run to see the result.
5Write the programCurriculum practice

Print the data type of the value "100" (with the quotes) using type().

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
6Write the programExtra practice

A film lasts 125 minutes. Print 2 h 5 min, letting the computer work out the hours with // and the leftover minutes with %.

HintAn hour is 60 minutes.
Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

Mariam bought 3 notebooks at 15 pounds each and a pen for 10 pounds. Print Total: 55 pounds, letting the computer calculate 3 × 15 + 10 in one line.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • Operators: + - * /, plus // for whole division, % for remainder and ** for power.
  • Multiply and divide before add and subtract; brackets before everything.
  • Anything in quotes is text; str() turns a number into text to join with +, and int() turns text into a number.