set[str] = {"e", "i", "o", "u", "æ", "ɑ", "ɔ", "ə", "ɛ", "ɪ", "ʊ"}
vowels:
print(f"|V| = {len(vowels)}")
|V| = 11
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
.
set[str] = {"e", "i", "o", "u", "æ", "ɑ", "ɔ", "ə", "ɛ", "ɪ", "ʊ"}
vowels:
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"""
= 0
i while True:
yield i
+= 1
i
# initialize a generator of the natural numbers
int] = natural_numbers()
N: Generator[
# 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