Cardinality

The number of things in a set is its cardinality.

\[|V| = |\{\text{e, i, o, u, æ, ɑ, ɔ, ə, ɛ, ɪ, ʊ}\}| = 11\]

In Python, we compute the cardinality using len.

vowels: set[str] = {"e", "i", "o", "u", "æ", "ɑ", "ɔ", "ə", "ɛ", "ɪ", "ʊ"}

print(f"|V| = {len(vowels)}")
|V| = 11

Sets can have infinite cardinality. For instance, the set of natural numbers is infinite.

\[\mathbb{N} = \{0, 1, 2, 3, \ldots\}\text{ (or }\{1, 2, 3, \ldots\})\]

We unfortunately can’t use set to work with infinite sets in Python. Instead, we have to use generators. One way to initialize a generator is to define a function containing a yield statement.

from collections.abc import Generator

def natural_numbers() -> int:
    """Initialize a generator for the natural numbers"""
    i = 0
    while True:
        yield i
        i += 1

# initialize a generator of the natural numbers
N: Generator[int] = natural_numbers()

# print the first 10 natural numbers
for i in N:
  if i < 10:
    print(i)
  else:
    break
0
1
2
3
4
5
6
7
8
9