Exerpad
🔥 1
10 XP
Lesson · Chapter: Linear Programs+10 XP for reading

Programs Step by Step

Programs run from top to bottom, one line at a time. Let's learn how to do math and build text!

Prefer to watch? This chapter as a video:

Where you'll use this
Shop receipts, tip calculators, level-up trackers — programs that calculate for you are half of all software.

Doing Math

Python can do math with these operators:

python
print(3 + 2) # Addition: 5
print(10 - 4) # Subtraction: 6
print(3 * 5) # Multiplication: 15
print(10 / 3) # Division: 3.3333...
print(10 // 3) # Integer division: 3
print(10 % 3) # Remainder: 1
Output
Press Run to see the output.

Math with Variables

You can store numbers and compute results:

python
width = 5
height = 3
area = width * height
print(area)
Output
Press Run to see the output.

Output:

15

Building Strings

You can join strings together with +:

python
first = "Hello"
second = "World"
message = first + " " + second
print(message)
Output
Press Run to see the output.

Output:

Hello World

f-strings

f-strings are the easiest way to mix text and variables:

python
name = "Alex"
age = 10
print(f"Hello, {name}! You are {age} years old.")
Output
Press Run to see the output.

Output:

Hello, Alex! You are 10 years old.

Put f before the quotes, then use {variable} to insert values.

Order of Operations

Python follows math rules — multiplication and division happen before addition and subtraction:

python
print(2 + 3 * 4) # 14, not 20
print((2 + 3) * 4) # 20, parentheses first
Output
Press Run to see the output.
Did you know?
The word "computer" used to mean a person! Before machines, a computer was someone whose whole job was doing math by hand — NASA employed rooms full of them to calculate rocket paths.

Check yourself

Pause & predict — what will this print?
print(10 - 2 * 3)