Lesson 3: Values, and the names that hold them
Module 1 · Lesson 3 · About 45 minutes · Needs: Lessons 1 and 2 (running a cell, print, reading an error's last line)The situation
Amira wants the computer to print a card for one book. Before it can print anything, it has to hold the facts: which book, who wrote it, who has it, and since when. The book she picks is Frankenstein, the copy Jonah borrowed on Monday 14 September and still has. It is loan L04, and the course follows it further than any other.
This lesson gives you the two things every program is made of: values (the facts, such as the year 1818) and variables (names for those facts).
A real case, first
On 23 July 1983, Air Canada Flight 143, a Boeing 767, ran out of fuel in the middle of a flight. The crew glided the aircraft to a landing at a former air force base at Gimli, Manitoba, which is why the event is remembered as the "Gimli Glider". Nobody was killed. Canada's Board of Inquiry reported on it in 1985.
The cause was a number without its meaning. The amount of fuel on board was worked out using the wrong unit: pounds where kilograms were needed. What the number measured was not recorded with it, so nobody saw that the aircraft carried far less fuel than everyone believed.
Shelf records invite the same mistake. Is 14 a number of days, of books, or of pence? A value on its own does not say. This lesson gives every value a name that does.
What you will be able to do
- Tell the four basic types of value apart and ask Python for a value's type.
- Give a value a name with
=, and change what a name points at. - Choose names Python accepts and other people can read.
- Recognise the keywords you cannot use as names.
- Assign several names on one line, and mark the shelf's rules as constants.
1. Values and their types at a glance (Essential)
Everything a program works with is a value: a title, a year, a fee. Python treats a year very differently from a title, so you need to know which kind you hold.
A bare value is what went wrong at Gimli. On the shelf, 14 could be the loan period, a count of books or a fee. The fix, built in this lesson, is a name that carries the meaning.
Each value has a type: the kind of value it is. Text is a str (short for string, a string of characters). A whole number is an int (integer). A number with a decimal point is a float (floating-point number). True and False are a bool (Boolean, after the mathematician George Boole), the type for yes or no.
Quotation marks change the type. 1818 with no quotation marks is a number Python can count with. "1818" is text that happens to hold four digit characters. Module 2 shows how to turn one into the other.
print(type("Frankenstein"))
print(type(1818))
print(type("1818"))
print(type(0.2))
print(type(True))Code walkthrough
type(...)is a tool built into Python: it reports the type of the value inside its brackets, andprintshows the report. Module 2 Lesson 2 looks at it closely.0.2is afloat: a 20-pence fee held carelessly as pounds. This course always holds money as whole pence, anint; Module 2 Lesson 1 shows why.Truehas a capital T and no quotation marks.
Output
<class 'str'>
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>Read <class 'int'> as "this is an int": class is Python's word for a type (Module 9).
2. Variables as names for values (Essential)
Typing "Frankenstein" every time is slow and easy to get wrong. A variable lets you write the value once and use its name from then on.
The line title = "Frankenstein" is an assignment. The equals sign = means "make this name refer to this value": the name title on the left, the value "Frankenstein" on the right. The spaces around = are optional but usual.
A variable is that name. Picture a label tied to a value: the name is not the value, it points at it. The course always draws a grey label with an arrow to a blue box.
Here is Jonah's loan in six variables, the Module 1 slice of the shared Booknook data.
title = "Frankenstein"
author = "Mary Shelley"
year = 1818
loan_id = "L04"
member = "Jonah"
borrowed = "Monday 14 September 2026"
print(title, "by", author, year)
print(loan_id, member, borrowed)Code walkthrough
- Six assignments.
yearholds a number, so it has no quotation marks; the other five hold text. printtakes several values separated by commas and shows them on one line, a space between each."by"is typed straight in; for a name,printshows the value it points at.
Output
Frankenstein by Mary Shelley 1818
L04 Jonah Monday 14 September 20263. Assignment and reassignment (Essential)
Facts on the shelf change. Jonah borrowed the only copy, so the count on the shelf went from one to none. A program follows changes like that by pointing a name at a new value.
Python runs an assignment in two steps: first it works out the value on the right, then it attaches the name on the left. So = is not "equals" in the maths sense; it is an instruction, done when the line runs. Testing equality uses == (Module 2).
Reassignment means assigning a new value to a name that already exists. The label moves to the new value. The old value is not changed; the name just stops pointing at it.
copies_on_shelf = 1
print("on the shelf:", copies_on_shelf)
copies_on_shelf = 0 # Jonah borrows it on 14 September
print("on the shelf:", copies_on_shelf)Code walkthrough
- Line 1 points
copies_on_shelfat1. Line 3 moves the same name to0; after the#is a comment, which Python ignores. - The two prints of one name give two answers, because a line in between moved it.
Output
on the shelf: 1
on the shelf: 0holder = member makes holder point at whatever member points at now, "Jonah". Move member to "Priya" later and holder does not follow. Names point at values, never at other names.
member = "Jonah"
holder = member
member = "Priya"
print("holder:", holder)
print("member:", member)Code walkthrough
- Line 2 points
holderat the valuememberholds,"Jonah". Line 3 moves onlymember.
Output
holder: Jonah
member: PriyaA name exists only after the line that makes it has run. Colab cells can run in any order, so a cell using title fails if the cell making it has not run this session. When in doubt, choose Runtime, then Run all.
Break it: use a name that was never made
Swap two letters: the cell below asks for tilte. It fails on purpose. Run this and you will see:
title = "Frankenstein"
print(tilte)Traceback (most recent call last):
File "<string>", line 2, in <module>
NameError: name 'tilte' is not defined. Did you mean: 'title'?Code walkthrough
- Line 2 asks for
tilte, a name no line has made. The error's last line says so.
A NameError means Python met a name it does not know. The last line names the problem and here guesses the fix; Colab may word or omit the guess.
4. Naming rules and conventions (Essential)
Python refuses some names outright, and accepts others that will confuse the next reader, often you a month later.
The rules: a name may use letters, digits and the underscore _, must not start with a digit, and holds no spaces or hyphens. Break one and Python stops before running anything, with a SyntaxError: code it cannot read.
These three lines each fail on purpose. Run each in its own cell and you will see:
2nd_copy = 2
due day = 28
due-day = 28 File "<string>", line 1
2nd_copy = 2
^
SyntaxError: invalid decimal literal
File "<string>", line 1
due day = 28
^^^
SyntaxError: invalid syntax
File "<string>", line 1
due-day = 28
^^^^^^^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?Code walkthrough
2nd_copystarts with a digit.due dayis two names side by side.due-dayreads asdueminusday, a sum you cannot assign to.- The
^marks show where Python gave up reading. A SyntaxError has no "Traceback" line, because nothing ran.
Names are case-sensitive: title and Title are two separate names, so making Title and printing title gives a NameError.
Conventions are habits Python does not enforce, written down in PEP 8, the style guide for Python code (a PEP is a Python Enhancement Proposal). For variables it asks for lower case words joined by underscores, snake_case: due_day, not DueDay. A good name also says what the value means, and its unit: the Gimli lesson again.
due_day = 28 # shelf day: 14 September is day 14
fee_per_day_pence = 20
copies_on_shelf = 0
print("L04 due on day", due_day)
print("fee per day in pence:", fee_per_day_pence)Code walkthrough
- Until Module 7, dates are shelf days: 1 September is day 1, so L04, borrowed on day 14, is due on day 28.
fee_per_day_penceis long, but nobody will read it as pounds. Clear beats short.
Output
L04 due on day 28
fee per day in pence: 205. Keywords you cannot use as names (Essential)
A few words already have a job in Python's grammar. Use one as a name and Python refuses, so it pays to know the list.
These reserved words are keywords: if, for and while (Module 4), def and return (Module 6), class (Module 9), and True, False and None. Python prints the full list.
help("keywords")Code walkthrough
help(...)is Python's built-in help; given"keywords", it lists them.
Output
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not That is 35 words. Using one as a name fails at once. Run this and you will see:
class = "gothic" File "<string>", line 1
class = "gothic"
^
SyntaxError: invalid syntaxCode walkthrough
- A book's category, so
classseems natural, but it is a keyword.
Use another word: the shared data says genre. It is also why Module 9's Library has return_book, not return. Python 3.12 also has four soft keywords, match, case, type and _, allowed as names but best avoided.
Break it: hide one of Python's own tools
print and type are not keywords, so nothing stops you reusing them. This breaks type for the whole session, so the course does not run it in your notebook. Run this and you will see:
type = "gothic"
print(type(1818))Traceback (most recent call last):
File "<string>", line 2, in <module>
TypeError: 'str' object is not callableCode walkthrough
- Line 1 points
typeat the text"gothic", so the tool can no longer be reached by that name. Line 2 finds text where it expects a tool.
"Not callable" means it cannot be used with brackets like a tool. If it happens in Colab, choose Runtime, then Restart session, which forgets every name.
6. Multiple assignment on one line (Recommended)
Some values belong together: a title, author and year. Python can name them in one line, which keeps related facts side by side.
Put several names on the left of = and as many values on the right, separated by commas. Python pairs them by position. This is unpacking.
title, author, year = "Frankenstein", "Mary Shelley", 1818
print(title)
print(author)
print(year)Code walkthrough
- One line does three assignments; the prints show each name got the value in its position.
Output
Frankenstein
Mary Shelley
1818The counts must match. Three names and two values is an error. Run this and you will see:
title, author, year = "Frankenstein", "Mary Shelley"Traceback (most recent call last):
File "<string>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)Code walkthrough
yearhas no value to pair with.
Amira has two books face out on the counter, Frankenstein left and Dracula right, and wants them swapped. Python reads the whole right side before attaching any name, so left, right = right, left swaps them.
left = "Frankenstein"
right = "Dracula"
left, right = right, left
print(left, "|", right)
late_days = fee_pence = 0
print(late_days, fee_pence)Code walkthrough
- Line 3 reads both values, then moves both names.
- Line 5 is a chained assignment: both names point at the same value,
0.
Output
Dracula | Frankenstein
0 07. Constants by convention (Recommended)
The shelf has four rules that never change: 14 days, 20 pence a day, a 300-pence cap, 3 books. Naming each once, in a way that says "do not change me", beats writing 14 everywhere.
A constant is a variable you promise never to reassign. PEP 8 writes constants in capitals with underscores. These four names are fixed for the whole course.
LOAN_DAYS = 14 # a loan lasts 14 days
FEE_PER_DAY = 20 # late fee, in pence, for each day after the due date
FEE_CAP = 300 # the most one loan can be charged, in pence
MAX_BOOKS = 3 # the most books one member may have out at once
print(LOAN_DAYS, FEE_PER_DAY, FEE_CAP, MAX_BOOKS)Code walkthrough
- Each comment gives the rule's meaning and unit; the capitals tell readers these are rules.
Output
14 20 300 3The capitals are a promise, not a lock:
LOAN_DAYS = 7
print("loan days now", LOAN_DAYS)
LOAN_DAYS = 14
print("loan days back to", LOAN_DAYS)Code walkthrough
- Line 1 breaks the promise with no error. Line 3 puts the rule back.
Output
loan days now 7
loan days back to 14In production
Code is read far more often than it is written, so teams lean on these conventions. Most Python projects follow PEP 8 for names, and many run a linter, a tool that reads code and flags names such as DueDay. Shared rules sit once near the top of a file in capitals, so changing the loan period means changing one line. A type checker can also warn when a constant is reassigned, using type hints (Module 9).
What you added to the Booknook shelf
- Jonah's loan L04 held in six variables:
title,author,year,loan_id,member,borrowed. - The shelf's four rules as constants:
LOAN_DAYS,FEE_PER_DAY,FEE_CAP,MAX_BOOKS. - A habit: snake_case names that say what a value means, and its unit. The module lab turns these into Amira's first shelf card.
Check yourself
- What type is
"20", and what type is20?
"20" is a str, because of the quotation marks. 20 is an int.
- After
a = "Kwame", thenb = a, thena = "Lena", what doesprint(b)show?
Kwame. b was pointed at the value "Kwame"; moving a later does not move b.
- Which of these are legal names:
book2,2book,book_2,book-2,for?
book2 and book_2. 2book starts with a digit, book-2 has a hyphen, and for is a keyword.
Exercise
Priya has loan L24: The Adventures of Sherlock Holmes, by Arthur Conan Doyle, 1892, borrowed on Thursday 1 October 2026. Hold it in variables, with one multiple assignment for title, author and year, and print it. Compare your output with the one below, line for line.
loan_id = "L24"
member = "Priya"
borrowed = "Thursday 1 October 2026"
title, author, year = "The Adventures of Sherlock Holmes", "Arthur Conan Doyle", 1892
print(title, "by", author, year)
print(loan_id, member, borrowed)
print(type(year))Code walkthrough
- Three assignments, one unpacking, and a check that
yearis anint.
Output (yours should match exactly):
The Adventures of Sherlock Holmes by Arthur Conan Doyle 1892
L24 Priya Thursday 1 October 2026
<class 'int'>Now write year as "1892" and rerun: the first line looks the same, the last says <class 'str'>. type shows what printing hides.
Key terms
| Term | Meaning |
|---|---|
| value | A piece of data, such as "Frankenstein" or 1818 |
| type | The kind of value: str, int, float or bool so far |
| variable | A name that points at a value |
| assignment | A line with = that makes a name point at a value |
| reassignment | Pointing an existing name at a new value |
| NameError | The error for a name Python does not know |
| SyntaxError | The error for code Python cannot read at all |
| snake_case | Lower case words joined by underscores, the convention for variable names |
| keyword | A word reserved by Python's grammar, such as class or return; not allowed as a name |
| unpacking | Naming several values in one line: a, b = 1, 2 |
| constant | A variable in capitals that the program promises never to reassign |
| PEP 8 | The official style guide for Python code |
Sources
- Canada, Board of Inquiry into the Air Canada Boeing 767 C-GAUN incident at Gimli, Manitoba, on 23 July 1983, final report, 1985 (no stable official link known; cited by name).
- Guido van Rossum, Barry Warsaw and Alyssa Coghlan, "PEP 8: Style Guide for Python Code", 2001, https://peps.python.org/pep-0008/ (sections "Naming Conventions" and "Constants").
- Python Software Foundation, "The Python Language Reference, 3.12: Lexical analysis, Identifiers and keywords", 2023, https://docs.python.org/3.12/reference/lexical_analysis.html#identifiers
- Python Software Foundation, "Built-in Functions: type and help", Python 3.12 documentation, 2023, https://docs.python.org/3.12/library/functions.html