S9 · Computer Studies I · Grade 10 · ages 15–16
Variables & Operators
Identifiers, data types and operators in Python
Grade 10 · 3.3 Identifiers and OperatorsCBE strandFree · works offline
▶ Start the interactive lessonThe full lesson adds narration, an interactive mission and a 10-question bank, and the quiz explains any mistakes.
Variables and constants
A variable is a named box in memory whose value can change, for example marks = 85. A constant is a value that should not change while the program runs. Python has no special constant type, so by convention constants are written in capitals: PASS_MARK = 50.
Naming rules (identifiers)
An identifier is the name you give a variable or function. In Python it may contain letters, digits and underscores, but must not start with a digit, must have no spaces, and must not be a keyword like if, for or class. Names are case-sensitive: Marks and marks are different. Choose meaningful names like total_fees, not x.
Data types
Every value has a type. int is a whole number (42), float is a decimal (3.5), str is text in quotes ("Nairobi"), and bool is True or False. The type decides what you can do: 5 + 3 is 8, but "5" + "3" joins text and gives "53".
Input and output
print() shows output on the screen. input() reads what the user types, and always gives back text (a str). To do maths with it, convert it first: age = int(input("Age? ")).
Operators
Arithmetic: + − * / for add, subtract, multiply and divide, // for whole-number division, % for the remainder, ** for power. Comparison: == (equal to), != (not equal), <, >, <=, >=, which give True or False. Logical: and, or, not combine conditions. = assigns a value; == compares.
Key points
- A variable is a named box whose value can change.
- By convention, constants are written in capitals, like PASS_MARK.
- Names can't start with a digit, can't have spaces, and can't be keywords.
- int is whole numbers, float is decimals, str is text, bool is True or False.
- input always gives text, so convert it with int before doing maths.
- Double slash divides to a whole number; percent gives the remainder.
- One equals sign assigns; two equals signs compare.
Try it for real: Your first variables
- If you can, open Python (on a computer, or a free online Python editor) and try: name = input("Name? ") then print("Habari", name).
- Write a program that asks for a price in KSh and prints the price with 16% VAT added.