4.1.1.1 - Data types and references (Python)

4.1.1.1 - Data types and references (Python)

Choose suitable types for a program's values, follow references to objects, and build records and indexed collections in Python.

Built-in types and suitable values

A value's data type determines what kind of value it is and which operations make sense for it. A count can be added to another count; a name can be joined to another piece of text. Choosing a type is a modelling decision, not just a way to store printed symbols.

Data type

A classification of values with a set of permitted operations and representation rules.

RequirementSuitable typePython example and reason
Number of ticketsInteger, inttickets = 12: a whole-number count
Measured temperatureReal/float, floattemperature = 18.5: permits fractional values
Whether a gate is openBoolean, boolgate_open = True: true or false
One initialCharacterinitial = "R": Python uses a length-one str, with no separate character type
A postcodeString, strpostcode = "AB1 2CD": textual symbols, not a quantity
A calendar deadlineDate/timeA date or datetime object supports calendar operations

Quotes matter: "12" is text whereas 12 is an integer. "12" + "3" concatenates to "123"; 12 + 3 adds to 15. float approximates many real numbers with finite precision. It is suitable for many measurements but does not make every decimal fraction exact. Python integers are not restricted to a fixed small range by an explicit declaration.

Assignment binds a name to an object: tickets = 12. Python determines the type of the object at runtime; tickets = "sold out" subsequently binds the same name to a string. That freedom does not make inconsistent types a good design. type(tickets) lets you inspect the current object's type.

Python
from datetime import date, datetime

tickets = 12
temperature = 18.5
gate_open = True
initial = "R"
deadline = date(2027, 5, 14)
arrival = datetime(2027, 5, 14, 9, 30)
print(type(tickets).__name__, type(temperature).__name__)
print(gate_open, len(initial), deadline, arrival)

The date arguments are year, month, day. datetime additionally accepts hour and minute here. These are objects with date/time meaning, rather than strings that happen to look like dates. Detailed conversion is taught in the strings and dates lesson.

References, mutation and rebinding

Two names can refer to the same object, so changing that object can be visible through either name. A reference provides access to an object created at runtime. In a language with explicit pointer/reference variables, such a variable stores the object's memory address. Python manages references for you; ordinary Python source does not expose address arithmetic or a pointer declaration.

Alias

A second name or reference that refers to the same object.

Lists are mutable: their existing contents can change. In second = first, Python does not automatically copy a list. By contrast, assigning second = [9, 9] later makes second refer to a different list; it does not replace first's list. That distinction is mutation versus rebinding.

Follow a shared list

{"code":"first = [4, 7]\nsecond = first\nsecond[0] = 9\nprint(first, second)\nsecond = [1, 2]\nprint(first, second)\n","instructions":"Predict both output lines before Run. Then replace second = first with second = first.copy(), predict the effect of second[0] = 9, and run again. Explain which version shares one list and which creates a separate shallow copy.","files":[],"original_filename":"main.py","runtime_profile":"python-console-v1","file_mode":"fresh"}

Continue this lesson.

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

Create free account