6.3.1–6.3.2 – Values, variables and constants
Choose suitable primitive values, trace assignments and conversions, and distinguish changing variables from named constants. Build a small data setup whose values and types match its requirements.
Choosing primitive data types
A data type describes the kind of value and the operations that make sense for it. Choose a type from the meaning of the data, not just its appearance on screen. Pearson names four primitive types: each represents one simple value rather than a collection of items.
| Type | Meaning | Python representation | Example value |
|---|---|---|---|
| Integer | Whole number | int | 4 |
| Real | Number that may have a fractional part | float | 12.6 |
| Boolean | One of two truth values | bool | True |
| Character | One character | One-character str | "K" |
Python has no separate char type. It represents a character using a string of length one; longer text also uses str. The strings "7" and "False" are text, whereas 7 is an integer and False is a Boolean. Boolean literals start with a capital letter and have no quotation marks.
attempts = 4
lap_time = 12.6
door_open = True
player_initial = "K"
Each statement associates a name with a value. A quantity such as an attempt count is an integer; a measurement such as a lap time may need a real. A flag such as whether a door is open is Boolean. A one-letter initial is a character represented by a string. A reference with leading zeros, such as "0042", should remain text if those zeros are part of its identity.
Initialising and updating variables
A variable is an identifier associated with a value that can change. Its first assignment is its initialisation. Later assignments replace the current value; they do not retain a history in the variable.
score = 3
saved_score = score
score = 5
print(score)
print(saved_score)
Python evaluates the right-hand side before assigning to the left. Here saved_score receives the integer 3. Later changing score does not change that separate stored integer.
5
3
print() displays its argument and starts a new output line. This lets us inspect stored values. The assignment sign = means set/change a value, not test equality.
| After source line | score | saved_score |
|---|---|---|
| 1 | 3 | not assigned |
| 2 | 3 | 3 |
| 3 | 5 | 3 |
PLS identifiers start with a letter and then use letters, digits or underscores. Names are case-sensitive: score and Score differ. Choose names that explain a role, such as questions_remaining, rather than an unexplained x. Do not use a Python keyword as an identifier. Python can assign a different type to an existing name, but repeatedly changing the type of one stable role often makes the program harder to understand.
Converting values
Type conversion returns a value of another allowable type. Assign the returned value if you need to retain it. Conversion does not automatically rewrite the source variable.
| Expression | Result | Reason |
|---|---|---|
int("18") | 18 | Numeric text to integer |
int(7.9) | 7 | Fraction discarded towards zero |
int(-7.9) | -7 | Truncation is not rounding down |
float("4.25") | 4.25 | Numeric text to real |
str(18) | "18" | Integer to text |
bool(0) | False | Numeric zero is false |
bool(2) | True | Nonzero number is true |
bool("") | False | Empty string is false |
bool("False") | True | Nonempty text is true |
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account