Every time a program calculates something — a score, a distance, a temperature — it uses expressions and assignment. An expression is any combination of values, variables, and operators that produces a result. Assignment takes that result and stores it in a variable for later use.
The Assignment Operator
In Python, the single equals sign = is the assignment operator. It means "take the value on the right and store it in the variable on the left".
age = 14
price = 9.99
greeting = "Hello, world!"
Do not confuse = (assignment) with == (comparison). age = 14 stores 14 in the variable age. age == 14 asks whether age is equal to 14 — it produces True or False but does not change anything.
Arithmetic Expressions
Python supports all the standard arithmetic operations:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 3 * 7 | 21 |
/ | Division | 15 / 4 | 3.75 |
// | Integer division | 15 // 4 | 3 |
% | Remainder (modulo) | 15 % 4 | 3 |
** | Exponentiation | 2 ** 8 | 256 |
base = 6
height = 4
area = (base * height) / 2 # expression on the right
print(area) # 12.0
Building Up Expressions
Expressions can use variables, literal values, and multiple operators all at once. Python follows standard mathematical order of operations — multiplication and division before addition and subtraction. Use brackets to be explicit:
total = 3 + 4 * 2 # evaluates as 3 + 8 = 11
total = (3 + 4) * 2 # evaluates as 7 * 2 = 14
Updating Variables
A very common pattern is updating a variable using its own current value:
score = 0
score = score + 10 # add 10 to whatever score currently holds
score = score + 5
print(score) # 15
Python provides shorthand operators for this:
score += 10 # same as score = score + 10
score -= 3 # same as score = score - 3
score *= 2 # same as score = score * 2
score //= 4 # same as score = score // 4
The += family of operators is extremely common in real code. Get comfortable with them early — you will see them constantly in loops and games.
String Expressions
Assignment is not just for numbers. The + operator on strings concatenates (joins) them:
first_name = "Sam"
last_name = "Taylor"
full_name = first_name + " " + last_name
print(full_name) # Sam Taylor
The * operator repeats a string:
divider = "-" * 20
print(divider) # --------------------
You cannot mix number types and string types with +. "Score: " + 42 will crash. You need to convert the number to a string first: "Score: " + str(42) or use an f-string: f"Score: {42}".
A Complete Example
item = "notebook"
price = 2.50
quantity = 4
total = price * quantity
discount = total * 0.1 # 10% off
final_price = total - discount
print(f"Buying {quantity} {item}s")
print(f"Total before discount: £{total:.2f}")
print(f"Final price: £{final_price:.2f}")
Practice Problems
What is the value of result after: x = 7; y = 3; result = x ** 2 - y * 4?
Show answer
What does the % operator do? Give an example of when it would be useful in a program.