Lecture 9 - CS50's Introduction to Programming With Python
Lecture 9 - CS50's Introduction to Programming With Python
OpenCourseWare
Donate (https://cs50.harvard.edu/donate)
Lecture 9
Et Cetera
set
Global Variables
Constants
Type Hints
Docstrings
argparse
Unpacking
args and kwargs
map
List Comprehensions
filter
Dictionary Comprehensions
enumerate
Generators and Iterators
Congratulations!
This was CS50!
Et Cetera
Over the many past lessons, we have covered so much related to Python!
In this lesson, we will be focusing upon many of the “et cetera” items not previously
discussed. “Et cetera” literally means “and the rest”!
Indeed, if you look at the Python documentation, you will find quite “the rest” of other
features.
set
students = [
{"name": "Hermione", "house": "Gryffindor"},
{"name": "Harry", "house": "Gryffindor"},
{"name": "Ron", "house": "Gryffindor"},
{"name": "Draco", "house": "Slytherin"},
{"name": "Padma", "house": "Ravenclaw"},
]
houses = []
for student in students:
if student["house"] not in houses:
houses.append(student["house"])
Notice how we have a list of dictionaries, each being a student. An empty list called
houses is created. We iterate through each student in students . If a student’s house is
not in houses , we append to our list of houses .
It turns out we can use the built-in set features to eliminate duplicates.
In the text editor window, code as follows:
students = [
{"name": "Hermione", "house": "Gryffindor"},
{"name": "Harry", "house": "Gryffindor"},
{"name": "Ron", "house": "Gryffindor"},
{"name": "Draco", "house": "Slytherin"},
{"name": "Padma", "house": "Ravenclaw"},
]
houses = set()
for student in students:
houses.add(student["house"])
Notice how no checking needs to be included to ensure there are no duplicates. The set
object takes care of this for us automatically.
You can learn more in Python’s documentation of set
(https://docs.python.org/3/library/stdtypes.html#set).
Global Variables
In other programming languages, there is the notion of global variables that are
accessible to any function.
We can leverage this ability within Python. In the text editor window, code as follows:
balance = 0
def main():
print("Balance:", balance)
if __name__ == "__main__":
main()
Notice how we create a global variable called balance , outside of any function.
Since no errors are presented by executing the code above, you’d think all is well.
However, it is not! In the text editor window, code as follows:
balance = 0
def main():
print("Balance:", balance)
deposit(100)
withdraw(50)
print("Balance:", balance)
def deposit(n):
balance += n
def withdraw(n):
balance -= n
if __name__ == "__main__":
main()
Notice how we now add the functionality to add and withdraw funds to and from
balance . However, executing this code, we are presented with an error! We see an error
called UnboundLocalError . You might be able to guess that, at least in the way we’ve
currently coded balance and our deposit and withdraw functions, we can’t reassign it
a value value inside a function.
To interact with a global variable inside a function, the solution is to use the global
keyword. In the text editor window, code as follows:
balance = 0
def main():
print("Balance:", balance)
deposit(100)
withdraw(50)
print("Balance:", balance)
def deposit(n):
global balance
balance += n
def withdraw(n):
global balance
balance -= n
if __name__ == "__main__":
main()
Notice how the global keyword tells each function that balance does not refer to a
local variable: instead, it refers to the global variable we originally placed at the top of
our code. Now, our code functions!
Utilizing our powers from our experience with object-oriented programming, we can
modify our code to use a class instead of a global variable. In the text editor window, code
as follows:
class Account:
def __init__(self):
self._balance = 0
@property
def balance(self):
return self._balance
def main():
account = Account()
print("Balance:", account.balance)
account.deposit(100)
account.withdraw(50)
print("Balance:", account.balance)
if __name__ == "__main__":
main()
Notice how we use account = Account() to create an account. Classes allow us to solve
this issue of needing a global variable more cleanly because these instance variables are
accessible to all the methods of this class utilizing self .
Generally speaking, global variables should be used quite sparingly, if at all!
Constants
Some languages allow you to create variables that are unchangeable, called “constants”.
Constants allow one to program defensively and reduce the opportunities for important
values to be altered.
In the text editor window, code as follows:
MEOWS = 3
for _ in range(MEOWS):
print("meow")
Notice MEOWS is our constant in this case. Constants are typically denoted by capital
variable names and are placed at the top of our code. Though this looks like a constant, in
reality, Python actually has no mechanism to prevent us from changing that value within
our code! Instead, you’re on the honor system: if a variable name is written in all caps, just
don’t change it!
One can create a class “constant”, now in quotes because we know Python doesn’t quite
support “constants”. In the text editor window, code as follows:
class Cat:
MEOWS = 3
def meow(self):
for _ in range(Cat.MEOWS):
print("meow")
cat = Cat()
cat.meow()
Because MEOWS is defined outside of any particular class method, all of them have access
to that value via Cat.MEOWS .
Type Hints
In other programming languages, one expresses explicitly what variable type you want to
use.
As we saw earlier in the course, Python does not require the explicit declaration of types.
Nevertheless, it’s good practice need to ensure all of your variables are of the right type.
mypy is a program that can help you test to make sure all your variables are of the right
type.
You can install mypy by executing in your terminal window: pip install mypy .
def meow(n):
for _ in range(n):
print("meow")
You may already see that number = input("Number: )" returns a string , not an int . But
meow will likely want an int !
A type hint can be added to give Python a hint of what type of variable meow should
expect. In the text editor window, code as follows:
Notice how the meow function has only a side effect. Because we only attempt to print
“meow”, not return a value, an error is thrown when we try to store the return value of
meow in meows .
We can further use type hints to check for errors, this time annotating the return values of
functions. In the text editor window, code as follows:
Notice how the notation -> None tells mypy that there is no return value.
We can modify our code to return a string if we wish:
Notice how we store in meows multiple str s. Running mypy produces no errors.
You can learn more in Python’s documentation of Type Hints
(https://docs.python.org/3/library/typing.html).
You can learn more about mypy (https://mypy.readthedocs.io/) through the program’s own
documentation.
Docstrings
A standard way of commenting your function’s purpose is to use a docstring. In the text
editor window, code as follows:
def meow(n):
"""Meow n times."""
return "meow\n" * n
Notice how the three double quotes designate what the function does.
You can use docstrings to standardize how you document the features of a function. In the
text editor window, code as follows: s
def meow(n):
"""
Meow n times.
Notice how multiple docstring arguments are included. For example, it describes the
parameters taken by the function and what is returned by the function.
Established tools, such as Sphinx (https://www.sphinx-doc.org/en/master/index.html), can
be used to parse docstrings and automatically create documentation for us in the form of
web pages and PDF files such that you can publish and share with others.
You can learn more in Python’s documentation of docstrings (https://peps.python.org/pep-
0257/).
argparse
Suppose we want to use command-line arguments in our program. In the text editor
window, code as follows:
import sys
if len(sys.argv) == 1:
print("meow")
elif len(sys.argv) == 3 and sys.argv[1] == "-n":
n = int(sys.argv[2])
for _ in range(n):
print("meow")
else:
print("usage: meows.py [-n NUMBER]")
Notice how sys is imported, from which we get access to sys.argv —an array of
command-line arguments given to our program when run. We can use several if
statements to check whether the use has run our program properly.
Let’s assume that this program will be getting much more complicated. How could we
check all the arguments that could be inserted by the user? We might give up if we have
more than a few command-line arguments!
Luckily, argparse is a library that handles all the parsing of complicated strings of
command-line arguments. In the text editor window, code as follows:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-n")
args = parser.parse_args()
for _ in range(int(args.n)):
print("meow")
Notice how argparse is imported instead of sys . An object called parser is created
from an ArgumentParser class. That class’s add_argument method is used to tell
argparse what arguments we should expect from the user when they run our program.
Finally, running the parser’s parse_args method ensures that all of the arguments have
been included properly by the user.
We can also program more cleanly, such that our user can get some information about the
proper usage of our code when they fail to use the program correctly. In the text editor
window, code as follows:
import argparse
for _ in range(int(args.n)):
print("meow")
Notice how the user is provided some documentation. Specifically, a help argument is
provided. Now, if the user executes python meows.py --help or -h , the user will be
presented with some clues about how to use this program.
We can further improve this program. In the text editor window, code as follows:
import argparse
Notice how not only is help documentation included, but you can provide a default
value when no arguments are provided by the user.
You can learn more in Python’s documentation of argparse
(https://docs.python.org/3/library/argparse.html).
Unpacking
Would it not be nice to be able to split a single variable into two variables? In the text
editor window, code as follows:
Notice how this program tries to get a user’s first name by naively splitting on a single
space.
It turns out there are other ways to unpack variables. You can write more powerful and
elegant code by understanding how to unpack variables in seemingly more advanced
ways. In the text editor window, code as follows:
Notice how a list called coins is created. We can pass each value in by indexing using 0 ,
1 , and so on.
This is getting quite verbose. Wouldn’t it be nice if we could simply pass the list of coins
to our function?
To enable the possibility of passing the entire list, we can use unpacking. In the text
editor window, code as follows:
print(total(*coins), "Knuts")
Notice how a * unpacks the sequence of the list of coins and passes in each of its
individual elements to total .
Suppose that we could pass in the names of the currency in any order? In the text editor
window, code as follows:
Notice how a dictionary called coins is provided. We can index into it using keys, such as
“galleons” or “sickles”.
Since the total function expects three arguments, we cannot pass in a dictionary. We
can use unpacking to help with this. In the text editor window, code as follows:
print(total(**coins), "Knuts")
Notice how the named values are provided in the form of a dictionary.
Thinking about the print function above, you can see how *objects takes any number
of positional arguments.
You can learn more in Python’s documentation of print
(https://docs.python.org/3/library/functions.html#print).
map
def main():
yell("This is CS50")
def yell(word):
print(word.upper())
if __name__ == "__main__":
main()
def main():
yell(["This", "is", "CS50"])
def yell(words):
uppercased = []
for word in words:
uppercased.append(word.upper())
print(*uppercased)
if __name__ == "__main__":
main()
Notice we accumulate the uppercase words, iterating over each of the words and
uppercasing them. The uppercase list is printed utilizing the * to unpack it.
Removing the brackets, we can pass the words in as arguments. In the text editor window,
code as follows:
def main():
yell("This", "is", "CS50")
def yell(*words):
uppercased = []
for word in words:
uppercased.append(word.upper())
print(*uppercased)
if __name__ == "__main__":
main()
Notice how *words allows for many arguments to be taken by the function.
map allows you to map a function to a sequence of values. In practice, we can code as
follows:
def main():
yell("This", "is", "CS50")
def yell(*words):
uppercased = map(str.upper, words)
print(*uppercased)
if __name__ == "__main__":
main()
Notice how map takes two arguments. First, it takes a function we want applied to every
element of a list. Second, it takes that list itself, to which we’ll apply the aforementioned
function. Hence, all words in words will be handed to the str.upper function and
returned to uppercased .
You can learn more in Python’s documentation of map
(https://docs.python.org/3/library/functions.html#map).
List Comprehensions
List comprehensions allow you to create a list on the fly in one elegant one-liner.
We can implement this in our code as follows:
def main():
yell("This", "is", "CS50")
def yell(*words):
uppercased = [arg.upper() for arg in words]
print(*uppercased)
if __name__ == "__main__":
main()
Notice how instead of using map , we write a Python expression within square brackets.
For each argument, .upper is applied to it.
Taking this concept further, let’s pivot toward another program.
In the text editor window, type code gryffindors.py and code as follows:
students = [
{"name": "Hermione", "house": "Gryffindor"},
{"name": "Harry", "house": "Gryffindor"},
{"name": "Ron", "house": "Gryffindor"},
{"name": "Draco", "house": "Slytherin"},
]
gryffindors = []
for student in students:
if student["house"] == "Gryffindor":
gryffindors.append(student["name"])
Notice we have a conditional while we’re creating our list. If the student’s house is
Gryffindor, we append the student to the list of names. Finally, we print all the names.
More elegantly, we can simplify this code with a list comprehension as follows:
students = [
{"name": "Hermione", "house": "Gryffindor"},
{"name": "Harry", "house": "Gryffindor"},
{"name": "Ron", "house": "Gryffindor"},
{"name": "Draco", "house": "Slytherin"},
]
gryffindors = [
student["name"] for student in students if student["house"] == "Gryffindor
]
filter
Using Python’s filter function allows us to return a subset of a sequence for which a
certain condition is true.
In the text editor window, code as follows:
students = [
{"name": "Hermione", "house": "Gryffindor"},
{"name": "Harry", "house": "Gryffindor"},
{"name": "Ron", "house": "Gryffindor"},
{"name": "Draco", "house": "Slytherin"},
]
def is_gryffindor(s):
return s["house"] == "Gryffindor"
Notice how a function called is_gryffindor is created. This is our filtering function that
will take a student s , and return True or False depending on whether the student’s
house is Gryffindor. You can see the new filter function takes two arguments. First, it
takes the function that will be applied to each element in a sequence—in this case,
is_gryffindor . Second, it takes the sequence to which it will apply the filtering function
—in this case, students . In gryffindors , we should see only those students who are in
Gryffindor.
filter can also use lambda functions as follows:
students = [
{"name": "Hermione", "house": "Gryffindor"},
{"name": "Harry", "house": "Gryffindor"},
{"name": "Ron", "house": "Gryffindor"},
{"name": "Draco", "house": "Slytherin"},
]
Dictionary Comprehensions
We can apply the same idea behind list comprehensions to dictionaries. In the text editor
window, code as follows:
gryffindors = []
print(gryffindors)
Notice how this code doesn’t (yet!) use any comprehensions. Instead, it follows the same
paradigms we have seen before.
We can now apply dictionary comprehensions by modifying our code as follows:
print(gryffindors)
Notice how all the prior code is simplified into a single line where the structure of the
dictionary is provided for each student in students .
We can even simplify further as follows:
print(gryffindors)
We may wish to provide some ranking of each student. In the text editor window, code as
follows:
for i in range(len(students)):
print(i + 1, students[i])
Notice how enumerate presents the index and the value of each student .
You can learn more in Python’s documentation of enumerate
(https://docs.python.org/3/library/functions.html#enumerate).
n = int(input("What's n? "))
for i in range(n):
print(" " * i)
Notice how this program will count the number of sheep you ask of it.
We can make our program more sophisticated by adding a main function by coding as
follows:
def main():
n = int(input("What's n? "))
for i in range(n):
print(" " * i)
if __name__ == "__main__":
main()
def main():
n = int(input("What's n? "))
for i in range(n):
print(sheep(i))
def sheep(n):
return " " * n
if __name__ == "__main__":
main()
def main():
n = int(input("What's n? "))
for s in sheep(n):
print(s)
def sheep(n):
flock = []
for i in range(n):
flock.append(" " * i)
return flock
if __name__ == "__main__":
main()
def main():
n = int(input("What's n? "))
for s in sheep(n):
print(s)
def sheep(n):
for i in range(n):
yield " " * i
if __name__ == "__main__":
main()
Notice how yield provides only one value at a time while the for loop keeps working.
You can learn more in Python’s documentation of generators
(https://docs.python.org/3/howto/functional.html#generators).
You can learn more in Python’s documentation of iterators
(https://docs.python.org/3/howto/functional.html#iterators).
Congratulations!
As you exit from this course, you have more of a mental model and toolbox to address
programming-related problems.
First, you learned about functions and variables.
Second, you learned about conditionals.
Third, you learned about loops.
Fourth, you learned about exceptions.
Fifth, you learned about libraries.
Sixth, you learned about unit tests.
Seventh, you learned about file I/O.
Eighth, you learned about regular expressions.
Most recently, you learned about object-oriented programming.
Today, you learned about many other tools you can use.
import cowsay
import pyttsx3
engine = pyttsx3.init()
this = input("What's this? ")
cowsay.cow(this)
engine.say(this)
engine.runAndWait()
Notice how running this program provides you with a spirited send-off.
Our great hope is that you will use what you learned in this course to address real
problems in the world, making our globe a better place.
This was CS50!