6.3.1–6.3.2 – Values, variables and constants

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.

TypeMeaningPython representationExample value
IntegerWhole numberint4
RealNumber that may have a fractional partfloat12.6
BooleanOne of two truth valuesboolTrue
CharacterOne characterOne-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.

Python
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.

Python
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.

Output
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 linescoresaved_score
13not assigned
233
353

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.

ExpressionResultReason
int("18")18Numeric text to integer
int(7.9)7Fraction discarded towards zero
int(-7.9)-7Truncation is not rounding down
float("4.25")4.25Numeric text to real
str(18)"18"Integer to text
bool(0)FalseNumeric zero is false
bool(2)TrueNonzero number is true
bool("")FalseEmpty string is false
bool("False")TrueNonempty text is true

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account