Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic...

18

Transcript of Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic...

Page 1: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering
Page 2: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

Introduction to Python Programming

Page 3: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering
Page 4: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

Introduction to Python Programming

Dr. Pavel Solin

Revision October 11, 2018

Page 5: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

About the Author

Dr. Pavel Solin is Professor of Applied and Computational Mathematics at the Univer-sity of Nevada, Reno. He is an expert in scientific computing, leader of open sourceprojects, organizer of international scientific congresses, and the author of several text-books, research monographs, and many research articles in international journals.

Acknowledgment

Our sincere thanks go to many educators and students for providing valuable feed-back on this textbook as well as on the self-paced Python I and Python II courses inNCLab.

Copyright:

Copyright (c) 2018 NCLab Inc. All Rights Reserved.

Page 6: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

Preface

Python is a modern high-level dynamic programming language which is widely usedin business, science, and engineering applications. According to Github, as of 2018Python is the second most popular programming language after Javascript. Python’sgrowing popularity has the following reasons:

– It is known as a beginner’s language because of its simplicity.– It helps developers to be more productive from development to deployment and

maintenance.

Python’s syntax is very simple and high level when compared to Java, C and C++.Therefore, applications can be built with less code. Last, Python has a large collectionof powerful libraries.

Python libraries

Thanks to its free libraries, Python is replacing traditional software products in manyareas. In data analysis and statistics, Python has become more popular than SAS andeven than R. In scientific computing and visualization, the collection of the librariesScipy, Numpy, Sympy, Matplotlib and Mayavi has become more popular than MAT-LAB. Python also has powerful libraries for machine learning (Tensorflow, Keras), forgraph theory (NetworkX), and many others.

How to read this textbook

In contrast to other languages, Python can be used by non-programmers. Therefore,as the first thing we will show you how to use Python as a powerful command-linescientific calculator and data visualization tool.

Starting with Section 4 we will explore Python as a programming language. We willshow you how to work with text strings, variables, tuples, lists, dictionaries, functions,loops, conditions, files, exceptions, recursion, object-oriented programming, recursion,decorators, and more.

In every section, we will begin with simple examples and gradually progress to ad-vanced applications. Therefore, you don’t need to read every section until the end. It’sperfectly fine to read the first part of a section, move on, and return to the advancedtopics later.

Enjoy the textbook, and contact me at [email protected] if you have any questions,find typos, or have suggestions for other topics you would like to see here. Manythanks! – Pavel

Page 7: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering
Page 8: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

Table of Contents

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Why learn Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Compiled and interpreted programming languages . . . . . . . . . . . . . . . . . . . . . 21.4 A few more details about Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.5 Create a user account in NCLab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.6 Working in the cloud setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.7 Internet connection requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.8 Recommended devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 Using Python as a Scientific Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Why use Python as a calculator? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.3 Addition and subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.4 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.5 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.6 Floor division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.7 Modulo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.8 Powers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.9 Priority of operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.10 Using empty spaces makes your code more readable . . . . . . . . . . . . . . . . . . . 92.11 Importing the Numpy library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.12 Finite computer arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.13 Rounding numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.14 List of math functions and constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.15 Using the Fractions library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.16 Working with random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.17 Complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3 Drawing, Plotting, and Data Visualization with Matplotlib . . . . . . . . . . . . . . . . . . 153.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.2 Why learn about data visualization with Python? . . . . . . . . . . . . . . . . . . . . . . . 153.3 RGB colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.4 Secondary colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.5 Figuring out RGB codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.6 RGB color palettes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.7 Starting with Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Page 9: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

3.8 Two ways to specify color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183.9 Making axes equally scaled . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.10 Filling shapes with color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213.11 Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223.12 Interrupting lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.13 Making holes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.14 Plotting functions of one variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243.15 Line style, label, and legend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263.16 Showing the grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283.17 Adjusting plot limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293.18 Plotting multiple functions at once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303.19 Plotting circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.20 Making a circular hole . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333.21 Plotting ellipses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.22 Plotting spirals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.23 Parametric curves in the 3D space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363.24 Wireframe plots of functions of two variables . . . . . . . . . . . . . . . . . . . . . . . . . 373.25 Surface plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383.26 Color maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.27 Contour plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423.28 Changing view and adding labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433.29 Patches and artists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443.30 Pie charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.31 Donut charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.32 Bar charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493.33 Statistical data visualization with the Seaborn library . . . . . . . . . . . . . . . . . . 523.34 Interactive scientific data visualization with Mayavi2 . . . . . . . . . . . . . . . . . . 53

4 Working with Text Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544.2 Why learn about text strings? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544.3 Defining text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544.4 Storing text strings in variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554.5 Measuring the length of text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554.6 Python is case-sensitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564.7 The print function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564.8 Function help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574.9 Changing the default behavior of the print function . . . . . . . . . . . . . . . . . . . 574.10 Undesired trailing spaces and function repr . . . . . . . . . . . . . . . . . . . . . . . . . . 594.11 Cleaning text strings with strip, lstrip, and rstrip . . . . . . . . . . . . . . . 59

ii

Page 10: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

4.12 Wait - what is a "method" exactly? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604.13 Calling text string methods on raw text strings . . . . . . . . . . . . . . . . . . . . . . . . 614.14 Using single and double quotes in text strings . . . . . . . . . . . . . . . . . . . . . . . . . 614.15 A more robust approach - using characters \’ and \" . . . . . . . . . . . . . . . . . . . 624.16 Length of text strings containing special characters . . . . . . . . . . . . . . . . . . . . 624.17 Newline character \n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634.18 Writing long code lines over multiple lines with the backslash \ . . . . . . . . 634.19 Multiline strings enclosed in triple quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644.20 Adding text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654.21 Multiplying text strings with integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664.22 Parsing text strings with the for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664.23 Reversing text strings with the for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674.24 Accessing individual characters via their indices . . . . . . . . . . . . . . . . . . . . . . . 684.25 Using negative indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684.26 ASCII table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694.27 Finding the ASCII code of a given character . . . . . . . . . . . . . . . . . . . . . . . . . . . 694.28 Finding the character for a given ASCII code . . . . . . . . . . . . . . . . . . . . . . . . . . 704.29 Slicing text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704.30 Third index in the slice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714.31 Creating copies of text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714.32 Reversing text strings using slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724.33 Retrieving current date and time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724.34 Making text strings lowercase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734.35 Checking for substrings in a text string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734.36 Making the search case-insensitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744.37 Making text strings uppercase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754.38 Finding and replacing substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754.39 Counting occurrences of substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764.40 Locating substrings in text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764.41 Splitting a text string into a list of words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774.42 Splitting a text string while removing punctuation . . . . . . . . . . . . . . . . . . . . . 774.43 Joining a list of words into a text string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784.44 Method isalnum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784.45 Method isalpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794.46 Method isdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794.47 Method capitalize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804.48 Method title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804.49 C-style string formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804.50 Additional string methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

iii

Page 11: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

4.51 The string library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814.52 Natural language toolkit (NLTK) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

5 Variables and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835.2 Why learn about variables? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835.3 Statically vs. dynamically typed languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845.4 Types of variables (data types) in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845.5 Using a non-initialized variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845.6 Various ways to create variables and initialize them with values . . . . . . . . . 855.7 Casting variables to text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865.8 Casting can be tricky sometimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875.9 Inadmissible casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885.10 Dynamic type interpretation and its abuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885.11 Determining the type of a variable at runtime . . . . . . . . . . . . . . . . . . . . . . . . . 895.12 Operators +=, -=, *= and \= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915.13 Local and global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925.14 Using global variables in functions can get you in trouble . . . . . . . . . . . . . . 925.15 Changing global variables in functions can get you in big trouble . . . . . . . 935.16 Shadowing of variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945.17 Callable variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

6 Boolean Values, Functions, Expressions, and Variables . . . . . . . . . . . . . . . . . . . . . . 956.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956.2 Why learn about Booleans? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956.3 Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956.4 Boolean functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966.5 Comparison operators for numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966.6 Comparison operator == vs. assignment operator = . . . . . . . . . . . . . . . . . . . . . 976.7 Comparison operators for text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986.8 Storing results of Boolean expressions in variables . . . . . . . . . . . . . . . . . . . . . . 986.9 Example: Checking if there exists a triangle with edge lengths a, b, c . . . . . . 996.10 Logical and and its truth table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006.11 Logical or and its truth table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016.12 Negation not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026.13 Booleans in conditions and the while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026.14 Example: Monte Carlo calculation of π . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

7 Lists, Tuples, Dictionaries, and Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067.2 Why learn about lists? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067.3 Creating a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

iv

Page 12: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

7.4 Important properties of lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077.5 Measuring the length of a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077.6 Appending new items to lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077.7 Adding lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1087.8 Adding is not appending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097.9 Multiplying lists with integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097.10 Parsing lists with the for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1107.11 Accessing list items via their indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1107.12 Slicing lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1117.13 Creating a copy of a list – the wrong way and the right way . . . . . . . . . . . . 1117.14 Popping list items by index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1127.15 Deleting list items by index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1137.16 Checking if an item is in a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1137.17 Locating an item in a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1147.18 Finding and deleting an item from a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1147.19 Finding and deleting all occurrences of an item . . . . . . . . . . . . . . . . . . . . . . . . 1157.20 Counting occurrences of items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1157.21 Inserting list items at arbitrary positions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1157.22 Sorting a list in place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1167.23 Creating sorted copy of a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1167.24 Using key functions in sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177.25 Using lambda functions in sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177.26 Reversing a list in place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187.27 Creating reversed copy of a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187.28 Zipping lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187.29 List comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1197.30 List comprehension with if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1197.31 List comprehension with if and else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1207.32 List comprehension with nested loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1217.33 Mutable and immutable objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1227.34 Mutability of lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1247.35 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1267.36 Read-only property of tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1277.37 Immutability of tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1277.38 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1287.39 Creating an empty and nonempty dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . 1297.40 Keys, values, and items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1297.41 Accessing values using keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1307.42 Adding, updating, and deleting items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

v

Page 13: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

7.43 Checking for keys, values, and items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1317.44 Finding all keys for a given value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1317.45 Finding all keys for a given value using comprehension . . . . . . . . . . . . . . . . 1327.46 Reversing a dictionary using comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . 1337.47 Beware of repeated values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1337.48 Creating a dictionary from two lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1347.49 Reversing a dictionary using zip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1347.50 Creating a copy of a dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1347.51 Merging dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1357.52 Adding dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1367.53 Composing dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1367.54 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1377.55 Creating sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1387.56 Adding and removing elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1387.57 Popping random elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1397.58 Checking if an element is in a set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1407.59 Checking for subsets and supersets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1407.60 Intersection and union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1417.61 Difference and symmetric difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1427.62 Using a set to remove duplicated items from a list . . . . . . . . . . . . . . . . . . . . . 143

8 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1448.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1448.2 Why learn about functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1448.3 Defining new functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1448.4 Docstrings and function help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1458.5 Function parameters vs. arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1468.6 Names of functions should reveal their purpose . . . . . . . . . . . . . . . . . . . . . . . . 1468.7 Functions returning multiple values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1478.8 What is a wrapper? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1498.9 Using parameters with default values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1498.10 Functions accepting a variable number of arguments . . . . . . . . . . . . . . . . . . 1518.11 args is not a keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1528.12 Passing a list as *args . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1538.13 Obtaining the number of *args . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1548.14 Combining standard arguments and *args . . . . . . . . . . . . . . . . . . . . . . . . . . . 1548.15 Using keyword arguments *kwargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1558.16 Passing a dictionary as **kwargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1568.17 Defining local functions within functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1578.18 Anonymous lambda functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

vi

Page 14: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

8.19 Creating multiline lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1598.20 Using lambdas to create a live list of functions . . . . . . . . . . . . . . . . . . . . . . . . . 1598.21 Using lambdas to create a live table of functions . . . . . . . . . . . . . . . . . . . . . . . 1608.22 Using lambdas to create a live table of logic gates . . . . . . . . . . . . . . . . . . . . . . 1608.23 Using lambdas to create a factory of functions . . . . . . . . . . . . . . . . . . . . . . . . . 1618.24 Variables of type ’function’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1628.25 Inserting conditions into lambda functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1638.26 Using lambdas to customize sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1638.27 Obtaining useful information about functions from their attributes . . . . . . 164

9 The ’For’ Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1659.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1659.2 Why learn about the for loop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1659.3 Parsing text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1669.4 Splitting a text string into a list of words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1669.5 Parsing lists and tuples (including comprehension) . . . . . . . . . . . . . . . . . . . . . 1669.6 Parsing two lists simultaneously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1669.7 Iterating over sequences of numbers and the built-in function range . . . . . 1679.8 Parsing dictionaries (including comprehension) . . . . . . . . . . . . . . . . . . . . . . . . 1689.9 Nested for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1699.10 Terminating loops with the break statement . . . . . . . . . . . . . . . . . . . . . . . . . . 1709.11 Terminating current loop cycle with continue . . . . . . . . . . . . . . . . . . . . . . . 1719.12 The for-else statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1739.13 The for loop behind the scenes – iterables and iterators . . . . . . . . . . . . . . . . 1759.14 Making your own classes iterable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1789.15 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1789.16 Functional programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

10 Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18010.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18010.2 Why learn about conditions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18010.3 Boolean values, operators, and expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18010.4 Using conditions without the keyword if . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18010.5 The if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18110.6 Types of values accepted by the if statement . . . . . . . . . . . . . . . . . . . . . . . . . 18210.7 The optional else branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18310.8 The full if-elif-else statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18310.9 Example – five boxes of apples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18410.10 Conditional (ternary) expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18510.11 Nested conditional expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18710.12 Famous workarounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

vii

Page 15: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

11 The ’While’ Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18911.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18911.2 Why learn about the while loop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18911.3 Syntax and examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18911.4 How to time your programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19011.5 The break statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19211.6 The continue statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19211.7 Emulating the do-while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19311.8 The while-else statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19411.9 Abusing the while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19511.10 Solving an unsolvable equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19611.11 Numerical methods and scientific computing . . . . . . . . . . . . . . . . . . . . . . . . 198

12 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19912.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19912.2 Why learn about exceptions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19912.3 Catching exceptions – statement try-except . . . . . . . . . . . . . . . . . . . . . . . . 20112.4 List of common exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20212.5 Catching a general exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20412.6 Function assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20412.7 Throwing exceptions – statement raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20612.8 The full statement try-except-else-finally . . . . . . . . . . . . . . . . . . . . . 206

13 File Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20713.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20713.2 Why learn about files? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20713.3 NCLab file system and security of your data . . . . . . . . . . . . . . . . . . . . . . . . . . 20713.4 Creating sample text file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20813.5 Creating sample Python file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20913.6 Opening a text file for reading – method open . . . . . . . . . . . . . . . . . . . . . . . . . 21013.7 Closing a file – method close . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21013.8 Checking whether a file is closed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21013.9 Using the with statement to open files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21113.10 Getting the file name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21113.11 A word about encoding, Latin-1, and UTF-8 . . . . . . . . . . . . . . . . . . . . . . . . . . 21113.12 Checking text encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21213.13 Other modes to open a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21213.14 Reading the file line by line using the for loop . . . . . . . . . . . . . . . . . . . . . . . 21313.15 The mystery of empty lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21413.16 Reading individual lines – method readline . . . . . . . . . . . . . . . . . . . . . . . . 21513.17 Reading the file line by line using the while loop . . . . . . . . . . . . . . . . . . . . 217

viii

Page 16: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

13.18 Reading the file using next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21813.19 Reading the file as a list of lines – method readlines . . . . . . . . . . . . . . . . 21813.20 File size vs available memory considerations . . . . . . . . . . . . . . . . . . . . . . . . . 21913.21 Reading the file as a single text string – method read . . . . . . . . . . . . . . . . . 22013.22 Rewinding a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22013.23 File pointer, and methods read, tell and seek . . . . . . . . . . . . . . . . . . . . . 22213.24 Using tell while iterating through a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22513.25 Another sample task for tell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22713.26 Writing text to a file – method write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22713.27 Writing a list of lines to a file – method writelines . . . . . . . . . . . . . . . . . 23013.28 Writing to the middle of a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23113.29 Things can go wrong: using exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23313.30 Binary files I – checking byte order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23413.31 Binary files II – writing unsigned integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23413.32 Binary files III – writing bit sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23513.33 Binary files IV – reading byte data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

14 Object-Oriented Programming I - Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23814.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23814.2 Why learn about OOP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23814.3 Procedural (imperative) programming and OOP . . . . . . . . . . . . . . . . . . . . . . 23814.4 Main benefits of OOP in a nutshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23914.5 There are disadvantages, too . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23914.6 Procedural vs. object-oriented thinking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23914.7 Classes, objects, attributes and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24014.8 Defining class Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24114.9 Using class Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

15 Object-Oriented Programming II - Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . 24715.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24715.2 Why learn about class inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24715.3 Hierarchy of vehicles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24715.4 Class inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24915.5 Base class Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24915.6 Hierarchy of geometrical shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25015.7 Deriving class Polygon from class Geometry . . . . . . . . . . . . . . . . . . . . . . . . 25115.8 Deriving classes Triangle and Quad from class Polygon . . . . . . . . . . . . . 25315.9 Deriving class Rectangle from class Quad . . . . . . . . . . . . . . . . . . . . . . . . . . . 25415.10 Deriving class Square from class Rectangle . . . . . . . . . . . . . . . . . . . . . . . 25415.11 Deriving class Circle from class Geometry . . . . . . . . . . . . . . . . . . . . . . . . . 25515.12 Sample application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

ix

Page 17: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

16 Object-Oriented Programming III - Advanced Aspects . . . . . . . . . . . . . . . . . . . . . 25916.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25916.2 Why learn about polymorphism and multiple inheritance? . . . . . . . . . . . . . 25916.3 Inspecting objects with isinstance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25916.4 Inspecting instances of custom classes with isinstance . . . . . . . . . . . . . . 26016.5 Inspecting objects with type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26116.6 Inspecting classes with help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26116.7 Obtaining class and class name from an instance . . . . . . . . . . . . . . . . . . . . . . 26516.8 Inspecting classes with issubclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26516.9 Inspecting objects with hasattr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26616.10 Polymorphism I - Meet the Lemmings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26716.11 Polymorphism II - Geometry classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26916.12 Multiple inheritance I - Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27016.13 Multiple inheritance II - The Diamond of Dread . . . . . . . . . . . . . . . . . . . . . . 27116.14 Multiple inheritance III - Leonardo da Vinci . . . . . . . . . . . . . . . . . . . . . . . . . . 271

17 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27417.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27417.2 Why learn about recursion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27417.3 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27417.4 Is your task suitable for recursion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27517.5 Calculating the factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27617.6 Stopping condition and infinite loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27917.7 Parsing binary trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

18 Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28218.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28218.2 Building a timing decorator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28418.3 Building a debugging decorator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28718.4 Combining decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28818.5 Decorating class methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28918.6 Passing arguments to decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29118.7 Debugging decorated functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29318.8 Python Decorator Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

19 Selected Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29519.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29519.2 Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29519.3 Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29619.4 Function reduce() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29719.5 Shallow and deep copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

x

Page 18: Introduction to Python Programming · 2020-02-09 · Preface Python is a modern high-level dynamic programming language which is widely used in business, science, and engineering

Full text is available upon enrollment.