Stewdent.eu
▾

Assignment and Expressions

Assignment stores a value in a variable. Expressions calculate new values using operators. Together they are the engine behind almost every program.

Last updated: 24 December 2025

★☆☆☆☆ Level 1computingprogrammingVariablesexpressionsoperators4 min read

Report an error

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!"
Note

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:

OperatorMeaningExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication3 * 721
/Division15 / 43.75
//Integer division15 // 43
%Remainder (modulo)15 % 43
**Exponentiation2 ** 8256
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
Tip

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)     # --------------------
Warning

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
x squared is 49, y times 4 is 12. So result = 49 - 12 = 37.

What does the % operator do? Give an example of when it would be useful in a program.

Show answer
The % operator returns the remainder after division. For example, 17 % 5 = 2. It is useful for checking whether a number is even or odd (number % 2 == 0 means even), or for cycling through values in a fixed range (such as positions in a circular list).