Watching videos can give you an overview, but you only learn to code by writing, running, and debugging your own programs. Here are some exercises for each of the videos so that you can practise what you've learned:
- Make sure you have Python installed!
- Change what you print: what happens when you use numbers or change the text?
- What happens when you change the order of input and output?
- What happens if you remove the text in the brackets?
- How can you tell whether the output of a calculation will be an integer, float (decimal) or boolean (true/false)?
//
returns an integer when diving two other integers. What happens when the inputs are floats?"a" + "b" -> "ab"
. Which of the other operations work on strings?- What happens when you try to add a number and a string?
- What happens when you try to add an integer and a float?
- Another argument to
print
isend
. What happens when you change the default value ofend
? - What happens when you try to divide by zero?
- How does the program change if we use
int
to convert the input to an integer instead offloat
? - Which lines will still run if we remove the
float
from the input and leave the inputs as strings?
- Can you write a function to make coffee?
- Can you write a function that calls itself?
- What happens if you have two parameters with the same name? What about if you change which values have defaults and which don't?
- What happens if you have two functions with the same name but different arguments?
- What happens if you create a variable with the same name as a function?
- Try rewriting the calculator from lesson 4 to use an
if
statement in the input to only perform a single calculation, specified by the user. match
statements are new to Python. See if you can use them for the task above.
- You can put anything in a list, including other lists. Try making a list of lists.
- What happens if you try to access an item that doesn't exist in a list?
- We only ever assign to values in the list. Can you create a variable that contains one of the values of the list?
- Define a list
importantItems2 = importantItems
. What happens when you changeimportantItems2
? How does this differ from changingimportantItems
? - You can get around this by using
importantItems2 = list(importantItems)
orimportantItems2 = importantItems.copy()
. Does this change the behaviour?
- Python lists support some other methods. Try using some of them to change the list.
- You can convert from a list of strings to a single string using
"<sep>".join(list)
. Try using this to print the list in a more readable format. - Can you use the join function to display a list of strings without the quotes (
["a", "b", "c"] -> [a, b, c]
)? - What happens when you append a list to itself?
- Use your imagination to see what you can draw!
- Write a method called
square
that draws a square of a given size. - Write a method called
hexagon
that draws a hexagon of a given size.
- Write a method called
polygon
that draws a polygon of a given size and number of sides. Try using the line method from lesson 9 instead of forward and right. - Try doing this without calling
turtle.radians()
. - How can you draw a circle with the polygon methods?
- Can you rewrite this without the state variable (just checking conditions based on min and max)?
- Can you rewrite a
for
loop just using awhile
loop? - Using the
time
module, can you write a less efficient version oftime.sleep()
using awhile
loop?
- The
random
module has a lot of other functions. Try playing around with some of them. - These numbers are actually pseudorandom and you can fix the seed to get the same sequence of numbers each time. Try using
random.seed()
to fix the seed and see what happens. - Can you simulate how many times it takes to get a 6 when rolling a dice? Repeat this 1000 times and see what the average is.
- Try implementing a board game, such as ludo or snakes and ladders. Try different strategies and see which is most effective.
- Try implementing a card game, such as blackjack or poker. Can you write a program that performs better than a random opponent or a human?
- Can you write a program that asks the user for a capital city and tells them the country?
- You can do a lot more with dictionaries. Try playing around with some of the other methods.
- What values are valid for the keys of dictionaries?
- Create a function that takes in a person's data (name, age, height, etc.) and tells you which information is missing and which information is unnecessary.
- The following code finds Fibonacci numbers but computes the solution to some values multiple times:
def fib(n):
if n <= 1:
return 1
elif n % 2 == 0:
return fib(n//2) ** 2 + fib(n//2-1) ** 2
else:
return fib(n//2) * (fib(n//2+1) + fib(n//2-1))
Can you use a dictionary to store intermediate results and avoid recomputing the same value twice?
- What data can you store in JSON?
- How does JSON differ from Python dictionaries?
- Have a look at YAML. How does it differ from JSON?
- Have a look at the
slice
function. What are the advantages of using this over the slice notation? - How are
slice
andrange
related?
- How can you use
json.dump
, which is very similar tojson.dumps
? - What happens if you try to open a file that doesn't exist? What about writing to a file that doesn't exist? What about writing to a closed file?
pickle
is a module that can be used to save Python objects to a file. Try using it to save a list of dictionaries. What are the advantages/disadvantages of usingpickle
overjson
?- Write a program that can store birthdays. You should be able to add, edit, remove and see upcoming birthdays.
- Python Turtle also has a
Screen
class. Try experimenting with some of these methods. - You can move each turtle individually. Try making a program that draws a square with each turtle moving at a different speed.
- Try to draw a star where each turtle draws one line and they start and finish at the same time.
- Try creating a class that represents a person. Save it using
pickle
and load it again. - Create classes for different animals. Give each animal a method that makes a noise.
- Create a class that represents a deck of cards. Give it a method that shuffles the deck. Give it a method that deals a card.
- Try doing the previous exercise where each card is an object.
- Try writing a function that takes only
**kwargs
and checks that the keys are valid. - What happens when you change the order of the parameters? Can you still use
myfunc(foo=bar)
to make sure that the parameters are assigned correctly? - What are the limits on what you can use in a
lambda
function? - Python has a
functools
module that contains some useful functions. Try using some of them and writing your own versions of some of them. - The Python module
inspect
has aSignature
class. Try using this to check that the parameters of a function are valid.
- Create a class and define a function
__repr__
that returns a string. Callstr()
on the object. What happens? What happens if you define a function__str__
instead? - A new feature in Python is f-strings. Try using these instead of
format
. - It can be useful for filenames to have a fixed number of digits, filled in with leading zeros. Try writing a function to do this and have a look at how you can do this with string formatting directly.
- Try writing a pretty-print function for a dictionary. Make sure that it works for nested dictionaries and on a limited screen width.
- Try creating a class that inherits from
dict
and overrides the__getitem__
method to return a default value if the key doesn't exist. - What happens if you try to inherit from multiple classes? What about if you try to inherit from a class that inherits from another class? Can you come up with examples that break Python's method resolution order?
- Try creating a class that inherits from
list
and overrides the__add__
method to add the elements of the lists together instead of concatenating them. - Create classes for different chess pieces and a class for the board. Give each piece a method that returns the squares it can move to and simulate a random chess game.
- It's now common to use an Ellipsis (
...
) instead ofpass
in a function that hasn't been implemented yet. How does this differ frompass
? - Some other interesting statements are
assert
,raise
anddelete
. Try using these in your code.
- How much faster/slower are sets than lists? Benchmark this using the
timeit
module. - Try writing your own version of a set where the elements are stored in order. What are the main difficulties you encounter?
importlib
offers a lot more functionality than theimport
statement. Try using some of the other functions.- Python has a lot of conventions around how you should import modules. Find a convention that you find appealing and try to stick to it.
- Try writing a module that imports itself. What happens?
- Can you write two modules that import each other without an
ImportError
?
- Before exceptions, it was common to return a special value to indicate an error (such as -1). What are the advantages/disadvantages of this approach?
- Create an
ArithmeticError
and add this to your calculator from lesson 4. Subclass your error with the different types of errors that can occur when doing arithmetic. - When doing internal testing, it is common to use
assert
statements to generate errors. Try using these in your code.
- Create a menu utility that gets the user to select options by inputting numbers. At each step, handle incorrect inputs so the user tries only the previous step again.
- Modify the menu code so that after 3 incorrect inputs, the program exits.
- Write a program that can read a file and the user can add lines. If the user enters a blank line, the program should save and exit. If the user presses
Ctrl+C
, the program should save the file and exit. What other errors can occur and how should they be handled?
- As well as global, there is a
nonlocal
variable. Try using this in your code. - Define a function called
adder
that returns a function that adds a number to the input:
>>> plus_2 = adder(2)
>>> plus_2(3)
5
How can you change the names of the variables to avoid conflicts? 3. Why does the following code not work?
def counter(f):
count = 0
def wrapper(*args, **kwargs):
count += 1
print(count)
return f(*args, **kwargs)
return wrapper
How can you fix it?
>>> print_counter = counter(print)
>>> print_counter("Hello")
1
Hello
>>> print_counter("World")
2
World
- Python has lots of other magic methods and some interesting attributes. Try using some of them.
- Write a
Matrix
class that can add and multiply matrices. You may also want to support other operators, such as__repr__
and__getitem__
. - Write a
Vector
class that is compatible with theMatrix
class.
- You already wrote a
counter
decorator in lesson 27. Try writing some other decorators. - The
functools
module has a lot of useful decorators. Try using some of them. - Try writing your own code for some of the decorators in
functools
.
- One decorator that I didn't cover is the
classmethod
decorator. Try using this in your code. What happens when you inherit from a class with a class method? - Properties also support setters and deleters. Define an
age
property based on their birthday that allows setting and deleting via modifying their birthday.
- How much jitter is there in the Python
sleep
method? (If I runsleep(1)
1000 times what are the minimum and maximum times?) - Write an extension of
timedelta
that takes in months.
- What happens when you relead a file after changing the class definition? What about if you change the module name?
- Write a class that can be pickled and unpickled. Define different versions so that when you change the class, you can still load the old version.
- Try using the
dill
module instead ofpickle
. What are the advantages/disadvantages?
- Python ranges must have an end value. Write a class that accepts any value (including floats and infinity) and can be used as a range.
itertools
is a module that contains a lot of useful functions, try writing your own versions for some of them using generators.- List comprehensions don't just apply to lists. Try using them with a dictionary to make all the keys lowercase or a set to keep only even values.
- try calling help on a function with a multiline comment (docstring). what happens?
- multiline definitions are rarely used in practice. instead, you tend to use brackets. Try using the
black
formatter to see how it handles long lines.
- Python stores negative integers in two's complement. Try making a number positive/negative by only flipping the bits.
- Gray Code is a way of representing numbers where only one bit changes between each number. Try writing a function that converts between Gray Code and natural numbers.
- Convert the infinite range class from lesson 33 into an iterator.
- Write a class that can be used as an iterator but also supports indexing.
- Write an iterator that supports nested iteration of powers of 10:
iterator = NestedTens()
for i in iterator:
for j in iterator:
print(i, j)
# 0, 0
# 0, 10
# 0, 20
# ...
# 0, 90
# 1, 0
# 1, 10
# ...
- Without using loops, write code to generate a random integer between 0 and N-1 weighted by the square of the index.
- Scale all numbers in an array so that the min is 0 and the max is 1.
- Write a function that takes in a list of numbers and returns the index of the number closest to the mean.
- Write a function that takes in a list of numbers and returns the index of the number closest to the median (try to do this without the builtin
numpy.median
function). - Using NumPy, create a histogram of the numbers in a list (the index is the number and the value is the number of times it appears in the list):
>>> histogram([1, 2, 4, 1, 2, 4, 0, 1, 1])
[1, 4, 2, 0, 2]
- Create two spreadsheets that are similar but one has a few changes. Use pandas to find the differences between the two.
- Take a flat-file spreadsheet and use pandas to normalise the data.
- Download a dataset from the internet and use pandas to analyse it.
- Remove anomalies from the dataset using pandas.
- Try to make some of the charts look beautiful.
- Using the dataset from lesson 37, create some charts that show interesting information.
- Pandas has a
plot
method that can be used to plot data. Try creating some of these charts using matplotlib instead.
- Modify the calculator from lesson 4 to take in arguments from the command line.
- Write a program that reads data from an input file, performs some calculations and writes the results to an output file. Use the argparse module to specify the input, output files and parameters for the calculations.
- Create a program to encode/encrypt and decode/decrypt a file. Use argparse for the input and output and give the user multiple modes (sub-commands) to choose from.
- Modify the program from lesson 26 to use argparse instead of input. Try to make it as user-friendly as possible.
- Use argparse to verify the arguments of a program and print a help message if they are invalid.
- Modify the programs from lesson 39 to use a GUI instead of the command line.
- Try using the widgets to make specific types of input easier.
- Try using the progress bar while the program is running.
- Start from an arbitrary website and try to find links off the page. Repeat this on the links you find and see if you can find a page that is frequently linked to.
- Find all the pages on a website and count the number of times each word is used across the site.
- Find all the pages on a website and count the number of times each image is used across the site.
- Create a scraper to automatically convert a website from HTML into Markdown.
- Start on an arbitrary Wikipedia page and try to keep clicking on links to get to a random page.
- Think of a task that you do routinely on the internet and try to automate it.
- Try to book flights or order shopping with a bot.
- Log into a social media account and download all the images and text to a local folder.
- Try to find the roots of a polynomial using the
scipy.optimize
module. - Integrate a complicated function using the
scipy.integrate
module. - Use the
scipy.stats
module to generate random numbers from a distribution and plot the distribution. - Use the
scipy.signal
module to filter a noisy signal. - Use the
scipy.linalg
module to solve a system of linear equations.
- The Public APIs GitHub repo provides a list of publicly available APIs. Try using some of them.
- Try to find an API that requires authentication and use it.
- Create a database of your favourite films and actors. Try to normalise the data.
- Find a website that provides a lot of data and write a scraper to generate a database.
- In lesson 37, you normalised a spreadsheet. Try to write a program that can convert a normalised database into a spreadsheet.
- What queries can you perform on the database using SQL that are slower in Python?
- Write a regex to match a valid email address.
- Try to parse an HTML page using regex. Search for links, email addresses, phone numbers, etc.
- Try to parse a CSV file using regex. Which columns are strings? Which are numbers?
- Try to convert the database from lesson 45 to MongoDB.
- Convert some of the pandas and MySQL queries from lessons 37 and 45 to MongoDB.
- Convert the JSON file from lesson 14 to YAML.
- Convert the database from lesson 45 or 47 into YAML. If you're using a spreadsheet, what are the different ways you can represent the data?
- Try to write a program that can find all the files on your computer that contain a given string.
- Write code that backs up all your files each day but only includes files that have changed and keeps backups for a week.
- Try to use the os library to run an executable file and capture the output.
ast
is a module that can be used to parse Python code. Try using it to parse a file and find all the functions that are defined.ast.literal_eval
is a function that can be used to evaluate a string as Python code. What are the advantages/disadvantages of using this overeval
?
- Convert a list of integers to a binary file and then load it again.
- How could you reduce the size of a file if you knew that it only contained a-z, A-Z, 0-9, spaces and a dot? What's the smallest file you can make that contains the alphabet?
- Huffman Coding is a way of compressing data. Try to implement it to compress and decompress a file.
- Generate code to iterate over all images in a folder and resize them to a given size.
- Add a caption to the images based on the filename.
- Try to find all the images on a website and download them. Convert them all to jpgs and resize them to a given size.
- Pillow provides a lot of other functionality. Try using some of it.
- Generate Julia Sets using OpenCV.
- Use OpenCV modules to detect the foreground and background of an image.
- Download an image with shapes on it and use OpenCV to detect the shapes and their positions.
- Use OpenCV to animate a pendulum swinging.
- Try to write a program that uses threads to download multiple files at once.
- Write a program to increment a counter using threads. How much faster is it than using a single thread?
- Write a consumer and producer program where some threads add to a list and other threads take from the list and print the results. Make sure that the length is always less than 10.
- Use sys to print the arguments passed to a program. Can you write a simpler version of ArgParse from lesson 39?
- Use sys to write the stdout and stderr to separate files.
- Run another program from your program using sys. How does this compare to using os?
- Use sys to print the current memory usage of your program.
pathlib
is a module that can be used to manipulate paths. Try using it to find all the files in a folder and its subfolders.- Delete cached files from your computer.
- Rename files so that all the paths become dots, for example,
a/b/c/d.txt
becomesa.b.c.d.txt
. - Sort files in a folder based on their extension, for example,
a.txt
andb.jpg
becometxt/a.txt
andjpg/b.jpg
.
- Trim silence from the start and end of an audio file.
- Group all songs by artist and album and save them to appropriate folders.
- Compress all songs without sacrificing too much quality.
- Try to find the shortest path between two Wikipedia pages using only links on the page.
- Find a page and see how many different languages it is available in.
- Find a few similar pages and see how many references they have in common.
- Create a script to fill in the emojis in a file.
- Write code to replace as many words in a file with emojis as possible.
- Write a script to convert a file to emoji art.
- Plot some of the charts from lesson 38 using seaborn.
- Play around with the given datasets and see what interesting information you can find.
- Use a heatplot to plot the correlations between different columns in a dataset.
- Modify the dataframe difference code from lesson 37 to highlight additions, deletions and changes in different colours.
- Write code to print a coloured progress bar that changes colour as it progresses.
- Display an image in the terminal using colour characters.
- Use CMA-ES to find the minimum of a bounded polynomial.
- Create a weighted heuristic for a game and use CMA-ES to find the best weights.
- Create a race car simulator and use CMA-ES to find the best parameters for the car.
- Take a program that you've written and try to reduce the number of lines by using semicolons.
- It's bad practice to use semicolons in Python. Write a piece of code to replace all unnecessary semicolons with newlines.
- Write a program that downloads a lot of web pages. Place the urls in a queue and have multiple threads downloading and saving the pages.
- Use a priority queue to implement Dijkstra's algorithm.
- Parallelise code from lesson 53 using one queue for the values that need computing and another queue for the results.
- Have a look at
scipy.stats
. What can you do with scipy that you can't do with statistics? - Try to implement some of the functions in statistics using numpy.
- Try to implement some of the functions in itertools using generators.
- Use an infinite iterator to create a loading spinner.
- Create a function to flatten nested lists using itertools.
- Simulate different games of the lottery and see how likely you are to win.
- Try to write your own versions of the functions in functools.
- Create a function to map each word to a unique integer. Use an LRU cache to speed up the function and see what the optimal cache size is.
- Lots of the functions in the
math
module take multiple arguments. Write functions, such asdouble
andcube
that usefunctools.partial
to create functions that only take one argument.
- Write code to compress a folder to a zip file then delete the folder and to decompress a zip file and delete the zip file.
- Write code to extract all the images from a zip file but no other files.
- Transfer a file between two computers using a zip file. What level of compression minimises the total time?
- Find the best polynomial to fit the
log
function within a a given range. - See how changing the loss function affects the results. Which loss function gives the best results?
- How can you modify the parameters to find the solution? Try combining CMAES from lesson 62 to find the best parameters.
- Docstrings often have a standardised format. Which format do you prefer? Try to stick to it in your code.
- Add typing hints to some of the functions you've written in previous exercises.
- The
typing
module has a lot of different types. Try using some of them in your code.
- Try to create an executable for some of the programs you've written.
- Try to make it look as professional as possible.
- Use the
cryptography
module to save the users password to a file securely. - Verify the hash of the password against a known hash. That way, you don't need to store the password at all.
- Modify the XML from the lesson so that the default language is German and the English is provided as a translation.
- Try to convert the JSON file from lesson 14 to XML.
- Convert the YAML from lesson 48 into XML. Search for elements with certain properties using an XPath query.
- Create a program that acts as a text editor using pynput. Try to implement backspace and undo.
- Use pynput to use the browser and download a file. Assume that all the buttons stay in the same place.
- Create a GUI that counts the number of button presses in one second. How many can you do? How many can you do with pynput?
- Password protect your key file before allowing the user to use it.
- Create a password manager that uses the key file to encrypt the passwords. How can you give different users access to different passwords?
- Create a wrapper that takes in a dictionary and only passes in the keys that are valid for a function.
- Modify this function to have default arguments for the missing keys.
- Use inspect to create a webpage that displays documentation and source code for a python module.
dataclasses
provide a lot of boilerplate code. How could you write your own version of a dataclass?
- Take a screenshot of a webpage and use pytesseract to extract the text.
- Extract the text from images of road signs.
- What are the limits of pytesseract? What kinds of text can it not read?
- Use the built in translate function to encode and decode a message. Can you write an automatic decryptor?
- Translate some text with the
translate
module. - Use the
googletrans
module for translation. How does it comparetranslate
?
- Is it faster to perform the sorting in PrettyTable or Pandas?
- Display a DataFrame in PrettyTable, highlighting the min and the max in each column.
- Write code that takes a PrettyTable and converts it to a DataFrame.
- Create an iterable that acts as a spinner until broken:
for _ in Spinner():
...
if condition:
break
- Create a progress bar iterable to wrap around the range function:
for _ in ProgressRangeBar(100):
...
- Make these two iterables customisable for the type of Spinner/Progress Bar.
- Create a program that takes in a birthday and tells you how many days you have to wait.
- Create a world clock that displays the time of different cities.
- Display the date and time in different formats. What feels most natural?
- Try to implement some of the functions in collections using dictionaries.
- Create a class that inherits from
collections.UserDict
and overrides the__getitem__
method to return a default value if the key doesn't exist. - Create a class that inherits from
collections.UserList
and overrides the__getitem__
method to extend the list and fill withNone
when out of range.
- Create a flask app that supports multiple users logging in to view a personalised page.
- Create a flask app that acts as a search engine for a user's files.
- Create a flask app that allows users to upload files and view them.
- Create a flask app that stores all of the user's interactions in a database and displays an analysis of their behaviour.
- Create test cases for the calculator from lesson 4.
- Create test cases for the functions in lesson 37.
- Unittest supports mocking. Write test cases for a function that uses an external API and mock the API.
pytest
adds additional functionality to unittest. Try using it instead of and alongside unittest.
- Create a pretty-print function for JSON that uses pattern matching to display the data in a more readable format.
- Create a reverse function for a list that uses pattern matching and recursion. (Don't use the builtin
reversed
function.) - Using the matrix multiplication function from lesson 18, create a function that uses pattern matching to multiply scalars, vectors and matrices.
- Write a program to automatically turn a markdown document into a PDF.
- Write a program that checks the weather forecast and produces a report as a PDF.
- Write some utility scripts to combine PDFs, delete pages, etc.
- Connect two computers and send messages between them.
- Create a chatroom that allows multiple users to connect and send messages.
- Create a server that sends work to multiple clients and collects the results.
- Create safe arithmetic operators that raise
ValueError
s when the operation would raise aTypeError
. Do this without usingtry
/except
. - Create a
mint
class that does arithmetic modulo a prime number. - Use the decimal class to find an approximation of pi.
- Create a metaclass that checks that all the methods in a class have a docstring.
- Modify the
mint
class from lesson 88 to use a metaclass to check that all inputs to functions are integers.
- Create a program to take in a dataframe and send personalised emails to the recipients.
- Create a program to send an email to yourself when a program finishes running.
- Python also has an
IMAP
module. Try using this to read and respond to emails automatically.
- Create a program to add labels to emails based on their subject and content.
- Create a program to reply automatically to emails based on a condition.
- Send and receive zip files via email and automatically download and extract them.
- Create an abstract class
Saveable
that allows you toobject.save(filename)
. - Create an abstract
Configurable
class that allows you toCls.from_config(config)
. Also create aConfig
class that can be used to store the configuration.
- Find who your most common friends of friends are that you aren't friends with on Twitter.
- Find the most common words in your tweets.
- Tweet a random joke or inspirational quote every day.
- Send yourself a message when someone tweets about you.
- Create a program that waits a random amount of time during the loop and displays a progress bar. How accurate is the prediction?
- Create nested for loops and display a progress bar for each loop. How can you make sure that the progress bars are displayed correctly?
- Have multiple threads performing different tasks and display a progress bar for each task.
- Have multiple threads performing the same task with a shared progress bar.
- Jazz up Für Elise with different musical features.
- Pretty MIDI is an alternative library for working with MIDI files. How does the interface compare?
- Create a program to combine two MIDI files into one in a way that sounds good.
- Try downloading FashionMNIST and training a neural network to classify the images. This dataset is much harder than MNIST but how well can you do?
- PyTorch has a lot of tutorials. Try following them and getting better at using the framework.
- Try using your improved neural network from lesson 96 to classify the images in TensorFlow.
- TensorFlow has a lot of tutorials. Try following them and getting better at using the framework.
jax
is starting to replace TensorFlow. Try using it instead of TensorFlow.
- Manim has a lot of features. Try using some of them.
- Create a video that explains a mathematical concept.
- Create a video that explains a programming concept.
- There are lots of style guides and formatters for Python. Try using some of them and see which one you prefer.
- Try taking some of your earlier code and refactoring it to be more Pythonic.
- GitHub is a website that allows you to share code online. Have a look at some source code done by professionals and see how you can improve your code.
- Try contributing to a GitHub project.