Python Arena
Step 3 of 3

What a module contains

How do you find out what's in a module? There are two ways: read its documentation, or ask Python itself.

The documentation

Every module of the standard library has a page in Python's official documentation. The list of all of them, grouped by theme, is at docs.python.org/3/library, and the math module's page is docs.python.org/3/library/math.html. Each function has a short description, and often an example. Looking things up there is part of every programmer's day, so it's worth getting used to it early.

dir

In part 4 you used dir("") to list the methods of strings. Given a module, dir lists the names the module contains. The list for math is long, so this session looks at its end, and checks for one name:

In the Python interpreter
>>> import math
>>> names = dir(math)
>>> names[-7:]
['sqrt', 'sumprod', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
>>> "floor" in names
True

The list is in alphabetical order. It starts with names wrapped in double underscores, such as __name__, which Python uses for its own bookkeeping; you can skip those. dir shows only the names, so it's a quick reminder of how something was spelled. The documentation tells what each one does.

Modules hold values too

Not everything in a module is a function. The string module holds handy strings of characters:

import string

print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.digits)
print(string.punctuation)
Output
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

(string.ascii_letters has both the small and the capital letters.) These save typing, and typos: a hand-typed alphabet with one letter missing is a bug that hides well. Together with in they make it easy to sort characters into groups:

import string

code = "AB-12"
digits = 0
for character in code:
    if character in string.digits:
        digits += 1
print("Digits:", digits)
Output
Digits: 2

Note the difference between the module string and the type of text values, str: they're two different things with similar names.

Your task

Challenge 1 of 3

What does this program print? Type each line it prints.

Challenge 2

A website accepts a username only if it is 3 to 12 characters long and has nothing but small letters a to z, digits and underscores (_). Write a function valid_username(name) that returns True for a valid username and False otherwise. Use the strings of the string module instead of typing the letters and digits yourself.

valid_username("ada_99") returns True, and valid_username("Ada") returns False.

Challenge 3

Reading the documentation of math turns up math.comb(n, k): the number of different ways to choose k things out of n, when the order doesn't matter. A chess club with 12 members that sends 4 players to a tournament can pick its team in math.comb(12, 4) different ways. (Ada, Ben, Cy and Di are the same team as Di, Cy, Ben and Ada.)

Write a program that asks how many members the club has and how many players go to the tournament, and prints how many different teams there are. For example:

Members in the club: 12
Players in a team: 4
Different teams: 495