A set of Python3 exercises to develop solid scripting foundations
Before going through these exercices, you should be familiar with sections 2 to
8 of the official Python tutorial. Some
sections have See also
links. Please consult these also.
Sections 9, 10, and 11 of the official Python tutorial are more advanced material that is worth looking at once you are comfortable with the previous sections.
The goal of these exercices is to help you gain experience in what many introductions to programming in Python avoid: writing useful scripts that actually run on your own computer, as opposed to the interactive terminal, an embedded browser interpreter or an iPython notebook.
As a result, a lot of the exercises will require parsing user input (for example parameters) given on the command line, as well as reading and writing files. These are fundamental notions for scripting that are too often ommited from introductory material.
The exercises are numbered and go from easy to more advanced. Problems and solutions may be added through time.
For each problem:
- Read the problem description in its
<problem>_template.py
file - Any needed input file are found in the problem's folder
- Build your solution by filling the template
- Check the solution(s) for more learning opportunities
- Solutions should work with Python 3.6+ under Windows, Mac, and Linux
- You can and should re-use ideas from previous solutions
- You are encouraged to experiment with new ideas
The exercises will provide practice for:
- Using Python scripts that start with
#!/usr/bin/env python3
(Linux and MacOS only) - Writing useful documentation strings (aka: docstrings) and using them (
__doc__
) - Making sure input values are valid
- Using the basic data types (numbers, strings, lists, tuples...)
- Reading and writing files
- Reading from and writing to compressed files with the
gzip
module - Simplifying programs by using appropriate data structures
- Creating tree-like structures with dictionaries
- Constructing trees with dictionaries
- Parsing user input with the
argparse
module - Using more advanced functionalities (
defaultdict
, Counter, regular expressions...) - Using paths properly in all OSes (Windows, Mac, Linux...) with the
os
module - Simple search and replace and with regular expressions using the
re
module - Keeping data well organized with simple classes
- Reading multiple files from a folder
- Classical text encription and decription (ceasar, substitution, vigenère, playfair...)
- Finding the longuest palindrome
- Mimic input text
Python exercises by Eric Normandeau is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.