
About the Book
You already know you want to learn Python, and a smarter way to learn Python 3 is to learn by doing. The Python Workshop focuses on building up your practical skills so that you can build up your machine learning skills as a data scientist, write scripts that help automate your life and save you time, or even create your own games and desktop applications. You'll learn from real examples that lead to real results.
Throughout The Python Workshop, you'll take an engaging step-by-step approach to understanding Python. You won't have to sit through any unnecessary theory. If you're short on time you can jump into a single exercise each day or spend an entire weekend learning about Python scripting. It's your choice. Learning on your terms, you'll build up and reinforce key skills in a way that feels rewarding.
Every physical copy of The Python Workshop unlocks access to the interactive edition. With videos detailing all exercises and activities, you'll always have a guided solution. You can also benchmark yourself against assessments, track progress, and receive free content updates. You'll even earn a secure credential that you can share and verify online upon completion. It's a premium learning experience that's included with your printed copy. To redeem, follow the instructions located at the start of your Python book.
Fast-paced and direct, The Python Workshop is the ideal companion for Python beginners. You'll build and iterate on your code like a software developer, learning along the way. This process means that you'll find that your new skills stick, embedded as best practices. You will have a solid foundation for the years ahead.
About the Chapters
Chapter 1, Vital Python – Math, Strings, Conditionals, and Loops, explains how to write basic Python programs, and outlines the fundamentals of the Python language.
Chapter 2, Python Structures, covers the essential elements that are used to store and retrieve data in all programming languages.
Chapter 3, Executing Python – Programs, Algorithms, and Functions, explains how to write more powerful and concise code through an increased appreciation of well-written algorithms, and an understanding of functions.
Chapter 4, Extending Python, Files, Errors, and Graphs, covers the basic I/O (input-output) operations for Python and covers using the matplotlib and seaborn libraries to create visualizations.
Chapter 5, Constructing Python – Classes and Methods, introduces the most central concepts in object-oriented programming, and it will help you write code using classes, which will make your life easier.
Chapter 6, The Standard Library, covers the importance of the Python standard library. It explains how to navigate in the standard Python libraries and overviews some of the most commonly used modules.
Chapter 7, Becoming Pythonic, covers the Python programming language, with which you will enjoy writing succinct, meaningful code. It also demonstrates some techniques for expressing yourself in ways that are familiar to other Python programmers.
Chapter 8, Software Development, covers how to debug and troubleshoot our applications, how to write tests to validate our code and the documentation for other developers and users.
Chapter 9, Practical Python – Advanced Topics, explains how to take advantage of parallel programming, how to parse command-line arguments, how to encode and decode Unicode, and how to profile Python to discover and fix performance problems.
Chapter 10, Data Analytics with pandas and NumPy, covers data science, which is the core application of Python. We will be covering NumPy and pandas in this chapter.
Chapter 11, Machine Learning, covers the concept of machine learning and the steps involved in building a machine learning algorithm.
Conventions
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Python provides the collections.defaultdict type."
A block of code is set as follows:
cubes = [x**3 for x in range(1,6)]
print(cubes)
New terms and important words are shown like this: "A script is a file that is designed to be executed, usually from the command line."
Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "You can also use Jupyter (New | Text File)."
Long code snippets are truncated and the corresponding names of the code files on GitHub are placed at the top of the truncated code. The permalinks to the entire code are placed below the code snippet. It should look as follows:
Exercise66.ipynb
def annotate_heatmap(im, data=None, valfmt="{x:.2f}",
textcolors=["black", "white"],
threshold=None, **textkw):
import matplotlib
if not isinstance(data, (list, np.ndarray)):
Before You Begin
Each great journey begins with a humble step. Our upcoming adventure in the land of Python is no exception. Before you can begin, you need to be prepared with the most productive environment. In this section, you will see how to do that.
To Install Jupyter on Windows
To install Jupyter on windows, follow these steps:
You will be using Python 3.7 (from https://python.org):
- You will first be installing Anaconda Navigator, which is an interface through which you can access your local Jupyter Notebook.
- Head to https://www.anaconda.com/distribution/ and download and install the software.
- Now use the Windows search functionality, type in Anaconda Navigator, and click to open the software. You will see the following screen:
Figure 0.1: Anaconda installation screen
- Now, click on Launch under the Jupyter Notebook option and launch the notebook on your local system:

Figure 0.2: Jupyter Notebook launch option
You have successfully installed Jupyter Notebook onto your system.
To Install the Python Terminal on Windows
To install the Python terminal on Windows, follow these steps:
- Open the following link, which is the Python community website URL: https://www.python.org/downloads/.
- Once you have downloaded the software, you need to install it.
- Next, in the Windows Start menu, search for Python and click on the software.
The Python terminal will look like this:

Figure 0.3: Python terminal interface
You have successfully installed the Python terminal onto your system.
A Few Important Packages
Some of the exercises in this book require the following packages:
- Matplotlib
- Seaborn
- NumPy
- scikit-learn
Install them by following this guide. On Windows, open up the command prompt. On macOS or Linux, open up the terminal. Type the following commands:
pip install matplotlib seaborn numpy
If you prefer to use Anaconda to manage your packages, type in the following:
conda install matplotlib seaborn numpy
conda install scikit-learn
If you installed Anaconda as recommended, these packages should already be included.
To install Docker
- Go to docker.com and click Get Started:
Figure 0.4: Getting started with Docker
- Next, click Download Desktop and Take a Tutorial:
Figure 0.5: Download Desktop and Take a Tutorial
- Follow the instructions to create a Docker ID:
Figure 0.6: Creation of the Docker ID
- Once you have signed in, click Get Started with Docker Desktop:
Figure 0.7: Downloading Docker to your local system
- Download and install Docker Desktop:

Figure 0.8: Getting started with Docker on your local system
Installing the Code Bundle
Download the code files from GitHub at https://packt.live/2PfducF and place them in a new folder called C:\Code on your local system. Refer to these code files for the complete code bundle.