Table of Contents
Copyright
Python Mini Reference:
A Quick Guide to the Modern Python Programming Language
2022 Coding Books Press
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 author, nor its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Published: October 2022
Harry Yoon
San Diego, California
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 isto 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 languagesfor scientists,who have traditionally been using more high-level tools like Matlab.Python provides an easier "upgrade" pathto these "non-professional" programmers.Now, Python is becoming the language for machine learningand data science.
Python is beginner-friendly.In fact, it seems to be the most favorite first languagefor 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 broadand sanitized overview of the Python language,as of its most recent incarnation (e.g., 3.10 and 3.11).This book can be usefulto anyone who has been dabbling with Python without solid foundation.It can also be usefulto the people who have experience with other programming languagesand want to get some quick overview of the language.
This book is written as an (informal) language reference.The goal of this book is notto 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 bookmore 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 thatthere 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 languagegrammar.
It appears thatthe line between the programming language properand 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 modulesthat 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,e.g., in the Python interpreter.
Python programs are logically organized into,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 elementsof the .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.Everythingwhich we deal with in a Python program is objects.We start the main part of the bookby introducing various important conceptsrelated to the .
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 thein 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.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 valueusing other expressions and values.Python supports most offound 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 toattain the desired goal.In Python,there are two kinds of statements,.Compound, or complex, statementscan "include" other simple or compound statements.
Simple statements include.
One of the most significant changes to Pythonfor the last 30+ years has been the addition ofto 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 availableacross many different programming languageslike 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 Pythonin the coming years.Although it is currently supported only in the context ofthe ,it is conceivable, and in fact expected,that pattern matching will be available more broadly across the languagein the near future,considering its simplicity, elegance, and power.