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!
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
Math with Variables
You can store numbers and compute results:
python
width = 5
height = 3
area = width * height
print(area)
Output:
text
15
Building Strings
You can join strings together with +:
python
first = "Hello"
second = "World"
message = first + " " + second
print(message)
Output:
text
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:
text
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