Full Speed Python
João Ventura
Contents
1 Introduction
2 Installation
Installing on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing on macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 Numbers and strings
Exercises with numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises with strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
4
4
6
6
8
9
9
4 Lists
11
Exercises with lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
12
List comprehensions
Exercises with list comprehensions
12
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
5 Modules and functions
Exercises with the math module . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises with functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recursive functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises with recursive functions . . . . . . . . . . . . . . . . . . . . . . . . .
6 Iteration and loops
Exercises with the for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises with the while statement
. . . . . . . . . . . . . . . . . . . . . . . .
7 Dictionaries
Exercises with dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises with sub-dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . .
8 Classes
Exercises with classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises with inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9 Iterators
Iterator classes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises with iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
15
15
15
16
17
19
19
21
22
23
24
25
25
25
27
28
29
1
10 Generators
30
Exercises with generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
11 Coroutines
Exercises with coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises with coroutine pipelines . . . . . . . . . . . . . . . . . . . . . . . . .
32
33
33
35
2
Chapter 1
Introduction
This book aims to teach the Python programming language using a practical approach.
Its method is quite simple: after a short introduction to each topic, the reader is invited
to learn more by solving the proposed exercises.
These exercises have been used extensively in my web development and distributed
computing classes at the Superior School of Technology of Setúbal. With these exercises,
most students are up to speed with Python in less than a month. In fact, students of
the distributed computing course, taught in the second year of the software engineering
degree, become familiar with Python’s syntax in two weeks and are able to implement a
distributed client-server application with sockets in the third week.
Please note that this book is a work in progress and, as such, may contain a few spelling
errors that may be corrected in the future. However it is made available now as it is so it
can be useful to anyone who wants to use it. I sincerely hope you can get something good
through it.
The source of this book is available on github (https://github.com/joaoventura/full-speed-
python). I welcome any pull requests to correct misspellings, suggest new exercises or to
provide clarification of the current content.
All the best,
João Ventura - Adjunct Professor at the Escola Superior de Tecnologia de Setúbal.
3
Chapter 2
Installation
In this chapter we will install and run the Python interpreter in your local computer.
Installing on Windows
1. Download the latest Python 3 release for Windows on https://www.python.org/
downloads/windows/ and execute the installer. At the time of writing, this is Python
3.6.4.
2. Make sure that the “Install launcher for all users” and “Add Python to PATH”
settings are selected and choose “Customize installation”.
Figure 2.1: Windows installation
3. In the next screen “Optional Features”, you can install everything, but it is essential
to install “pip” and “pylauncher (for all users)”. Pip is the Python package manager
that allows you to install several Python packages and libraries.
4. In the Advanced Options, make sure that you select “Add Python to environment
variables”. Also, I suggest that you change the install location to something like
4
C:\Python36\ as it will be easier for you to find the Python installation if something
goes wrong.
Figure 2.2: Windows installation
5. Finally, allow Python to use more than 260 characters on the file system by selecting
“Disable path length limit” and close the installation dialog.
Figure 2.3: Windows installation
6. Now, open the command line (cmd) and execute “python” or “python3”. If everything
was correctly installed, you should see the Python REPL. The REPL (from Read,
Evaluate, Print and Loop) is a environment that you can use to program small
snippets of Python code. Run exit() to leave the REPL.
5
Figure 2.4: Python REPL
Installing on macOS
You can download the latest macOS binary releases from https://www.python.org/
downloads/mac-osx/. Make sure you download the latest Python 3 release (3.6.4 at
the time of writing). You can also use Homebrew, a package manager for macOS
(https://brew.sh/). To install the latest Python 3 release with Homebrew, just do “brew
install python3” on your terminal. Another option is to use the MacPorts package
manager (https://www.macports.org/) and command “port install python36”.
Figure 2.5: Python REPL
Finally, open the terminal, execute python3 and you should see the Python REPL as
above. Press Ctrl+D or write exit() to leave the REPL.
Installing on Linux
To install Python on Linux, you can download the latest Python 3 source releases
from https://www.python.org/downloads/source/ or use your package manager (apt-get,
6
aptitude, synaptic and others) to install it. To make sure you have Python 3 installed on
your system, run python3 --version in your terminal.
Finally, open the terminal, execute python3 and you should see the Python REPL as in
the following image. Press Ctrl+D or write exit() to leave the REPL.
Figure 2.6: Python REPL
7