The Python Workshop: Write Python code to solve challenging real-world problems, 2nd Edition
Gain proficiency, productivity, and power by working on projects and kick-starting your career in Python with this comprehensive, hands-on guide.
- Understand and utilize Python syntax, objects, methods, and best practices
- Explore Python’s many features and libraries through real-world problems and big data
- Use your newly acquired Python skills in machine learning as well as web and software development
Python is among the most popular programming languages in the world. It’s ideal for beginners because it’s easy to read and write, and for developers, because it’s widely available with a strong support community, extensive documentation, and phenomenal libraries – both built-in and user-contributed.
This project-based course has been designed by a team of expert authors to get you up and running with Python. You’ll work though engaging projects that’ll enable you to leverage your newfound Python skills efficiently in technical jobs, personal projects, and job interviews. The book will help you gain an edge in data science, web development, and software development, preparing you to tackle real-world challenges in Python and pursue advanced topics on your own. Throughout the chapters, each component has been explicitly designed to engage and stimulate different parts of the brain so that you can retain and apply what you learn in the practical context with maximum impact.
By completing the course from start to finish, you’ll walk away feeling capable of tackling any real-world Python development problem.
What you will learn
- Write efficient and concise functions using core Python methods and libraries
- Build classes to address different business needs
- Create visual graphs to communicate key data insights
- Organize big data and use machine learning to make regression and classification predictions
- Develop web pages and programs with Python tools and packages
- Automate essential tasks using Python scripts in real-time execution
Who this book is for
This book is for professionals, students, and hobbyists who want to learn Python and apply it to solve challenging real-world problems. Although this is a beginner’s course, you’ll learn more easily if you already have an understanding of standard programming topics like variables, if-else statements, and functions. Experience with another object-oriented program, though not essential, will also be beneficial. If Python is your first attempt at computer programming, this book will help you understand the basics with adequate detail for a motivated student.
Cover Title Page Copyright and Credits Contributors Table of Contents Preface Chapter 1: Python Fundamentals – Math, Strings, Conditionals, and Loops Overview Introduction Technical requirements Opening a Jupyter Notebook Python as a calculator Standard math operations Basic math operations Order of operations Exercise 1 – getting to know the order of operations Python concept – spacing Number types – integers and floats Exercise 2 – integer and float types Complex number types Errors in Python Variable assignment Exercise 3 – assigning variables Casting – changing types Activity 1 – assigning values to variables Variable names Exercise 4 – naming variables Multiple variables Exercise 5 – assigning multiple variables Comments Exercise 6 – comments in Python Docstrings Activity 2 – finding the area of a triangle Strings – concatenation, methods, and input() String syntax Exercise 7 – string error syntax Escape characters Multiline strings The print() function Exercise 8 – displaying strings String operations and concatenation Exercise 9 – string concatenation String interpolation Comma separators f-strings The len() function String methods Exercise 10 – implementing string methods Casting Exercise 11 – types and casting The input() function Exercise 12 – using the input() function Activity 3 – using the input() function to rate your day String indexing and slicing Indexing Slicing Strings and their methods Booleans and conditionals Booleans Exercise 13 – Boolean variables Logical operators Comparison operators Exercise 14 – comparison operators Comparing strings Exercise 15 – practicing comparing strings Conditionals The if syntax Indentation Exercise 16 – using the if syntax if else Exercise 17 – using the if-else syntax The elif statement Loops while loops The break keyword Activity 4 – finding the least common multiple (LCM) Programs Exercise 18 – calculating perfect squares Exercise 19 – real estate offer for loops Exercise 20 – using for loops The continue keyword Activity 5 – building conversational bots using Python Summary Chapter 2: Python Data Structures Overview Introduction Technical requirements The power of lists Exercise 21 – working with Python lists List methods Exercise 22 – basic list operations Accessing an item from a list Exercise 23 – accessing an item from shopping list data Adding an item to a list Exercise 24 – adding items to our shopping list Exercise 25 – looping through a list Matrices as nested lists Exercise 26 – using a nested list to store data from a matrix Activity 6 – using a nested list to store employee data Matrix operations Exercise 27 – implementing matrix operations (addition and subtraction) Matrix multiplication operations Exercise 28 – implementing matrix operations (multiplication) Dictionary keys and values Exercise 29 – using a dictionary to store a movie record Activity 7 – storing company employee table data using a list and a dictionary Dictionary methods Exercise 30 – accessing a dictionary using dictionary methods Tuples Exercise 31 – exploring tuple properties in a dance genre list Zipping and unzipping dictionaries and lists using zip() Exercise 32 – using the zip() method to manipulate dictionaries A survey of sets Exercise 33 – using sets in Python Set operations Exercise 34 – implementing set operations Choosing types Summary Chapter 3: Executing Python – Programs, Algorithms, and Functions Overview Introduction Technical requirements Python scripts and modules Exercise 35 – writing and executing our first script Python function example Exercise 36 – writing and importing our first module Shebangs in Ubuntu Docstrings Exercise 37 – adding a docstring to my_module.py Importing libraries Exercise 38 – finding the system date The if __name__ == ‘__main__’ statement Activity 8 – what’s the time? Python algorithms Exercise 39 – finding the maximum number Time complexity Sorting algorithms Exercise 40 – using bubble sort in Python Searching algorithms Exercise 41 – linear search in Python Exercise 42 – binary search in Python Basic functions Exercise 43 – defining and calling a function in the shell Exercise 44 – defining and calling a function in a Python script Exercise 45 – importing and calling the function from the shell Positional arguments Keyword arguments Exercise 46 – defining a function with keyword arguments Exercise 47 – defining a function with positional and keyword arguments Exercise 48 – using **kwargs Activity 9 – formatting customer names Iterative functions Exercise 49 – a simple function with a for loop Exiting early Exercise 50 – exiting the function during the for loop Activity 10 – the Fibonacci function with an iteration Recursive functions A terminating case Exercise 51 – recursive countdown Exercise 52 – factorials with iteration and recursion Activity 11 – the Fibonacci function with recursion Dynamic programming Exercise 53 – summing integers Timing your code Exercise 54 – calculating your code’s timing Activity 12 – the Fibonacci function with dynamic programming Helper functions Don’t Repeat Yourself Exercise 55 – helper currency conversion Variable scope Variables Defining inside versus outside a function The global keyword The nonlocal keyword Lambda functions Exercise 56 – the first item in a list Mapping with lambda functions Exercise 57 – mapping with a logistic transform Filtering with lambda functions Exercise 58 – using a filter lambda Sorting with lambda functions Summary Chapter 4: Extending Python, Files, Errors, and Graphs Overview Introduction Technical requirements Reading files Exercise 59 – reading a text file using Python Exercise 60 – reading partial content from a text file Writing files Exercise 61 – creating and writing content to files to record the date and time in a text file Preparing for debugging (defensive code) Writing assertions Exercise 62 – working with incorrect parameters to find the average using assert with functions Plotting techniques Exercise 63 – drawing a scatter plot to study the data between ice cream sales versus temperature Exercise 64 – drawing a line chart to find the growth in stock prices Exercise 65 – plotting bar plot to grade students Exercise 66 – creating a pie chart to visualize the number of votes in a school Exercise 67 – generating a heatmap to visualize the grades of students Exercise 68 – generating a density plot to visualize the scores of students Exercise 69 – creating a contour plot Extending graphs Exercise 70 – generating 3D plots to plot a sine wave The don’ts of plotting graphs Manipulating the axis Cherry picking data Wrong graph, wrong context Activity 13 – visualizing the Titanic dataset using a pie chart and bar plots Summary Chapter 5: Constructing Python – Classes and Methods Overview Introduction Technical requirements Classes and objects Exercise 71 – exploring strings Defining classes Exercise 72 – creating a Pet class The __init__ method Exercise 73 – creating a Circle class Keyword arguments Exercise 74 – the Country class with keyword arguments Methods Instance methods Exercise 75 – adding an instance method to our Pet class Adding arguments to instance methods Exercise 76 – computing the size of our country The __str__ method Exercise 77 – adding an __str__ method to the Country class Static methods Exercise 78 – refactoring instance methods using a static method Class methods Exercise 79 – extending our Pet class with class methods Properties The property decorator Exercise 80 – the full name property The setter method Exercise 81 – writing a setter method Validation via the setter method Inheritance The DRY principle revisited Single inheritance Exercise 82 – inheriting from the Person class Subclassing classes from Python packages Exercise 83 – subclassing the datetime.date class Overriding methods Calling the parent method with super() Exercise 84 – overriding methods using super() Multiple inheritances Exercise 85 – creating a consultation appointment system Method resolution order Activity 14 – creating classes and inheriting from a parent class Summary Chapter 6: The Standard Library Overview Introduction Technical requirements The importance of the Standard Library High-level modules Lower-level modules Knowing how to navigate the Standard Library Exercise 86 – using the dataclass module Exercise 87 – extending the echo.py example Working with dates and times Exercise 88 – comparing datetime across time zones Exercise 89 – calculating the time delta between two datetime objects Exercise 90 – calculating the Unix epoch time Activity 15 – calculating the time elapsed to run a loop Interacting with the OS OS information Exercise 91 – inspecting the current process information Using pathlib Exercise 92 – using the glob pattern to list files within a directory Listing all hidden files in your home directory Using the subprocess module Exercise 93 – customizing child processes with env vars Activity 16 – testing Python code Logging in Python Using logging Logger object Exercise 94 – using a logger object Logging in warning, error, and fatal categories Configuring the logging stack Exercise 95 – configuring the logging stack Using collections in Python The counter class Exercise 96 – counting words in a text document The defaultdict class Exercise 97 – refactoring code with defaultdict The ChainMap class Using functools Caching with functools.lru_cache Exercise 98 – using lru_cache to speed up our code Adapting functions with partial Exercise 99 – creating a print function that writes to stderr Activity 17 – using partial on class methods Summary Chapter 7: Becoming Pythonic Overview Introduction Technical requirements Using list comprehensions Exercise 100 – introducing list comprehensions Exercise 101 – using multiple input lists Activity 18 – building a chess tournament Set and dictionary comprehensions Exercise 102 – using set comprehensions Exercise 103 – using dictionary comprehensions Activity 19 – building a scorecard using dictionary comprehensions and multiple lists Using defaultdict to get default values Exercise 104 – adopting a default dict Creating custom iterators Exercise 105 – the simplest iterator Exercise 106 – a custom iterator Exercise 107 – controlling the iteration Leveraging itertools Exercise 108 – using infinite sequences and takewhile() Exercise 109 – turning a finite sequence into an infinite one, and back again Lazy evaluations with generators Exercise 110 – generating a Sieve Activity 20 – using random numbers to find the value of Pi Using regular expressions Exercise 111 – matching text with regular expressions Exercise 112 – using regular expressions to replace text Activity 21 – finding a winner for The X-Files Summary Chapter 8: Software Development Overview Introduction Technical requirements How to debug Exercise 113 – debugging a salary calculator Activity 22 – debugging sample Python code for an application Automated testing Test categorization Test coverage Writing tests in Python with unit testing Exercise 114 – checking sample code with unit testing Writing a test with pytest Creating a pip package Exercise 115 – creating a distribution that includes multiple files within a package Adding more information to your package Creating documentation the easy way Using docstrings Using Sphinx Exercise 116 – documenting a divisible code file More complex documentation Source code management Repository Commit Staging area Undoing local changes History Ignoring files Exercise 117 – making a change in CPython using Git Summary Chapter 9: Practical Python – Advanced Topics Overview Introduction Technical requirements Developing collaboratively Exercise 118 – writing Python on GitHub as a team Dependency management Virtual environments Exercise 119 – creating and setting up a conda virtual environment to install numpy and pandas Saving and sharing virtual environments Exercise 120 – sharing environments between a conda server and your local system Deploying code into production Exercise 121 – Dockerizing your Fizzbuzz tool Running code in parallel with multiprocessing Multiprocessing with execnet Exercise 122 – working with execnet to execute a simple Python squaring program Multiprocessing with the multiprocessing package Exercise 123 – using the multiprocessing package to execute a simple Python program Multiprocessing with the threading package Exercise 124 – using the threading package Parsing command-line arguments in scripts Exercise 125 – introducing argparse to accept input from the user Positional arguments Exercise 126 – using positional arguments to accept source and destination inputs from a user Performance and profiling Changing your Python environment PyPy Exercise 127 – using PyPy to find the time to get a list of prime numbers Cython Exercise 128 – adopting Cython to find the time taken to get a list of prime numbers Profiling code Profiling with cProfile Activity 23 – generating a list of random numbers in a Python virtual environment Summary Chapter 10: Data Analytics with pandas and NumPy Overview Introduction Technical requirements NumPy and basic stats Exercise 129 – converting lists into NumPy arrays Exercise 130 – calculating the mean of the test score Exercise 131 – finding the median from a collection of income data Skewed data and outliers Standard deviation Exercise 132 – finding the standard deviation from income data Finding the min, max, and sum Matrices Exercise 133 – working with matrices Computation time for large matrices Exercise 134 – creating an array to implement NumPy computations The pandas library Exercise 135 – using DataFrames to manipulate stored student test score data Exercise 136 – DataFrame computations with the student test score data Exercise 137 – more computations on DataFrames New rows and NaN Exercise 138 – concatenating and finding the mean with null values for our test score data Casting column types Working with big data Downloading data Downloading the Boston Housing data from GitHub Reading data Exercise 139 – reading and viewing the Boston Housing dataset Exercise 140 – gaining data insights on the Boston Housing dataset Null values Exercise 141 – viewing null values Replacing null values Creating statistical graphs Histograms Exercise 142 – creating a histogram using the Boston Housing dataset Exercise 143 – creating histogram functions Scatter plots Exercise 144 – creating a scatter plot for the Boston Housing dataset Correlation Exercise 145 – correlation values from the dataset Regression Box plots and violin plots Exercise 146 – creating box plots Exercise 147 – creating violin plots Activity 24 – performing data analysis to find the outliers in pay versus the salary report in the UK statistics dataset Summary Chapter 11: Machine Learning Overview Introduction Technical requirements Introduction to linear regression Simplifying the problem From one to N-dimensions The linear regression algorithm Exercise 148 – using linear regression to predict the accuracy of the median values of our dataset Linear regression function Testing data with cross-validation Exercise 149 – using the cross_val_score function to get accurate results on the dataset Regularization – Ridge and Lasso K-nearest neighbors, decision trees, and random forests K-nearest neighbors Exercise 150 – using k-nearest neighbors to find the median value of the dataset Exercise 151 – K-nearest neighbors with GridSearchCV to find the optimal number of neighbors Decision trees and random forests Exercise 152 – building decision trees and random forests Random forest hyperparameters Exercise 153 – tuning a random forest using RandomizedSearchCV Classification models Exercise 154 – preparing the pulsar dataset and checking for null values Logistic regression Exercise 155 – using logistic regression to predict data accuracy Other classifiers Naive Bayes Exercise 156 – using GaussianNB, KNeighborsClassifier, DecisionTreeClassifier, and RandomForestClassifier to predict the accuracy of our dataset Confusion matrix Exercise 157 – finding the pulsar percentage from the dataset Exercise 158 – confusion matrix and classification report for the pulsar dataset Boosting algorithms AdaBoost XGBoost Exercise 159 – using AdaBoost and XGBoost to predict pulsars Exercise 160 –using AdaBoost and XGBoost to predict median house values in Boston Activity 25 – using ML to predict customer return rate accuracy Summary Chapter 12: Deep Learning with Python Overview Introduction Technical requirements Colab notebooks Jupyter Notebook Introduction to deep learning Your first deep learning model First deep learning libraries Exercise 161 – preparing the Boston Housing dataset for deep learning Exercise 162 – using sequential deep learning to predict the accuracy of the median house values of our dataset Tuning Keras models Exercise 163 – modifying densely connected layers in a neural network to improve the score Number of epochs Exercise 164 – modifying the number of epochs in the neural network to improve the score Early Stopping Exercise 165 – optimizing the number of epochs with Early Stopping Additional regularization technique – Dropout Exercise 166 – using Dropout in a neural network to improve the score Building neural networks for classification Exercise 167 – building a neural network for classification Activity 26 – building your own neural network to predict whether a patient has heart disease Convolutional neural networks MNIST Exercise 168 – preparing MNIST data for machine learning CNN kernel Exercise 169 – building a CNN to predict handwritten digits Activity 27 – classifying MNIST Fashion images using CNNs Summary Chapter 13: The Evolution of Python – Discovering New Python Features Overview Introduction Python Enhancement Proposals Python 3.7 Built-in breakpoint Module dynamic attributes Nanosecond support in a time module The dict insertion order is preserved Dataclasses Importlib.resources Python 3.8 Assignment expression functools.cached_property importlib.metadata typing.TypedDict, typing.Final, and typing.Literal f-string debug support via = Positional-only parameters Python 3.9 PEG parser Support for the IANA database Merge (|) and update (|=) syntax for dicts str.removeprefix and str.removesuffix Type hints with standard collections Python 3.10 Pattern matching – PEP Parenthesized context managers Better error messages Type union operator (|) – PEP Statistics – covariance, correlation, and linear_regression Python 3.11 Faster runtime Enhanced errors in tracebacks The new tomllib package Required keys in dicts The new LiteralString type Exceptions notes – PEP Summary Index Other Books You May Enjoy
How to download source code?
1. Go to:
2. In the Find a repository… box, search the book title:
The Python Workshop: Write Python code to solve challenging real-world problems, 2nd Edition, sometime you may not get the results, please search the main title.
3. Click the book title in the search results.
3. Click Code to download.
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.