Python Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #3
By Harry Yoon
()
About this ebook
Learn Python in a Weekend!
This book is an (informal) language reference on the Python programming language. Python is one of the most widely used languages in many different application areas. We go through all essential features of the modern Python programming language, including the match statement (3.10) and exception groups (3.11).
Although the book is written as a reference, you can read it more or less from beginning to end and you should be able to get the overall picture of the Python language if you have some prior experience with programming in Python.
The book covers
- Python program top-level components.
- Python package/module import system.
- Builtin type hierarchy. Data model.
- List, map, tuple literals.
- Expressions. Simple and compound statements.
- Function, class definitions.
- Object oriented programming in Python.
- Structural pattern matching.
- Coroutines, async/await.
Order your copy today and learn Python essentials this weekend!
Other titles in Python Mini Reference Series (7)
Golang Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #1 Rating: 0 out of 5 stars0 ratingsC# Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #2 Rating: 0 out of 5 stars0 ratingsPython Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #3 Rating: 0 out of 5 stars0 ratingsTypescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4 Rating: 0 out of 5 stars0 ratingsRust Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #5 Rating: 0 out of 5 stars0 ratingsHaskell Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #10 Rating: 0 out of 5 stars0 ratingsLua Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #12 Rating: 0 out of 5 stars0 ratings
Read more from Harry Yoon
Einstein's Relativity: The Special Theory and the General Theory - Free Chapter 2 Rating: 0 out of 5 stars0 ratingsThe Art of Go - Basics: Introduction to Programming in Go: Read and Learn Programming Rating: 0 out of 5 stars0 ratingsEinstein's Seven Essays on Relativity, Geometry, and Scientific Methods Rating: 0 out of 5 stars0 ratingsEinstein's Relativity Part I - The Special Theory of Relativity Rating: 0 out of 5 stars0 ratingsEinstein's Relativity: The Special Theory and the General Theory - Free Chapter 3 Rating: 0 out of 5 stars0 ratingsEinstein's Relativity: The Special Theory and the General Theory - Free Chapter 1 Rating: 0 out of 5 stars0 ratings
Related to Python Mini Reference
Titles in the series (7)
Golang Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #1 Rating: 0 out of 5 stars0 ratingsC# Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #2 Rating: 0 out of 5 stars0 ratingsPython Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #3 Rating: 0 out of 5 stars0 ratingsTypescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4 Rating: 0 out of 5 stars0 ratingsRust Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #5 Rating: 0 out of 5 stars0 ratingsHaskell Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #10 Rating: 0 out of 5 stars0 ratingsLua Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #12 Rating: 0 out of 5 stars0 ratings
Related ebooks
Essential Python 3 Rating: 0 out of 5 stars0 ratingsThe Ultimate Python Programming Guide For Beginner To Intermediate Rating: 4 out of 5 stars4/5Python Handbook For Beginners. A Hands-On Crash Course For Kids, Newbies and Everybody Else Rating: 0 out of 5 stars0 ratingsBeginner's guide to mastering python Rating: 0 out of 5 stars0 ratingsPython for Developers: Learn to Develop Efficient Programs using Python Rating: 0 out of 5 stars0 ratingsPython Made Simple: Learn Python programming in easy steps with examples Rating: 5 out of 5 stars5/5Python Programming: Your Beginner Guide To Learn Python in 7 Days Rating: 4 out of 5 stars4/5Python Essentials Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python Rating: 0 out of 5 stars0 ratingsMastering Objectoriented Python Rating: 5 out of 5 stars5/5Scripting Superpack For Beginners: Python, Powershell, Bash, And Java Mastery Rating: 0 out of 5 stars0 ratingsPython for Beginners: A Crash Course to Learn Python Programming in 1 Week Rating: 0 out of 5 stars0 ratingsBasic Core Python Programming: A Complete Reference Book to Master Python with Practical Applications (English Edition) Rating: 0 out of 5 stars0 ratingsPython: Beginner's Guide to Programming Code with Python: Python Computer Programming, #1 Rating: 0 out of 5 stars0 ratingsPython 3 Programming: A Beginner Crash Course Guide to Learn Python 3 in 1 Week Rating: 3 out of 5 stars3/5Mastering Python Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsPYTHON PROGRAMMING Rating: 4 out of 5 stars4/5Python All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsPython Programming Rating: 0 out of 5 stars0 ratingsGet Programming: Learn to code with Python Rating: 0 out of 5 stars0 ratingsPythonic AI: A beginner's guide to building AI applications in Python (English Edition) Rating: 5 out of 5 stars5/5Python For Kids For Dummies Rating: 0 out of 5 stars0 ratingsPYTHON FOR BEGINNERS: Master the Basics of Python Programming and Start Writing Your Own Code in No Time (2023 Guide for Beginners) Rating: 0 out of 5 stars0 ratings
Programming For You
Python: Learn Python in 24 Hours 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/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Algorithms For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5JavaScript All-in-One For Dummies 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 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Problem Solving in C and Python: Programming Exercises and Solutions, Part 1 Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5PYTHON PROGRAMMING Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Learn Python in 10 Minutes Rating: 4 out of 5 stars4/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsBeginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5
Reviews for Python Mini Reference
0 ratings0 reviews
Book preview
Python Mini Reference - Harry Yoon
Preface
Python is a dynamic language.
It means many different things. It means that the language is more flexible. It means that Python is an easier language to program with. You can quickly write a simple program without having to go through too much rituals
, compared to many other programming languages. Python scripts tend to require less boilerplate code. Python gives you more freedom.
On the other hand, it also means that the language is less safe, and more error prone. It means that it is harder to build a larger system with Python. It means that software written in Python is generally harder to maintain over a longer period of time.
The key to using Python effectively is to understand this tradeoff. Python can be an ideal language, for instance, for quick prototyping, or scripting
. On the flip side, Python is not as much used for the enterprise software
development.
Python is a general purpose programming language. Python is used in many different application areas, from system administration tasks to web application development. Python is now one of the most widely used programming languages for scientists, who have traditionally been using more high-level tools like Matlab. Python provides an easier upgrade
path to these non-professional
programmers. Now, Python is becoming the language for machine learning and data science.
Python is beginner-friendly. In fact, it seems to be the most favorite first language for beginning programmers, even more so than JavaScript.
It should be noted, however, that Python is not a simple language. Over the past 30 years or so of its history, it has gone through a lot of big and small changes. It is still easy to get started with programming in Python. Nonetheless, once you reach a certain point, say, from the advanced beginners to intermediate level, its complexity can be overwhelming.
This book will give you a broad and sanitized overview of the Python language, as of its most recent incarnation (e.g., 3.10 and 3.11). This book can be useful to anyone who has been dabbling with Python without solid foundation. It can also be useful to the people who have experience with other programming languages and want to get some quick overview of the language.
This book is written as an (informal) language reference. The goal of this book is not to teach you how to program effectively in Python, but rather to provide a concise summary of the language grammar. If you have some basic programming knowledge, you can read this book more or less from beginning to end, and you should be able to get the overall understanding of the Python programming language quickly, regardless of your particular programming background.
The book is written for a broad audience, but one caveat is that there are a fair amount of cross references, unlike in the books written in a tutorial style. If you have no prior experience with programming in Python or any similar language, then you may find it a little difficult to go through some parts of the book. This book is not for complete beginners. It skims through some elementary concepts in the beginning, for the sake of brevity, so that we can focus more on the intermediate and advanced level topics.
This book is not an authoritative language reference. For that, we recommend the readers to refer to the official language specification.
1. Introduction
This book will give you an overview of the Python language grammar.
It appears that the line between the programming language proper and the standard library is becoming more blurred these days. Although it is not our goal to go through the Python standard library (which is well beyond the scope of this book), we will touch upon a few important concepts from the standard library modules that are considered more or less part of the language.
This reference
starts with the most boring part of Python. If you plan to read the book from beginning to end, say, rather than using it just as a quick reference, then you can skip the first few chapters in your first reading, and come back to them later when needed.
The term program
means different things in different contexts, and across different programming languages. We start with somewhat formal explanations of what a Python program is, and how Python programs are executed, e.g., in the Python interpreter.
Python programs are logically organized into packages and modules, which we take a look at next. A module corresponds to a file in a physical file system, and modules, and including package modules, are the basic units of code reuse and sharing in Python.
Next, we briefly go through some of the lexical elements of the Python source code. There are some small variations across different programming languages, but their lexical compositions are rather similar, and Python is no different. This part can be skipped in your reading
.
Generally speaking, a program consists of code and data. Code refers to instructions. Data in Python is represented by objects. The object in Python is a fundamental component. Everything which we deal with in a Python program is objects. We start the main part of the book by introducing various important concepts related to the objects.
Although Python uses a dynamic type system, the types still play the foundational roles in the Python programming language. We first go through some of the basic builtin types in Python. Python includes quite a few builtin types, and this reference touches on only some of them. As indicated, this is generally true across all topics discussed in this reference. Completeness is not the goal of this mini reference.
Python also includes a few builtin compound types such as list and dictionary, which are important components of any non-trivial programs. We briefly go through these types in the next chapter. Advanced types, e.g., functions and classes, in particular, are explained in detail later in the book.
As with any imperative programming language, Python has expressions and statements. An expression prescribes how to compute a value using other expressions and values. Python supports most of the common operators and expressions found in other imperative languages. If you are coming from other procedural programming language background, you can skim through most of this part.
A statement is an instruction. Statements control the flow of a program to attain the desired goal. In Python, there are two kinds of statements, simple statements and compound statements. Compound, or complex, statements can include
other simple or compound statements.
Simple statements include expression statement, assignment statement, assert statement, pass statement, del statement, return statement, raise statement, break statement, continue statement, global statement, and nonlocal statement. Python’s compound statements include if statement, while statement, for statement, try statement, with statement, match statement, function def statement, class definition, and other coroutine-related statements.
One of the most significant changes to Python for the last 30+ years has been the addition of structural pattern matching to the language, as of Python 3.10 (2021). Pattern matching was first popularized by functional programming languages like Haskell, and it is now becoming more and more widely available across many different programming languages like C#, rust, swift, scala, and (yes) even Java.
As we more adopt this feature, as a community, pattern matching will likely change how we program in Python in the coming years. Although it is currently supported only in the context of the match statement, it is conceivable, and in fact expected, that pattern matching will be available more broadly across the language in the near future, considering its simplicity, elegance, and power.
A function definition is a compound statement. We dedicate its own chapter to the function definition. This chapter also includes other function-related topics such as lambda expressions and decorators.
Another compound statement, the class definition, is explained next. Other class-related concepts such enums and data classes are described in this chapter as well. We also provide an informal introduction to the object oriented programming styles in Python, including multiple inheritance.
Special kinds of functions, generators and coroutines, are separately discussed in the last part of the book, in the Coroutines chapter. We briefly touch on the (high-level) asynchronous programming style in Python using the new async and await keywords. In the last section, we provide a simple async programming example, namely, the producer-consumer problem.
As stated, we do not exhaustively cover every topic in Python in this mini reference
, including the (low-level) multi-thread and multi-process programming, etc. However, it goes through all the essential language features that any intermediate to advanced level Python programmers should be familiar with.
2. Python Programs
A complete Python program can be passed to the Python interpreter:
With the -c command line option with a program text,
With the -m command line option with a module name,
As a file name passed as the first command line argument, or
From the standard input.
The Python interpreter executes the input file, or the input text, as a complete program, in the namespace of a special module, __main__.
When the file or standard input is a terminal, or when the Python interpreter is invoked without an argument, the interpreter enters the interactive mode. The Python interpreter reads and executes one statement at a time (simple or compound) in the interactive mode instead of executing a complete program.
Here are a few examples:
①
A complete Python program is executed using the -c flag.
①
The Python script is passed to the interpreter as a command line argument.
①
The Python module hello_world is executed through the -m flag.
①
The Python code from the stdin (via Unix pipe).
①
Using the Unix shell here string
syntax.
①
The here doc
.
①
Invoking the python command without an argument starts the REPL.
①
Ditto. The Python interpreter ignores the flags following the dash -.
①
It also starts the