The new match
Boss Statement
Here is the program we built in class.
n = input("Enter an integer: ")
n = int(n)
match n:
case 1:
print("n = 1")
case 2:
print("n = 2")
case 3:
print("n = 3")
case 4|5|6: #here we are "orring
print("mighty big number")
case _: #this is the default.
print("Try a number 1-6, Bub.")
Modules and the Python Standard Library
What is a module? It's a Python program. Many modules
have no main routine. They are collections of related functions residing
in a file. We have seen this with the math
module.
Where can I see all of the Python modules? They can be
found in the Global
Module Index in the Python documentation. These modules can do a
vast array of tasks for you. You should look up the math
module
and see its contents.
We begin by learning about the module sys
and its
object sys.argv
, which returns a list of strings
that the user types after typing "python". This is the program
mystery.py
that we created.
import sys
print(sys.argv)
print(type(sys.argv))
Running this reveals that sys.argv
is a list of
strings. Run this by typing several items after python mystery.py
These items are called command-line arguments They allow you to
inject information into a program right at the beginnning so that
no input
statement is needed.
Can I make my own module? Yes. We will see how to do this the smart way.