A variable is a labelled box
A variable is a name attached to a value stored in memory. Writing age = 15 creates the name age and points it at the value 15. Writing age = 16 later does not create a second variable — it points the same name at a new value, and the old one is gone.
Python decides the type from the value you give it, which is called dynamic typing. You never declare that age is a whole number; Python sees 15 and knows. That makes the language quick to write and means a mistake about types is discovered when the program runs rather than before.
| Type | Holds | Example |
|---|---|---|
| int | a whole number | marks = 87 |
| float | a number with a decimal part | average = 72.5 |
| str | text | name = "Ayesha" |
| bool | True or False | passed = True |
| list | an ordered collection | scores = [80, 75, 91] |
Naming rules, and the one that catches people
A name may contain letters, digits and underscores, must not start with a digit, and must not be a Python keyword such as if, for or print. Names are case-sensitive, so Total and total are two different variables — and a program that assigns to one and prints the other fails with an error that names a variable you are sure you defined.
Input always arrives as text
input() displays a prompt and returns whatever the user typed — always as a string, even when it looks like a number. This single fact causes more first-program bugs than anything else.
If you read two numbers and add them without converting, Python joins the text instead of adding: "5" + "3" is "53". Convert with int() or float() first.
A program asks for two numbers and prints their total. Explain why a = input(); b = input(); print(a + b) prints 53 when the user types 5 and 3.
input()returns a string, soaholds"5"andbholds"3".The quotes are the point — these are text, not numbers.- The
+operator does different things for different types.For numbers it adds; for strings it joins end to end, which is called concatenation. - Since both are strings,
"5" + "3"produces"53".No error occurs, which is why this bug is easy to miss — the program runs and gives a wrong answer. - Fix it with
a = int(input())andb = int(input()).Now both are integers and + means addition, giving 8.
Both values are strings, so + joins them. Convert with int() first.
Operators
Python's arithmetic operators are mostly familiar, with three worth noting. / always produces a float, even when the division is exact — 6 / 3 is 2.0, not 2. // is integer division, discarding the remainder. % gives the remainder itself, and is how you test whether a number divides exactly.
| Operator | Meaning | Example |
|---|---|---|
| + − * | add, subtract, multiply | 7 * 3 → 21 |
| / | divide, always giving a float | 7 / 2 → 3.5 |
| // | integer division | 7 // 2 → 3 |
| % | remainder (modulus) | 7 % 2 → 1 |
| ** | power | 2 ** 8 → 256 |
| == != < > | comparison, giving True or False | 5 > 3 → True |
| and or not | combining conditions | x > 0 and x < 10 |
One equals sign is not two
= assigns a value: x = 5 puts 5 into x. == compares: x == 5 asks whether x holds 5 and answers True or False. Writing if x = 5: is a syntax error in Python — which is fortunate, because in some other languages it silently assigns and the condition is always true.
Following a program by hand
Reading code is a skill separate from writing it, and it is what exam questions test most often. Work through line by line, writing down what every variable holds after each statement — exactly as a trace table does.
Do it slowly for programs you think you understand. A trace is the only reliable way to catch the difference between what you meant and what you wrote.
Choose Swap and step to line 4. a is already 8, so without temp holding the original value the 5 would be lost — which is why swapping two variables always needs a third.
Comments, and writing code someone can read
A comment begins with # and is ignored by Python entirely. Its purpose is the human reader — including you, six months later.
Good comments explain why, not what. # add 1 to count next to count = count + 1 is noise; # count only students who actually sat the paper is worth having. The other half of readable code is naming: total_marks tells the reader something that t does not, and costs nothing.
Before you leave this chapter
- A variable is a name pointing at a value; assigning again replaces it.
- Python infers the type from the value — int, float, str, bool, list.
- input() always returns a string. Wrap it in int() or float() for numbers.
- / gives a float, // discards the remainder, % gives the remainder.
- = assigns, == compares. Names are case-sensitive.