Why this matters
Real programs remember things: a name, a score, a total. A variable is how Python
remembers a value, and print is how it shows results. Master these two and you can already
write programs that calculate and report.
The idea
A variable is like a labelled box. You assign a value with =:
city = 'Cairo'
Read = as "put the right side into the left side". It is not the maths equals sign.
A piece of text in quotes is a string ('Cairo'); a plain number is an integer or a
decimal (5, 1.5).
print(...) displays whatever is inside the parentheses. Strings need quotes; numbers do
not. Python's arithmetic operators let you calculate:
| Operator | Meaning |
|---|---|
+ - * / | add, subtract, multiply, divide |
// | whole-number quotient |
% | remainder |
** | power |
Picture it
flowchart TD A([Start]) --> B[a = 5] B --> C[b = 3] C --> D[print a + b] D --> E([End])
Worked example
a = 5
b = 3
print(a + b) # 8
print(a // b) # 1 (whole-number quotient)
print(a % b) # 2 (remainder)
print(a ** b) # 125 (5 to the power 3)
Your turn
Predict what a small program prints, then write a function that computes a value from its inputs and returns it.
Recap
- A variable stores a value;
=assigns the right side to the left. print(...)displays a value; strings use quotes, numbers do not.//is the quotient,%the remainder,**the power.