A variable is a named storage location in memory whose value can change during the execution of a program. Variables hold data that a program needs to process.
score = 0 — 'score' is the identifier, 0 is the valueA constant is a named value that is set once and cannot be changed during program execution.
PI = 3.14159 — this value stays fixed throughout the program| Feature | Variable | Constant |
|---|---|---|
| Value can change | ✓ Yes | ✗ No |
| Set at runtime | ✓ Yes | Set at program start |
| Prevents modification | ✗ | ✓ Yes |
| Example use | Player score | Pi, VAT rate |
Every variable or constant has a data type which determines what kind of data it can store and how much memory it uses.
| Data type | Description | Example |
|---|---|---|
| Integer | Whole numbers (positive, negative, zero) | 42, −7, 0 |
| Real / Float | Numbers with a decimal point | 3.14, −0.5 |
| Boolean | True or False only | True, False |
| Character | A single character | 'A', '5', '!' |
| String | A sequence of characters (text) | "Hello", "CSZone" |
Sometimes you need to convert between data types — called casting:
int("42") converts the string "42" to integer 42str(42) converts integer 42 to string "42"float("3.14") converts string to real number8 Edexcel-style questions · instantly marked
| Term | Definition |
|---|
Timed exam-style test.