A program is a list of instructions carried out in order, and the very first thing it needs is somewhere to put values. A variable is a named box in memory. You put a value in it with an assignment, and from then on the name stands for whatever is currently inside. The name is for you; the computer only cares about the address.
= means "take what is on the right and store it in the name on the left". So count = count + 1 is not a false equation — it reads the current value of count, adds one, and stores the result back. To test whether two things are equal you use ==, which is a completely different operator.Data types
| Type | Python name | Example | What it is for |
|---|---|---|---|
| Integer | int | 42, -7, 0 | Counting — whole numbers only |
| Floating point | float | 3.14, -0.5, 2.0 | Measurements, anything with a decimal |
| String | str | "TEJ3M", "5" | Text, including digits used as text |
| Boolean | bool | True, False | The result of a comparison; conditions |
| List | list | [4, 8, 15] | An ordered collection of values |
Python decides a variable's type from the value you assign, which is convenient but means type errors surface at run time rather than when you write the code. The distinction that trips everyone up is the string "5" versus the integer 5. They look identical on screen and behave completely differently: 5 + 5 gives 10, but "5" + "5" gives "55", because + on strings means join.
Input and output
| Python | What it does |
|---|---|
name = input("Your name: ") | Shows the prompt and stores what the user types — ALWAYS as a string |
age = int(input("Age: ")) | Converts the typed text to an integer before storing it |
temp = float(input("Temp: ")) | Converts to a floating point number |
print("Hello", name) | Prints values separated by a space |
print(f"Hi {name}, you are {age}") | An f-string: substitutes each variable inside the braces |
input() hands you the string "25". Forget to convert and age + 1 raises a TypeError, while age * 2 silently produces "2525" — which is worse, because it runs without complaining. Wrap every numeric input in int() or float() at the moment you read it.Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - * | Add, subtract, multiply | 7 * 3 | 21 |
/ | Division — always gives a float | 7 / 2 | 3.5 |
// | Integer division — discards the remainder | 7 // 2 | 3 |
% | Modulo — the remainder only | 7 % 2 | 1 |
** | Exponent | 2 ** 8 | 256 |
n % 2 == 0 tests whether n is even. n % 5 == 0 tests divisibility by 5. seconds % 60 gives the seconds left over after whole minutes. Any "every nth time" or "wrap around" problem is a modulo problem — including flashing an LED on every third loop.Planning before coding
Writing code before you know what it should do is the slowest way to write it. Two planning tools are standard. A flowchart uses the shapes above to draw the path a program takes. Pseudocode writes the logic in structured English with no syntax to get wrong. Both are about getting the logic right while it is still cheap to change.
| Pseudocode | Python |
|---|---|
READ radius | radius = float(input("Radius: ")) |
SET area TO 3.14159 * radius * radius | area = 3.14159 * radius ** 2 |
PRINT "Area is", area | print(f"Area is {area:.2f}") |
total_marks, not tm or x. Never use a single letter except for a loop counter. A well-named variable removes the need for a comment, and you will read your own code far more often than you write it.