Harvard CS50P 2022 Solutions
An introduction to programming using a language called Python. Learn how to read and write code as well as how to test and “debug” it. Designed for students with or without prior programming experience who’d like to learn Python specifically. Learn about functions, arguments, and return values (oh my!); variables and types; conditionals and Boolean expressions; and loops. Learn how to handle exceptions, find and fix bugs, and write unit tests; use third-party libraries; validate and extract data with regular expressions; model real-world entities with classes, objects, methods, and properties; and read and write files. Hands-on opportunities for lots of practice. Exercises inspired by real-world programming problems. No software required except for a web browser, or you can write code on your own PC or Mac.
Screenshots 🖼️
Week 0 - Functions, Variables 🧩
Functions. Arguments. Side Effects. Bugs. Return Values. Variables. Comments. Pseudocode. str. Parameters. int. Operators. Interactive Mode. float. def. Scope. return.
Week 1 - Conditionals 🚥
if. elif. else. or. and. bool. match.
Week 2 - Loops ➿
while. for. list. range. continue. break. list. len. dict. None.
Week 3 - Exceptions ⛔
SyntaxError. ValueError. try. except. NameError. else. pass. raise.
Week 4 - Libraries 📖
Modules. random. import. from. statistics. Command-Line Arguments. sys. sys.argv. IndexError. sys.exit. Slices. Packages. PyPI. pip. cowsay. APIs. requests. JSON. name.
Week 5 - Unit Tests 🧨
assert. AssertionError. pytest. Packages. init.py.
Week 6 - File I/O 📂
list. open. with. sorted. CSV. dict. csv. PIL.
Week 7 - Regular Expressions 🎭
Regular Expressions. Regexes. re. re.search. re.match. re.fullmatch. re.sub. re.split. re.findall.
Week 8 - Object-Oriented Programming 💻
tuple. Classes. class. Objects. Attributes. Instance Variables. Methods. Instance Methods. init. raise. str. Properties. @property. Decorators. int. str. str.lower. str.strip. list. list.append. dict. Class Methods. @classmethod. Static Methods. @staticmethod. Inheritance. BaseException. Operator Overloading. object.add.
Week 9 - Et Cetera ♾️
docs.python.org. set. global. Constants. Type Hints. mypy. Docstrings. peps.python.org. argparse. Unpacking. *args. **kwargs. print. map. List Comprehensions. filter. Dictionary Comprehensions. enumerate. Generators. yield. Iterators.
Final Project 📃
Once you have solved each of the course’s problem sets, it’s time to implement your final project, a Python program of your very own! The design and implementation of your project is entirely up to you, albeit subject to these requirements:
- Your project must be implemented in Python.
- Your project must have a main function and three or more additional functions. At least three of those additional functions must be accompanied by tests that can be executed with pytest.
- Your main function must be in a file called project.py, which should be in the “root” (i.e., top-level folder) of your project.
- Your 3 required custom functions other than main must also be in project.py and defined at the same indentation level as main (i.e., not nested under any classes or functions).
- Your test functions must be in a file called test_project.py, which should also be in the “root” of your project. Be sure they have the same name as your custom functions, prepended with test_ (test_custom_function, for example, where custom_function is a function you’ve implemented in project.py).
- You are welcome to implement additional classes and functions as you see fit beyond the minimum requirement.
- Implementing your project should entail more time and effort than is required by each of the course’s problem sets.
- Any pip-installable libraries that your project requires must be listed, one per line, in a file called requirements.txt in the root of your project.
CS50 Certificate 📑
License 📝
A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.