Learn Python Programming: A comprehensive, up-to-date, and definitive guide to learning Python
By Fabrizio Romano and Heinrich Kruger
()
Fabrizio Romano
Fabrizio Romano was born in Italy in 1975. He holds a master's degree in computer science engineering from the University of Padova. He's been working as a professional software developer since 1999. Fabrizio has been working at Sohonet since 2016, where he currently serves as a development manager. In 2020, the Television Academy honored him and his team with an Emmy Award in Engineering Development for advancing remote collaboration.
Read more from Fabrizio Romano
Learning Python: Learn to code like a professional with Python - an open source, versatile, and powerful programming language Rating: 5 out of 5 stars5/5Python编程入门与实战: Chinese Edition Rating: 0 out of 5 stars0 ratings
Related to Learn Python Programming
Related ebooks
Hands-On Python for DevOps: Leverage Python's native libraries to streamline your workflow and save time with automation Rating: 0 out of 5 stars0 ratingsHow Computers Make Books: From graphics rendering, search algorithms, and functional programming to indexing and typesetting Rating: 0 out of 5 stars0 ratingsSSO Standard Requirements Rating: 0 out of 5 stars0 ratings5 Books for Every DevOps Professional Rating: 0 out of 5 stars0 ratingsAI Unleashed: Strategies for Business Success Through Automation in 2025 Rating: 0 out of 5 stars0 ratingsMastering Time Series Analysis and Forecasting with Python Rating: 0 out of 5 stars0 ratingsAn Introduction to Scrum: Understanding and Applying the Software Rating: 0 out of 5 stars0 ratingsJBoss AS 7 Development Rating: 0 out of 5 stars0 ratingsTypescript Programming For Beginners: The Comprehensive Guide To Building, Scaling, And Maintaining Web Applications With Typescript Rating: 0 out of 5 stars0 ratingsNode.js Cookbook: Practical recipes for building server-side web applications with Node.js 22 Rating: 0 out of 5 stars0 ratingsSoftware development process A Clear and Concise Reference Rating: 0 out of 5 stars0 ratingsThe Art of JavaScript Performance Optimization: From Theory to Practice Rating: 0 out of 5 stars0 ratingsMastering Vim: Efficient and effortless editing with Vim and Vimscript Rating: 0 out of 5 stars0 ratingsLearning Pentaho CTools Rating: 0 out of 5 stars0 ratingsTOGAF® Version 9.1 - A Pocket Guide Rating: 0 out of 5 stars0 ratingsHTML language complete Rating: 0 out of 5 stars0 ratingsPython Data Structures and Algorithms Rating: 5 out of 5 stars5/5Master Python: Unlock the Language of the Future Rating: 0 out of 5 stars0 ratingsPython: The Middle Way: Python, #2 Rating: 0 out of 5 stars0 ratingsPython Essentials Rating: 5 out of 5 stars5/5PYTHON FOR BEGINNERS: A Comprehensive Guide to Learning Python Programming from Scratch (2023) Rating: 0 out of 5 stars0 ratingsPython: The Ultimate Beginner's Guide To Python Mastery Rating: 0 out of 5 stars0 ratingsMastering the Craft of Python Programming: Unraveling the Secrets of Expert-Level Programming Rating: 0 out of 5 stars0 ratingsPython Made Easy: A First Course in Computer Programming Using Python Rating: 0 out of 5 stars0 ratingsMastering Object-Oriented Programming with Python: Unlock the Secrets of Expert-Level Skills Rating: 0 out of 5 stars0 ratings
Programming For You
Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsBeginning Programming with Python For Dummies Rating: 3 out of 5 stars3/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsSQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Escape the Game: How to Make Puzzles and Escape Rooms Rating: 3 out of 5 stars3/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5PYTHON PROGRAMMING Rating: 4 out of 5 stars4/5iPhone 16 Pro Max User Manual: The Complete Step-By-Step Guide to Maximize your New iPhone 16 Pro Max and iOS 18 Rating: 0 out of 5 stars0 ratings
Reviews for Learn Python Programming
0 ratings0 reviews
Book preview
Learn Python Programming - Fabrizio Romano
Learn Python Programming
Fourth Edition
A comprehensive, up-to-date, and definitive guide to learning Python
Fabrizio Romano
Heinrich Kruger
Packt and this book are not officially connected with Python. This book is an effort from the Python community of experts to help more developers.
Learn Python Programming
Fourth Edition
Copyright © 2024 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Senior Publishing Product Manager: Denim Pinto
Acquisition Editor – Peer Reviews: Swaroop Singh
Project Editor: Amisha Vathare
Content Development Editor: Shazeen Iqbal
Copy Editor: Safis Editing
Technical Editor: Tejas Mhasvekar
Proofreader: Safis Editing
Indexer: Tejal Soni
Presentation Designer: Ganesh Bhadwalkar
Developer Relations Marketing Executive: Deepak Kumar
First published: December 2015
Second edition: June 2018
Third edition: October 2021
Fourth edition: November 2024
Production reference: 1261124
Published by Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK.
ISBN 978-1-83588-294-8
www.packt.com
To Margherita e Graziano, cuore del mio cuore. Thank you for all the love, help, and advice you’ve given me in the past twenty years
– Fabrizio Romano
To my wife, Debi, without whose love, support, and endless patience, I would not have been able to do this
– Heinrich Kruger
Contributors
About the authors
Fabrizio Romano was born in Italy in 1975. He holds a master’s degree in computer science engineering from the University of Padova. He’s been working as a professional software developer since 1999. Fabrizio has been working at Sohonet since 2016, where he currently serves as a development manager. In 2020, the Television Academy honored him and his team with an Emmy Award in Engineering Development for advancing remote collaboration.
I would like to thank all the people at Packt, in particular Amisha Vathare for her professionalism and kindness. Thanks to Stefano and Javier, the reviewers, for their insightful comments, and to Heinrich. To my wife Elisa goes my deepest gratitude: thank you for your love and support.
Heinrich Kruger was born in South Africa in 1981. He holds a master’s degree in computer science from Utrecht University in the Netherlands. He has been working as a professional software developer since 2014. Heinrich has been working alongside Fabrizio in the Product Team at Sohonet since 2017. In 2020, the Television Academy honored them with an Emmy Award in Engineering Development for advancing remote collaboration.
I want to thank Fabrizio for asking me to help him with this book. It’s been a great experience working with you, my friend. I would also like to thank Stefano Chinellato and Javier Navarro for their helpful comments, as well as everyone at Packt who helped us along the way. Most of all, I thank my wife Debi for all her love, encouragement, and support.
About the reviewer
Javier Muñoz was born in Valladolid, Spain, where he nurtured a deep interest in computers from an early age. He holds a degree in Telecommunications Engineering with a specialization in Telematics and a Master’s degree in Big Data Science from the Universidad de Valladolid, graduating with First Class Honours.
After his studies, Javier moved to Dublin, Ireland, where he worked as a software engineer and data analyst. His career progressed when he joined Optum, a Fortune 5 healthcare technology company, where he focused on AI. At Optum, he developed and deployed AI models to enhance medical diagnosis and treatment, collaborating with healthcare professionals to improve patient outcomes while ensuring compliance with regulatory standards like HIPAA and GDPR.
In parallel with his work at Optum, Javier founded Saoi Tech Solutions, a startup that delivered large-scale machine learning solutions for multinational corporations. His projects included pioneering research in facial recognition, and he demonstrated expertise in deploying AI models at scale using advanced cloud and containerization technologies.
Currently, Javier works in the United Kingdom as a software engineer at Sohonet, where he focuses on Python-based applications written in Django, primarily for the cinema and entertainment industry. His work at Sohonet involves developing tools that facilitate collaboration and enhance production workflows in film and television.
Javier’s technical expertise, particularly in Python and AI, has earned him a role as a technical reviewer for Learn Python Programming, Fourth Edition, where he ensures the content is practical, accurate, and relevant for today’s programmers.
I would like to sincerely thank my friends and colleagues Fabrizio and Heinrich, the authors of this book, for their incredible support, collaboration, and the opportunity to contribute as a technical reviewer. I am also grateful to the rest of the Sohonet team, as well as my past colleagues and mentors, whose guidance has been invaluable throughout my journey. This experience has been both rewarding and enriching, and I deeply appreciate everyone who has been part of it.
Stefano Chinellato is a software engineer residing in Italy. He holds a Master’s degree in Computer Science Engineering from the University of Padova. With over 15 years of experience in software development, he focuses on web applications and has a strong interest in algorithms and statistics.
Contents
Preface
Who this book is for
What this book covers
To get the most out of this book
Get in touch
A Gentle Introduction to Python
A brief introduction to programming
Enter the Python
About Python
Portability
Coherence
Developer productivity
An extensive library
Software quality
Software integration
Data science
Satisfaction and enjoyment
What are the drawbacks?
Who is using Python today?
Setting up the environment
Installing Python
Useful installation resources
Installing Python on Windows
Installing Python on macOS
Installing Python on Linux
The Python console
About virtual environments
Your first virtual environment
Installing third-party libraries
The console
How to run a Python program
Running Python scripts
Running the Python interactive shell
Running Python as a service
Running Python as a GUI application
How is Python code organized?
How do we use modules and packages?
Python’s execution model
Names and namespaces
Scopes
Guidelines for writing good code
Python culture
A note on IDEs
A word about AI
Summary
Built-In Data Types
Everything is an object
Mutability
Numbers
Integers
Booleans
Real numbers
Complex numbers
Fractions and decimals
Immutable sequences
Strings and bytes
Encoding and decoding strings
Indexing and slicing strings
String formatting
Tuples
Mutable sequences
Lists
Bytearrays
Set types
Mapping types: dictionaries
Data types
Dates and times
The standard library
Third-party libraries
The collections module
namedtuple
defaultdict
ChainMap
Enums
Final considerations
Small value caching
How to choose data structures
About indexing and slicing
About names
Summary
Conditionals and Iteration
Conditional programming
The if statement
A specialized else: elif
Nesting if statements
The ternary operator
Pattern matching
Looping
The for loop
Iterating over a range
Iterating over a sequence
Iterators and iterables
Iterating over multiple sequences
The while loop
The break and continue statements
A special else clause
Assignment expressions
Statements and expressions
Using the walrus operator
A word of warning
Putting all this together
A prime generator
Applying discounts
A quick peek at the itertools module
Infinite iterators
Iterators terminating on the shortest input sequence
Combinatoric generators
Summary
Functions, the Building Blocks of Code
Why use functions?
Reducing code duplication
Splitting a complex task
Hiding implementation details
Improving readability
Improving traceability
Scopes and name resolution
The global and nonlocal statements
Input parameters
Argument-passing
Assignment to parameter names
Changing a mutable object
Passing arguments
Positional arguments
Keyword arguments
Iterable unpacking
Dictionary unpacking
Combining argument types
Defining parameters
Optional parameters
Variable positional parameters
Variable keyword parameters
Positional-only parameters
Keyword-only parameters
Combining input parameters
More signature examples
Avoid the trap! Mutable defaults
Return values
Returning multiple values
A few useful tips
Recursive functions
Anonymous functions
Function attributes
Built-in functions
Documenting your code
Importing objects
Relative imports
One final example
Summary
Comprehensions and Generators
The map, zip, and filter functions
map
zip
filter
Comprehensions
Nested comprehensions
Filtering a comprehension
Dictionary comprehensions
Set comprehensions
Generators
Generator functions
Going beyond next
The yield from expression
Generator expressions
Some performance considerations
Do not overdo comprehensions and generators
Name localization
Generation behavior in built-ins
One last example
Summary
OOP, Decorators, and Iterators
Decorators
A decorator factory
OOP
The simplest Python class
Class and object namespaces
Attribute shadowing
The self argument
Initializing an instance
OOP is about code reuse
Inheritance and composition
Accessing a base class
Multiple inheritance
Method resolution order
Class and static methods
Static methods
Class methods
Private methods and name mangling
The property decorator
The cached_property decorator
Operator overloading
Polymorphism—a brief overview
Data classes
Writing a custom iterator
Summary
Exceptions and Context Managers
Exceptions
Raising exceptions
Defining your own exceptions
Tracebacks
Handling exceptions
Exception groups
Not only for errors
Context managers
Class-based context managers
Generator-based context managers
Summary
Files and Data Persistence
Working with files and directories
Opening files
Using a context manager to open a file
Reading from and writing to a file
Reading and writing in binary mode
Protecting against overwriting an existing file
Checking for file and directory existence
Manipulating files and directories
Manipulating pathnames
Temporary files and directories
Directory content
File and directory compression
Data interchange formats
Working with JSON
Custom encoding/decoding with JSON
I/O, streams, and requests
Using an in-memory stream
Making HTTP requests
Persisting data on disk
Serializing data with pickle
Saving data with shelve
Saving data to a database
Configuration files
Common formats
The INI configuration format
The TOML configuration format
Summary
Cryptography and Tokens
The need for cryptography
Useful guidelines
Hashlib
HMAC
Secrets
Random objects
Token generation
Digest comparison
JSON Web Tokens
Registered claims
Time-related claims
Authentication-related claims
Using asymmetric (public key) algorithms
Useful references
Summary
Testing
Testing your application
The anatomy of a test
Testing guidelines
Unit testing
Writing a unit test
Mock objects and patching
Assertions
Testing a CSV generator
Boundaries and granularity
Testing the export function
Final considerations
Test-driven development
Summary
Debugging and Profiling
Debugging techniques
Debugging with print
Debugging with a custom function
Using the Python debugger
Inspecting logs
Other techniques
Reading tracebacks
Assertions
Where to find information
Troubleshooting guidelines
Where to inspect
Using tests to debug
Monitoring
Profiling Python
When to profile
Measuring execution time
Summary
Introduction to Type Hinting
Python approach to types
Duck typing
History of type hinting
Benefits of type hinting
Type annotations
Annotating functions
The Any type
Type aliases
Special forms
Optional
Union
Generics
Annotating variables
Annotating containers
Annotating tuples
Fixed-length tuples
Tuples with named fields
Tuples of arbitrary length
Abstract base classes (ABCs)
Special typing primitives
The Self type
Annotating variable parameters
Protocols
The Mypy static type checker
Some useful resources
Summary
Data Science in Brief
IPython and Jupyter Notebook
Using Anaconda
Starting a Notebook
Dealing with data
Setting up the Notebook
Preparing the data
Cleaning the data
Creating the DataFrame
Unpacking the campaign name
Unpacking the user data
Renaming columns
Computing some metrics
Cleaning everything up
Saving the DataFrame to a file
Visualizing the results
Where do we go from here?
Summary
Introduction to API Development
The Hypertext Transfer Protocol
How does HTTP work?
Response status codes
APIs – An introduction
What is an API?
What is the purpose of an API?
API protocols
API data-exchange formats
The railway API
Modeling the database
Main setup and configuration
Application settings
Station endpoints
Reading data
Creating data
Updating data
Deleting data
User authentication
Documenting the API
Where do we go from here?
Summary
CLI Applications
Command-line arguments
Positional arguments
Options
Sub-commands
Argument parsing
Building a CLI client for the railway API
Interacting with the railway API
Creating the command-line interface
Configuration files and secrets
Creating sub-commands
Implementing sub-commands
Other resources and tools
Summary
Packaging Python Applications
The Python Package Index
Packaging with Setuptools
Project layout
Development installation
Changelog
License
README
pyproject.toml
Package metadata
Versioning and dynamic metadata
Specifying dependencies
Project URLs
Scripts and entry points
Defining the package contents
Accessing metadata in your code
Building and publishing packages
Building
Publishing
Advice for starting new projects
Other files
Alternative tools
Further reading
Summary
Programming Challenges
Advent of Code
Camel Cards
Part one – problem statement
Part one – solution
Part two – problem statement
Part two – solution
Cosmic Expansion
Part one – problem statement
Part one – solution
Part two – problem statement
Part two – solution
Final considerations
Other programming challenge websites
Summary
Other Books You May Enjoy
Index
Landmarks
Cover
Index
Preface
It is our great pleasure to introduce you to the fourth edition of our book. The first one came out in 2015, and since then the book has been a top seller all around the world.
Ten years ago, being a programmer meant working with certain tools, implementing certain paradigms. Today, the landscape is different. Developers tend to be even more specialized than before, and there is great focus on things like APIs, and distributed applications. We have tried to capture the current trends, and to offer you the best foundational layer we could think of, drawing from our experience as developers who work in a fast-paced industry.
Each new edition has brought about some kind of change. Obsolete chapters were removed, new ones were added, and others again have been amended to reflect the modern ways in which software is written.
This edition features three new chapters. The first one discusses the topic of type hinting. Type hinting is not new to Python, but now we feel it has become such an established practice that the book wouldn’t have been complete without it.
The second one, which discusses the topic of CLI applications, steps in by replacing an old chapter that was about GUIs. Most of what we do with a computer today happens in a browser, and many desktop applications have been built, or rewritten, by leveraging browser components, so we felt that a whole chapter dedicated to GUIs was perhaps a bit obsolete.
Finally, the third one explores the topic of competitive programming.
The remaining chapters have been updated to reflect the latest additions to the language, and improved to make the presentation even simpler and more fluid, while still aiming at offering interesting examples to the reader.
The soul of the book, its essence, is still intact. It shouldn’t feel like yet another Python book. It is, first and foremost, about programming. It tries to convey as much information as possible and, sometimes, when the page count couldn’t allow it, it points to the resources you need to further your knowledge.
It is designed to last. It explains concepts and information in a way that should stand the test of time, for as long as possible. There is great amount of work, thinking, and meetings, that goes into making sure of that.
It is also radically different than its first edition. It is much more mature, more professional, and focuses more on the language and slightly less on projects. We think the line that strikes the balance between these two parts has been drawn in the right place.
It will require you to focus and work hard. All the code is available for download. You can clone the repository from GitHub, if you like. Please check it out. It will help you cement what you will learn when reading these pages. Code is not a static thing. It is very much alive. It changes, it morphs. You will learn much more if you take the time to explore it, change it, and break it. We have left several guidelines in the book, to help you do that.
In closing, I want to express my gratitude to my co-author, Heinrich.
This book is now as much his as it is mine. Every chapter is infused with his talent, his creativity, and the depth of his knowledge. He is also gifted with tremendous memory: he can spot a redundant piece of information in chapters that are 200 pages apart. I can’t do that.
Like me, he has spent many long nights and weekends making sure everything was presented in the best possible way. It is because I have shared this journey with him, that I have so much confidence in the quality of this work.
Our advice for you is to study these pages well, and experiment with the source code. Once you are confident in your Python skills, please don’t stop learning. Try to go beyond the language, transcend it. A senior developer should know certain concepts and master certain skills that cannot be contained within one language, it’s just not possible. Studying other languages helps to learn how to discriminate between those features that pertain to a certain language, and others that are instead more generic, related to programming. Hopefully this book will help you get there.
Enjoy the journey and, whatever you learn, please share it with others.
Fabrizio
Who this book is for
This book is for people who have some programming experience, but not necessarily with Python. Some knowledge of basic programming concepts will be useful, although it is not a strict requirement.
Even if you already have some experience with Python, this book can still be useful to you, both as a reference to Python’s fundamentals, and for providing a wide range of considerations and suggestions collected over four combined decades of experience.
What this book covers
Chapter 1, A Gentle Introduction to Python, introduces you to fundamental programming concepts and constructs of the Python language. It also guides you through getting Python up and running on your computer.
Chapter 2, Built-In Data Types, introduces you to Python built-in data types. Python has a very rich set of native data types, and this chapter will give you a description and examples for each of them.
Chapter 3, Conditionals and Iteration, teaches you how to control the flow of the code by inspecting conditions, applying logic, and performing loops.
Chapter 4, Functions, the Building Blocks of Code, teaches you how to write functions. Functions are essential to code reuse, to reducing debugging time, and, in general, to writing higher quality code.
Chapter 5, Comprehensions and Generators, introduces you to the functional aspects of Python programming. This chapter teaches you how to write comprehensions and generators, which are powerful tools that you can use to write faster, more concise code, and save memory.
Chapter 6, OOP, Decorators, and Iterators, teaches you the basics of object-oriented programming with Python. It shows you the key concepts and all the potentials of this paradigm. It also shows you one of the most useful features of the language: decorators.
Chapter 7, Exceptions and Context Managers, introduces the concept of exceptions, which represent errors that occur in applications, and how to handle them. It also covers context managers, which are very useful when dealing with resources.
Chapter 8, Files and Data Persistence, teaches you how to deal with files, streams, data interchange formats, and databases.
Chapter 9, Cryptography and Tokens, touches upon the concepts of security, hashes, encryption, and tokens, which are essential for writing secure software.
Chapter 10, Testing, teaches you the fundamentals of testing, and guides you through a few examples on how to test your code, in order to make it more robust, fast and reliable.
Chapter 11, Debugging and Profiling, shows you the main methods for debugging and profiling code and some examples of how to apply them.
Chapter 12, Introduction to Type Hinting, guides you through the syntax and main concepts of type hinting. Type hinting has become more and more popular in recent years because it enriches both the language and the tools that are part of its ecosystem.
Chapter 13, Data Science in Brief, illustrates a few key concepts by means of a comprehensive example, using the powerful Jupyter Notebook.
Chapter 14, Introduction to API Development, introduces API development using the FastAPI framework.
Chapter 15, CLI Applications, introduces command-line interface applications. They are run in a console or terminal, and are a common and natural way in which developers write several of their own day-to-day tools.
Chapter 16, Packaging Python Applications, guides you through the process of preparing a project to be published, and shows you how to upload the result on the Python Package Index (PyPI).
Chapter 17, Programming Challenges, introduces the concept of competitive programming, by showing you how to solve two problems from the Advent of Code website.
To get the most out of this book
You are encouraged to follow the examples in this book. You will need a computer, an internet connection, and a browser. The book is written for Python 3.12, but it should also work, for the most part, with any recent version of Python 3. We have given guidelines on how to install
Python
on your operating system. The procedures to do that normally get out of date quickly, so we recommend you refer to the most up-to-date guide on the web to find precise setup instructions. We have also explained how to install all the extra libraries used in the various chapters. No particular editor is required to type the code; however, we suggest that those who are interested in following the examples should consider adopting a proper coding environment. We have offered suggestions on this matter in the first chapter.
Download the color images
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781835882948.
Download the example code files
The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Learn-Python-Programming-Fourth-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Conventions used
There are a number of text conventions used throughout this book.
CodeInText
: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: "The
as_integer_ratio()
method has also been added to integers and Booleans."
Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: "They are immutable sequences of Unicode code points."
Warnings or important notes appear like this.
Tips and tricks appear like this.
Get in touch
Feedback from our readers is always welcome.
General feedback: Email
and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at
.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packtpub.com/submit-errata, click Submit Errata, and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at
with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.
Share your thoughts
Once you’ve read Learn Python Programming, Fourth Edition, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.
Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.
Download a free PDF copy of this book
Thanks for purchasing this book!
Do you like to read on the go but are unable to carry your print books everywhere?
Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily.
Follow these simple steps to get the benefits:
Scan the QR code or visit the link below:
https://packt.link/free-ebook/9781835882948
Submit your proof of purchase.
That’s it! We’ll send your free PDF and other benefits to your email directly.
1
A Gentle Introduction to Python
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
—Chinese proverb
Computer programming, or coding, as it is sometimes known, is telling a computer to do something using a language it understands.
Computers are very powerful tools, but unfortunately, they cannot think for themselves. They need to be told everything: how to perform a task; how to evaluate a condition to decide which path to follow; how to handle data that comes from a device, such as a network or a disk; and how to react when something unforeseen happens, in the case of, say, something being broken or missing.
You can code in many different styles and languages. Is it hard? We would say yes and no. It is a bit like writing—it is something that everybody can learn. But what if you want to become a poet? Writing alone is not enough. You have to acquire a whole other set of skills, and this will involve a longer and greater effort.
In the end, it all comes down to how far you want to go down the road. Coding is not just putting together some instructions that work. It is so much more!
Good code is short, fast, elegant, easy to read and understand, simple, easy to modify and extend, easy to scale and refactor, and easy to test. It takes time to be able to write code that has all these qualities at the same time, but the good news is that you are taking the first step toward it at this very moment by reading this book. And we have no doubt you can do it. Anyone can; in fact, we all program all the time, only we are not aware of it.
Let’s say, for example, that you want to make instant coffee. You have to get a mug, the instant coffee jar, a teaspoon, water, and a kettle. Even if you are not aware of it, you are evaluating a lot of data. You are making sure that there is water in the kettle and that the kettle is plugged in, that the mug is clean, and that there is enough coffee in the jar. Then you boil the water and, maybe in the meantime, you put some coffee in the mug. When the water is ready, you pour it into the mug, and stir.
So, how is this programming?
Well, we gathered resources (the kettle, coffee, water, teaspoon, and mug) and we verified some conditions concerning them (the kettle is plugged in, the mug is clean, and there is enough coffee). Then we started two actions (boiling the water and putting coffee in the mug), and when both of them were completed, we finally ended the procedure by pouring water into the mug and stirring.
Can you see the parallel? We have just described the high-level functionality of a coffee program. It was not that hard because this is what the brain does all day long: evaluate conditions, decide to take actions, carry out tasks, repeat some of them, and stop at some point.
All you need now is to learn how to deconstruct all those actions you do automatically in real life so that a computer can actually make some sense of them. You need to learn a language as well so that the computer can be instructed.
So, this is what this book is for. We will show you one way in which you can code successfully, and we will try to do that by means of many simple but focused examples (our favorite kind).
In this chapter, we are going to cover the following:
Python’s characteristics and ecosystem
Guidelines on how to get up and running with Python and virtual environments
How to run Python programs
How to organize Python code and its execution model
A brief introduction to programming
We love to make references to the real world when we teach coding; we believe they help people to better retain the concepts they are learning. However, now is the time to be a bit more rigorous and see what coding is from a more technical perspective.
When we write code, we are instructing a computer about the things it has to do. Where does the action happen? In many places: the computer memory, hard drives, network cables, the CPU, and so on. It is a whole world, which most of the time is the representation of a subset of the real world.
If you write a piece of software that allows people to buy clothes online, you will have to represent real people, real clothes, real brands, sizes, and so on and so forth, within the boundaries of a program.
To do this, you will need to create and handle objects in your program. A person can be an object. A car is an object. A pair of trousers is an object. Luckily, Python understands objects very well.
The two key features any object has are properties and methods. Let us take the example of a person as an object. Typically, in a computer program, you will represent people as customers or employees. The properties that you store against them are things like a name, a social security number, an age, whether they have a driving license, an email, and so on. In a computer program, you store all the data needed in order to use an object for the purpose that needs to be served. If you are coding a website to sell clothes, you probably want to store the heights and weights as well as other measures of your customers so that the appropriate clothes can be suggested to them. So, properties are characteristics of an object. We use them all the time: Could you pass me that pen? —Which one? —The black one. Here, we used the color (black) property of a pen to identify it (most likely it was being kept alongside different colored pens for the distinction to be necessary).
Methods are actions that an object can perform. As a person, I have methods such as speak, walk, sleep, wake up, eat, dream, write, read, and so on. All the things that I can do could be seen as methods of the objects that represent me.
So, now that you know what objects are, that they provide methods that can be run and properties that you can inspect, you are ready to start coding. Coding, in fact, is simply about managing those objects that live in the subset of the world we’re reproducing in our software. You can create, use, reuse, and delete objects as you please.
According to the Data Model chapter on the official Python documentation (https://docs.python.org/3/reference/datamodel.html):
Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects.
We will take a closer look at Python objects in Chapter 6, OOP, Decorators, and Iterators. For now, all we need to know is that every object in Python has an ID (or identity), a type, and a value.
Object-Oriented Programming (OOP) is just one of many programming paradigms. In Python, we can write code using a functional or imperative style, as well as object-oriented. However, as we previously stated, everything in Python is an object, therefore we employ them all the time, regardless of the chosen coding style.
Once created, the ID of an object never changes. It is a unique identifier for it, and it is used behind the scenes by Python to retrieve the object when we want to use it. The type also never changes. The type states what operations are supported by the object and the possible values that can be assigned to it. We will see Python’s most important data types in Chapter 2, Built-In Data Types. The value of some objects can change. Such objects are said to be mutable. If the value cannot be changed, the object is said to be immutable.
How, then, do we use an object? We give it a name, of course! When you give an object a name, then you can use the name to retrieve the object and use it. In a more generic sense, objects, such as numbers, strings (text), and collections, are associated with a name. In other languages, the name is normally called a variable. You can see the variable as being like a box, which you can use to hold data.
Objects represent data. It is stored in databases or sent over network connections. It is what you see when you open any web page, or work on a document. Computer programs manipulate that data to perform all sorts of actions. They regulate its flow, evaluate conditions, react to events, and much more.
To do all this, we need a language. That is what Python is for. Python is the language we will use together throughout this book to instruct the computer to do something for us.
Enter the Python
Python is the marvelous creation of Guido Van Rossum, a Dutch computer scientist and mathematician who decided to gift the world with a project he was playing around with over Christmas 1989. The language appeared to the public somewhere around 1991, and since then has evolved to be one of the leading programming languages used worldwide today.
We (the authors) started programming when we were both very young. Fabrizio started at the age of 7, on a Commodore VIC-20, which was later replaced by its bigger brother, the Commodore 64. The language it used was BASIC. Heinrich started when he learned Pascal in high school. Between us, we’ve programmed in Pascal, Assembly, C, C++, Java, JavaScript, Visual Basic, PHP, ASP, ASP .NET, C#, and plenty of others we can’t even remember; only when we landed on Python did we finally get the feeling that you go through when you find the right couch in the shop, when all of your body is yelling: Buy this one! This one is perfect!
It took us about a day to become accustomed to it. Its syntax is a bit different from what we were used to, but after getting past that initial feeling of discomfort (like having new shoes), we both just fell in love with it. Deeply. Let us see why.
About Python
Before we get into the gory details, let us get a sense of why someone would want to use Python. It embodies the following qualities.
Portability
Python runs everywhere, and porting a program from Linux to Windows or Mac is usually just a matter of fixing paths and settings. Python is designed for portability, and it takes care of specific operating system (OS) quirks behind interfaces that shield you from the pain of having to write code tailored to a specific platform.
Coherence
Python is extremely logical and coherent. You can see it was designed by a brilliant computer scientist. Most of the time, you can just guess what a method is called if you do not know it.
You may not realize how important this is right now, especially if you are not that experienced as a programmer, but this is a major feature. It means less clutter in your head, as well as less skimming through the documentation, and less need for mappings in your brain when you code.
Developer productivity
According to Mark Lutz (Learning Python, 5th Edition, O’Reilly Media), a Python program is typically one-fifth to one-third the size of equivalent Java or C++ code. This means the job gets done faster. And faster is good. Faster means being able to respond more quickly to the market. Less code not only means less code to write, but also less code to read (and professional coders read much more than they write), maintain, debug, and refactor.
Another important aspect is that Python runs without the need for lengthy and time-consuming compilation and linkage steps, so there is no need to wait to see the results of your work.
An extensive library
Python has an incredibly extensive standard library (it is said to come with batteries included). If that wasn’t enough, the Python international community maintains a body of third-party libraries, tailored to specific needs, which you can access freely at the Python Package Index (PyPI). When you code in Python and realize that a certain feature is required, in most cases, there is at least one library where that feature has already been implemented.
Software quality
Python is heavily focused on readability, coherence, and quality. The language’s uniformity allows high readability, and this is crucial nowadays, as coding is more of a collective effort than a solo endeavor. Another important aspect of Python is its intrinsic multiparadigm nature. You can use it as a scripting language, but you can also employ object-oriented, imperative, and functional programming styles—it is extremely versatile.
Software integration
Another important aspect is that Python can be extended and integrated with many other languages, which means that even when a company is using a different language as their mainstream tool, Python can come in and act as a gluing agent between complex applications that need to talk to each other in some way. This is more of an advanced topic, but in the real world, this feature is important.
Data science
Python is among the most popular (if not the most popular) languages used in the fields of data science, machine learning, and artificial intelligence today. Knowledge of Python is therefore almost essential for those who want to have a career in these fields.
Satisfaction and enjoyment
Last, but by no means least, there is the fun of it! Working with Python is fun; we can code for eight hours and leave the office happy and satisfied, unaffected by the struggle other coders have to endure because they use languages that do not provide them with the same amount of well-designed data structures and constructs. Python makes coding fun, no doubt about it, and fun promotes motivation and productivity.
These are the major reasons why we would recommend Python to everyone. Of course, there are many other technical and advanced features that we could have mentioned, but they do not really pertain to an introductory section like this one. They will come up naturally, chapter after chapter, as we learn about Python in greater detail.
Now, let’s look at what the potential limitations of Python are.
What are the drawbacks?
Aside from personal preferences, the primary drawback of Python lies in its execution speed. Typically, Python is slower than its compiled siblings. The standard implementation of Python produces, when you run an application, a compiled version of the source code called byte code (with the extension
.pyc
), which is then run by the Python interpreter. The advantage of this approach is portability, which we pay for with increased runtimes because Python is not compiled down to the machine level, as other languages are.
Despite this, Python speed is rarely a problem today, hence its wide use regardless of this downside. What happens is that, in real life, hardware cost is no longer a problem, and usually you can gain speed by parallelizing tasks. Moreover, many programs spend a great proportion of the time waiting for I/O operations to complete; therefore, the raw execution speed is often a secondary factor to the overall performance.
It is worth noting that Python’s core developers have put great effort into speeding up operations on the most common data structures in the last few years. This effort, in some cases very successful, has somewhat alleviated this issue.
In situations where speed really is crucial, one can switch to faster Python implementations, such as PyPy, which provides, on average, just over a four-fold speedup by implementing advanced compilation techniques (check https://pypy.org/ for reference). It is also possible to write performance-critical parts of your code in faster languages, such as C or C++, and integrate that with your Python code. Libraries such as pandas and NumPy (which are commonly used for doing data science in Python) use such techniques.
There are a few different implementations of the Python language. In this book, we will use the reference implementation, known as CPython. You can find a list of other implementations at https://www.python.org/download/alternatives/.
If that is not convincing enough, you can always consider that Python has been used to drive the backend of services such as Spotify and Instagram, where performance is a concern. From this, it can be seen that Python has done its job perfectly well.
Who is using Python today?
Python is used in many different contexts, such as system programming, web and API programming, GUI applications, gaming and robotics, rapid prototyping, system integration, data science, database applications, real-time communication, and much more. Several prestigious universities have also adopted Python as their main language in computer science courses.
Here is a list of major companies and organizations that are known to use Python in their technology stack, product development, data analysis, or automation processes:
Tech industry
Google: Uses Python for many tasks including backend services, data analysis, and artificial intelligence (AI)
Facebook: Utilizes Python for various purposes, including infrastructure management and operational automation
Instagram: Relies heavily on Python for its backend, making it one of the largest Django (a Python web framework) users
Spotify: Employs Python mainly for data analysis and backend services
Netflix: Uses Python for data analysis, operational automation, and security
Financial sector
JP Morgan Chase: Uses Python for financial models, data analysis, and algorithmic trading
Goldman Sachs: Employs Python for various financial models and applications
Bloomberg: Uses Python for financial data analysis and its Bloomberg Terminal interface
Technology and software
IBM: Utilizes Python for AI, machine learning, and cybersecurity
Intel: Uses Python for hardware testing and development processes
Dropbox: The desktop client is largely written in Python
Space and research
NASA: Uses Python for various purposes, including data analysis and system integration
CERN: Employs Python for data processing and analysis in physics experiments
Retail and e-Commerce
Amazon: Uses Python for data analysis, product recommendations, and operational automation
eBay: Utilizes Python for various backend services and data analysis
Entertainment and media
Pixar: Uses Python for animation software and scripting in the animation process
Industrial Light & Magic (ILM): Employs Python for visual effects and image processing
Education and learning platforms
Coursera: Utilizes Python for web development and backend services
Khan Academy: Uses Python for educational content delivery and backend services
Government and non-profit
The United States Federal Government: Has various departments and agencies using Python for data analysis, cybersecurity, and automation
The Raspberry Pi Foundation: Uses Python as a primary programming language for educational purposes and projects
Setting up the environment
On our machines (MacBook Pro), this is the latest Python version:
>>> import sys
>>> print(sys.version)
3.12.2 (main, Feb 14 2024, 14:16:36) [Clang 15.0.0 (clang-1500.1.0.2.5)]
So, you can see that the version is 3.12.2, which was out on October 2, 2023. The preceding text is a little bit of Python code that was typed into a console. We will talk about this in a moment.
All the examples in this book will be run using Python 3.12. If you wish to follow the examples and download the source code for this book, please make sure you are using the same version.
Installing Python
The process of installing Python on your computer depends on the operating system you have. First of all, Python is fully integrated and, most likely, already installed in almost every Linux distribution. If you have a recent version of macOS, it is likely that Python 3 is already there as well, whereas if you are using Windows, you probably need to install it.
Regardless of Python being already installed in your system, you will need to make sure that you have version 3.12 installed.
To check if you have Python already installed on your system, try typing
python --version
or
python3 --version
in a command prompt (more on this later).
The place you want to start is the official Python website: https://www.python.org. This website hosts the official Python documentation and many other resources that you will find very useful.
Useful installation resources
The Python website hosts useful information regarding the installation of Python on various operating systems. Please refer to the relevant page for your operating system.
Windows and macOS:
https://docs.python.org/3/using/windows.html
https://docs.python.org/3/using/mac.html
For Linux, please refer to the following links:
https://docs.python.org/3/using/unix.html
https://ubuntuhandbook.org/index.php/2023/05/install-python-3-12-ubuntu/
Installing Python on Windows
As an example, this is the procedure to install Python on Windows. Head to https://www.python.org/downloads/ and download the appropriate installer according to the CPU of your computer.
Once you have it, you can double-click on it in order to start the installation.
Figure 1.1: Starting the installation process on Windows
We recommend choosing the default install, and NOT ticking the Add python.exe to PATH option to prevent clashes with other versions of Python that might be installed on your machine, potentially by other users.
For a more comprehensive set of guidelines, please refer to the link indicated in the previous paragraph.
Once you click on Install Now, the installation procedure will begin.
Figure 1.2: Installation in progress
Once the installation is complete, you will land on the final screen.
Figure 1.3: Installation complete
Click on Close to finish the installation.
Now that Python is installed on your system, open a command prompt and run the Python interactive shell by typing
py
. This command will select the latest version of Python installed on your machine. At the time of writing, 3.12 is the latest available version of Python. If you have a more recent version installed, you can specify the version with the command
py -3.12
.
To open the command prompt in Windows, go to the Start menu and type
cmd
in the search box to start your terminal up. Alternatively, you can also use Powershell.
Installing Python on macOS
On macOS, the installation procedure is similar to that of Windows. Once you have downloaded the appropriate installer for your machine, complete the installation steps, and then start a terminal by going to Applications > Utilities > Terminal. Alternatively, you can install it through Homebrew.
Once in the terminal window, you can type
python
. If that launches the wrong version, you can try and specify the version with either
python3
or
python3.12
.
Installing Python on Linux
The process of installing Python on Linux is normally a bit more complex than that for Windows or macOS. The best course of action, if you are on a Linux machine, is to search for the most up-to-date set of steps for your distribution online. These will likely be quite different from one distribution to another, so it is difficult to give an example that would be relevant for everyone. Please refer to the link in the Useful installation resources section for guidance.
The Python console
We will use the term console interchangeably to indicate the Linux console, the Windows Command Prompt or Powershell, and the macOS Terminal. We will also indicate the command-line prompt with the default Linux format, like this:
$ sudo apt-get update
If you are not familiar with that, please take some time to learn the basics of how a console works. In a nutshell, after the
$
sign, you will type your instructions. Pay attention to capitalization and spaces, as they are very important.
Whatever console you open, type
python
at the prompt (
py
on Windows) and make sure the Python interactive shell appears. Type
exit()
to quit. Keep in mind that you may have to specify
python3
or
python3.12
if your OS comes with other Python versions preinstalled.
We often refer to the Python interactive shell simply as the Python console.
This is roughly what you should see when you run Python (some details will change according to the version and OS):
$ python Python 3.12.2 (main, Feb 14 2024, 14:16:36) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin Type help
, copyright
, credits
or license
for more information. >>>
Now that Python is set up and you can run it, it is time to make sure you have the other tool that will be indispensable to follow the examples in the book: a virtual environment.
About virtual environments
When working with Python, it is very common to use virtual environments. Let us see what they are and why we need them by means of a simple example.
You install Python on your system, and you start working on a website for client X. You create a project folder and start coding. Along the way, you also install some libraries, for example, the Django framework. Let us say the Django version you installed for Project X is 4.2.
Now, your website is so good that you get another client, Y. She wants you to build another website, so you start Project Y and, along the way, you need to install Django again. The only issue is that now the Django version is 5.0 and you cannot install it on your system because this would replace the version you installed for Project X. You do not want to risk introducing incompatibility issues, so you have two choices: either you stick with the version you have currently on your machine, or you upgrade it and make sure the first project is still fully working correctly with the new version.
Let us be honest; neither of these options is very appealing, right? Definitely not. But there is a solution: virtual environments!
Virtual environments are isolated Python