liveBook · Manning

Ordinal data

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

An ordinal value is a representation that identifies the position of an item in an ordered sequence, such as first or second.

Ordinal data represents an item's position in an ordered sequence: 1 becomes first, 2 becomes second, and 3 becomes third. In Python, a list or dictionary can map numeric positions to ordinal strings. section 13.1.2 section 13.3.1

Lists use zero-based indexes, so a one-based day requires ordinal[day - 1]. A dictionary keyed by day numbers avoids that adjustment; either representation can support a reusable verse(day) function. section 13.3.1 section 13.3.1

Mapping days to ordinal strings

ordinal = ['first', 'second', 'third'] ordinal[3 - 1] # 'third' The same lookup can use {1: 'first', 2: 'second', 3: 'third'}. Tests can verify that verse(1) and verse(2) produce the expected opening lines. section 13.3.1 section 13.1.2

Do not confuse ordinal position with Python's ord(): ord() returns a character's numeric value in the ASCII table, while chr() converts that value back to a character. section 18.1.2

>>> day = 3
>>> ordinal[day - 1]
'third'

Inspect the one-based day to zero-based list-index conversion that retrieves 'third'. section 13.3.1

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains list and dictionary representations, indexing adjustments, tests, and the distinction from Python's ord() function.
Examples
Demonstrates day names, verse generation, pytest assertions, ASCII character values, and character-range exploration.
What it assumes
Assumes Python lists, dictionaries, functions, indexing, and tests while teaching the one-based versus zero-based distinction explicitly.
What you can do after
Apply it via Python by mapping positions to labels, handling indexing, testing lookups, and converting characters with ord() and chr().
How it connects
Connects ordinal values with lists, dictionaries, zero-based indexing, functions, testing, ASCII, ord(), and chr().

Also explained in

Compare how 8 books explain ordinal data →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.