Python Distilled Developer s Library 1st Edition David Beazley
Python Distilled Developer s Library 1st Edition David Beazley
Python Distilled Developer s Library 1st Edition David Beazley
Python Distilled Developer s Library 1st Edition David Beazley
1. Read Anytime Anywhere Easy Ebook Downloads at ebookmeta.com
Python Distilled Developer s Library 1st Edition
David Beazley
https://ebookmeta.com/product/python-distilled-developer-s-
library-1st-edition-david-beazley/
OR CLICK HERE
DOWLOAD EBOOK
Visit and Get More Ebook Downloads Instantly at https://ebookmeta.com
4. Contents
Preface
Acknowledgments
About the Author
Chapter 1: Python Basics
Chapter 2: Operators, Expressions, and Data
Manipulation
Chapter 3: Program Structure and Control Flow
Chapter 4: Objects, Types, and Protocols
Chapter 5: Functions
Chapter 6: Generators
Chapter 7: Classes and Object-Oriented
Programming
Chapter 8: Modules and Packages
Chapter 9: Input and Output
Chapter 10: Built-in Functions and Standard
Library
5. Table of Contents
Preface
Acknowledgments
About the Author
Chapter 1: Python Basics
Running Python
Python Programs
Primitives, Variables, and Expressions
Arithmetic Operators
Conditionals and Control Flow
Text Strings
File Input and Output
Lists
Tuples
Sets
Dictionaries
Iteration and Looping
Functions
Exceptions
Program Termination
Objects and Classes
Modules
Script Writing
6. Packages
Structuring an Application
Managing Third Party Packages
Python: It Fits Your Brain
Chapter 2: Operators, Expressions, and Data
Manipulation
Literals
Expressions and Locations
Standard Operators
In-place Assignment
Object Comparison
Ordered Comparison Operators
Boolean Expressions and Truth Values
Conditional Expressions
Operations Involving Iterables
Operations on Sequences
Operations on Mutable Sequences
Operations on Sets
Operations on Mappings
List, Set, and Dictionary Comprehensions
Generator Expressions
The Attribute (.) Operator
The Function Call () Operator
Order of Evaluation
Final Words - The Secret Life of Data
Chapter 3: Program Structure and Control Flow
7. Program Structure and Execution
Conditional Execution
Loops and Iteration
Exceptions
Context Managers and the with Statement
Assertions and __debug__
Final Words
Chapter 4: Objects, Types, and Protocols
Essential Concepts
Object Identity and Type
Reference Counting and Garbage Collection
References and Copies
Object Representation and Printing
First Class Objects
Using None for Optional or Missing Data
Object Protocols and Data Abstraction
Object Protocol
Number Protocol
Comparison Protocol
Conversion Protocols
Container Protocol
Iteration Protocol
Attribute Protocol
Function Protocol
Context Manager Protocol
Final words: On Being Pythonic
8. Chapter 5: Functions
Function Definitions
Default Arguments
Variadic Arguments
Keyword Arguments
Variadic Keyword Arguments
Functions Accepting All Inputs
Positional Only Arguments
Names, Documentation Strings, and Type Hints
Function Application and Parameter Passing
Return Values
Error Handling
Scoping Rules
Recursion
The lambda Expression
Higher Order Functions
Argument Passing in Callback Functions
Returning Results from Callbacks
Decorators
Map, Filter, and Reduce
Function Introspection, Attributes, and Signatures
Environment Inspection
Dynamic Code Execution and Creation
Asynchronous Functions and Await
Final Words: Thoughts on Functions and Composition
Chapter 6: Generators
9. Generators and yield
Restartable Generators
Generator Delegation
Using Generators in Practice
Enhanced Generators and yield Expressions
Applications of Enhanced Generators
Generators and the Bridge to Awaiting
Final Words: A Brief History of Generators and Looking Forward
Chapter 7: Classes and Object-Oriented
Programming
Objects
The class Statement
Instances
Attribute Access
Scoping Rules
Operator Overloading and Protocols
Inheritance
Avoiding Inheritance via Composition
Avoiding Inheritance via Functions
Dynamic Binding and Duck Typing
The Danger of Inheriting from Built-in Types
Class Variables and Methods
Static Methods
A Word About Design Patterns
Data Encapsulation and Private Attributes
Type Hinting
Properties
10. Types, Interfaces, and Abstract Base Classes
Multiple Inheritance, Interfaces, and Mixins
Type-based Dispatch
Class Decorators
Supervised Inheritance
The Object Life Cycle and Memory Management
Weak References
Internal Object Representation and Attribute Binding
Proxies, Wrappers, and Delegation
Reducing memory use with __slots__
Descriptors
Class Definition Process
Dynamic Class Creation
Metaclasses
Built-in Objects for Instances and Classes
Final Words: Keep it Simple
Chapter 8: Modules and Packages
Modules and the import Statement
Module Caching
Importing Selected Names from a Module
Circular Imports
Module Reloading and Unloading
Module Compilation
The Module Search Path
Execution as the Main Program
Packages
11. Running a Package submodule as a script
Controlling the Package Namespace
Controlling Package Exports
Package data
Module Objects
Module Attribute Access
Deploying Python Packages
Chapter 9: Input and Output
Data Representation
Text Encoding and Decoding
Text and Byte Formatting
Reading Command-Line Options
Environment Variables
Files and File Objects
I/O Abstraction Layers
Standard Input, Output, and Error
Directories
The print() function
Generating Output
Consuming Input
Object Serialization
Blocking Operations and Concurrency
Standard Library Modules
Final Words
Chapter 10: Built-in Functions and Standard
Library
13. Preface
More than 20 years have passed since I authored the Python
Essential Reference. At that time, Python was a much smaller
language and it came with a useful set of batteries in its standard
library. It was something that could mostly fit your brain. The
Essential Reference reflected that era. It was meant to be a small
book that you could take with you to write some Python code on a
desert island or inside a secret vault. Over three subsequent
revisions, the Essential Reference more-or-less evolved with this
vision of being a compact, but complete language reference—
because if you were going to code Python on vacation, why wouldn’t
you want to use all of it?
Today, more than a decade has passed since the publication of the
last edition and the Python world is much different. No longer a
niche language, Python has grown to become one of the most
popular programming languages in the world. Python programmers
also have a wealth of information at their fingertips in the form of
advanced editors, IDEs, notebooks, web pages, and more. In fact,
there’s probably little need to consult a "reference book" when
almost any reference material you might want can be conjured to
appear before your very eyes with the touch of a few keys.
If anything, the ease of information retrieval and the "bigness" of
the Python universe presents a different kind of challenge. If you’re
just trying to learn or starting to solve a new problem, it can be a bit
overwhelming to know where to begin. It can also be di˚cult to
separate the features of various tools from the core language itself.
These kinds of problems are the foundation for this book.
Python Distilled is a book about programming in Python. However,
rather than tring to document absolutely "everything" that’s possible
or has been previously done in Python, the focus is on presenting a
14. modern, yet curated (or distilled) core of the language. Much of this
has been informed by my years of teaching Python to scientists,
engineers, and software professionals. However, it’s also a product
of writing various software libraries, pushing the outer edges of what
makes Python tick, and learning about what’s most useful.
For the most part, the book stays focused on the topic of Python
programming itself. This includes abstraction techniques, program
structure, data, functions, objects, modules, and so forth—topics
that will well serve programmers working on Python projects of any
size. Pure reference material that can be easily obtained via an IDE
(i.e., lists of functions, names of commands, arguments, etc.) is
generally omitted. I’ve also made a conscious choice to not describe
the fast-changing world of Python tooling related to editors, IDEs,
deployment, and other matters.
Perhaps controversially, I don’t generally focus on language features
related to large-scale software project management. Python is
sometimes used for big and serious things—somehow involving
millions upon millions of lines of code. Maintaining such applications
requires tooling, and design, and features, and types, and
committees, and meetings, and decisions to be made about very
important matters. Matters of such importance are too important for
this small book. Thus, they are left as an exercise for the reader.
However, the honest answer is that I don’t use Python to write such
applications—and neither should you.
In writing a book, there is always a cut-off regarding ever-evolving
language features. This book was written during the era of Python
3.9. As such, it does not include some of the major additions
planned for later releases—for example, structural pattern matching.
That’s a topic for a different time and place.
Last, but not least, I think it’s important that programming remain
fun. I hope that the book not only helps you become a productive
Python programmer, but that it also captures some of the magic that
has inspired people to use Python for exploring the stars, flying
17. About the Author [This content
is currently in development.]
This content is currently in development.
18. Python Basics
This chapter provides an overview of the central core of the Python
language. Topics include variables, data types, expressions, control
flow, functions, classes, and input/output. The chapter concludes
with a discussion of modules, script writing, packages, and a few
tips on organizing larger programs. This chapter is not intended to
provide comprehensive coverage of every feature nor does it
concern itself with all of the tooling that might surround a larger
Python project. However, experienced programmers should be able
to extrapolate from the material here to write more advanced
programs. Newcomers are encouraged to try the examples in a
simple environment such as a terminal window and a basic text
editor.
Running Python
Python programs are executed by an interpreter. There are many
different environments in which the Python interpreter might run
ranging from IDEs, a browser, or a terminal window. However,
underneath all of that, the core of the interpreter is a text-based
application that can be started by typing python into a terminal
command shell such as bash. Because Python 2 and Python 3 might
be installed on the same machine you might need to type python2 or
python3 to pick a version. This book assumes Python 3.8 or newer.
When the interpreter starts, a prompt appears at which you can
start typing programs into a so called "read-evaluation print loop" (or
REPL). For example, in the following output, the interpreter displays
its copyright message and presents the user with the >>> prompt, at
which the user types a familiar "Hello World" program:
19. Python 3.8.0 (default, Feb 3 2019, 05:53:21)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on
darwin
Type "help", "copyright", "credits" or "license" for more
information.
>>> print('Hello World')
Hello World
>>>
Certain environments may display a different prompt such as the
following output from ipython (an alternate shell for Python).
Python 3.8.0 (default, Feb 4, 2019, 07:39:16)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for
help.
In [1]: print('Hello World')
Hello World
In [2]:
Regardless of the exact output you see, the underlying principle is
the same. If you type a command, it runs and you’ll immediately see
the output.
Python’s interactive mode is one of its most useful features because
you can type any valid statement and immediately see the results.
This is useful for debugging and experimentation. Many people,
including the author, even use interactive Python as their desktop
calculator. For example:
>>> 6000 + 4523.50 + 134.25
10657.75
>>> _ + 8192.75
18850.5
>>>
20. When you use Python interactively, the variable _ holds the result of
the last operation. This is useful if you want to use the result of the
last operation in subsequent statements. This variable only gets
defined when working interactively so you don’t use it in saved
programs.
You can exit the interactive interpreter by typing quit() or the EOF
(end of file) character. On UNIX, EOF is Ctrl+D; on Windows, it’s
Ctrl+Z.
Python Programs
If you want to create a program that you can run repeatedly, put
statements in a text file such as the following:
# hello.py
print('Hello World')
Python source files are UTF-8 encoded text files that normally have a
.py su˚x. The # character denotes a comment that extends to the
end of the line. International (Unicode) characters can be freely used
in the source code as long as you remember to use UTF-8 encoding
(this is often the default in most editors, but it never hurts to check
your editor settings if you’re unsure).
To execute the hello.py file, provide the filename to the interpreter
as follows:
shell % python3 hello.py
Hello World
shell %
It is common to use #! to specify the interpreter on the first line of a
program, like this:
#!/usr/bin/env python3
print('Hello World')
21. If you give this file execute permissions on Unix (e.g., using chmod
+x hello.py), you can run the program by typing hello.py into your
shell.
On Windows, you can double-click on a .py file or type the name of
the program into the "Run" command on the Windows Start menu to
launch it. The #! line, if given, is used to pick the interpreter version
(Python 2 vs 3). Be aware that execution of a program might take
place in a console window that disappears immediately after the
program completes its execution (often before you can read its
output). For debugging, it may be better to run the program within a
Python development environment.
The interpreter runs statements in order until it reaches the end of
the input file. At that point, the program terminates and Python
exits.
Primitives, Variables, and
Expressions
Python provides a collection of primitive types such as integers,
floats, and strings:
42 # int
4.2 # float
'forty-two' # str
True # bool
A variable is a name that refers to a value. A value represents an
object of some type (e.g., an integer, float, string, etc.).
x = 42
Sometimes you might see a type explicitly attached to a name. For
example,
x: int = 42
22. The presence of a type is merely a hint to assist code readability and
for possible use by third-party code checking tools. Otherwise, it is
completely ignored. It does not prevent you from assigning a
different kind of value later.
An expression is a combination of primitives, names, and operators
that produce a value:
2 + 3 * 4 # -> 14
The following program shows the use of variables and expressions
by performing a compound-interest calculation.
# interest.py
principal = 1000 # Initial amount
rate = 0.05 # Interest rate
numyears = 5 # Number of years
year = 1
while year <= numyears:
principal = principal * (1 + rate)
print(year, principal)
year += 1
When executed, the following output is produced:
1 1050.0
2 1102.5
3 1157.625
4 1215.5062500000001
5 1276.2815625000003
The while statement tests the conditional expression that
immediately follows. If the tested condition is true, the body of the
while statement executes. The condition is then retested and the
body executed again until the condition becomes false. The body of
the loop is denoted by indentation. Thus, the three statements
following while in interest.py execute on each iteration. Python
23. doesn’t specify the amount of required indentation, as long as it’s
consistent within a block. However, it is most common to use four
spaces per indentation level.
One problem with the interest.py program is that the output isn’t
very pretty. To make it better, you could right-align the columns and
limit the precision of principal to two digits. Change the print()
function to use a so-called "f-string" like this:
print(f'{year:>3d} {principal:0.2f}')
In the f-string, variable names and expressions can be evaluated by
enclosing them in curly-braces. Optionally, each substitution can
have a formatting specifier attached to it. '>3d' means a 3-digit
decimal number, right aligned. '0.2f' means a floating point
number with 2 decimal places of accuracy. More information about
these formatting codes can be found in Chapter 9.
Now the output of the program looks like this:
1 1050.00
2 1102.50
3 1157.62
4 1215.51
5 1276.28
Arithmetic Operators
Python has a standard set of mathematical operators as shown in
Table 1. These operators have the standard meaning as they do in
most other programming languages.
Table 1: Arithmetic Operators
24. The division operator (/) produces a floating point number when
applied to integers. Therefore 7/4 is 1.75. The truncating division
operator (//, also known as floor division) truncates the result to an
integer and works with both integers and floating-point numbers.
The modulo operator returns the remainder of the division x // y.
For example, 7 % 4 is 3. For floating-point numbers, the modulo
operator returns the floating-point remainder of x // y, which is x –
(x // y) * y.
In addition, the built-in functions in Table 2 provide a few more
commonly used numerical operations:
Table 2: Common Mathematic Functions
25. The round() function implements "Banker’s Rounding." If the value
being rounded is equally close to two multiples, it is rounded to the
nearest even multiple (for example, 0.5 is rounded to 0.0, and 1.5 is
rounded to 2.0).
Integers provide a few additional operators to support bit-
manipulation as shown in Table 3.
Table 3: Bit Manipulation Operators
One would commonly use these with binary integers. For example:
a = 0b11001001
mask = 0b11110000
x = (a & mask) >> 4 # x = 0b1100 (12)
In this example, 0b11001001 is how you write an integer value in
binary. You could have written it as decimal 201 or hexadecimal
0xc9, but if you’re fiddling with bits, binary makes it easier to
visualize what you’re doing.
The semantics of the bitwise operators assumes that the integers
are represented in a 2’s complement binary representation and that
the sign bit is infinitely extended to the left. Some care is required if
you are working with raw bit-patterns that are intended to map to
native integers on the hardware. This is because Python does not
truncate the bits or allow values to overflow—instead, the result will
26. grow arbitrarily large in magnitude. It’s up to you to make sure the
result is properly sized or truncated if needed.
To compare numbers, use the comparison operators in Table 4:
Table 4: Comparison Operators
The result of a comparison is a boolean value True or False.
The and, or, and not operators can form more complex Boolean
expressions related logical truth (not to be confused with the bit-
manipulation operators above). The behavior of these operators is as
shown in Table 5.
Table 5: Logical Operators
A value is considered "false" if it is literally False, None, numerically
zero, or empty. Otherwise, it’s considered true.
27. It is common to write an expression that updates a value. For
example:
x = x + 1
y = y * n
For these, you can write the following shortened operation instead:
x += 1
y *= n
This shortened form of update can be used with any of the the +, -,
*, **, /, //, %, &, |, ˆ, <<, >> operators. Python does not have
increment (++) or decrement (--) operators sometimes found in
other languages.
Conditionals and Control Flow
The while, if and else statements are used for looping and
conditional code execution. Here’s an example:
if a < b:
print('Computer says Yes')
else:
print('Computer says No')
The bodies of the if and else clauses are denoted by indentation.
The else clause is optional. To create an empty clause, use the pass
statement, as follows:
if a < b:
pass # Do nothing
else:
print('Computer says No')
To handle multiple-test cases, use the elif statement, like this:
28. if suffix == '.htm':
content = 'text/html'
elif suffix == '.jpg':
content = 'image/jpeg'
elif suffix == '.png':
content = 'image/png'
else:
raise RuntimeError(f'Unknown content type {suffix!r}')
If you are assigning a value in combination with a test, you can use
a conditional expression like this:
maxval = a if a > b else b
This is the same as the longer:
if a > b:
maxval = a
else:
maxval = b
Sometimes, you may see the assignment of a variable and a
conditional combined together using the := operator. This is known
as an assignment expression (or more colloquially as the "walrus
operator" since := looks like a walrus tipped over on its side—
presumably playing dead). For example:
x = 0
while (x := x + 1) < 10: # Prints 1, 2, 3, ..., 9
print(x)
The use of parentheses to surround an assignment expression is
always required.
The break statement can be used to abort a loop early. It only
applies to the inner-most loop. For example:
x = 0
while x < 10:
29. if x == 5:
break # Stops the loop. Moves to Done below
print(x)
x += 1
print('Done')
The continue statement is used to skip the rest of the loop body and
go back to the top of the loop. For example:
x = 0
while x < 10:
x += 1
if x == 5:
continue # Skips the print(x). Goes back to loop
start.
print(x)
print('Done')
Text Strings
To define string literals, enclose them in single, double, or triple
quotes as follows:
a = 'Hello World'
b = "Python is groovy"
c = '''Computer says no.'''
d = """Computer still says no."""
The same type of quote used to start a string must be used to
terminate it. Triple-quoted strings capture all the text that appears
prior to the terminating triple quote, as opposed to single- and
double-quoted strings, which must be specified on one logical line.
Triple-quoted strings are useful when the contents of a string literal
span multiple lines of text such as the following:
30. print('''Content-type: text/html
<h1> Hello World </h1>
Click <a href="http://www.python.org">here</a>.
''')
Immediately adjacent string literals are concatenated into a single
string. Thus, the above example could also be written as:
print(
'Content-type: text/htmln'
'n'
'<h1> Hello World </h1>n'
'Clock <a href="http://www.python.org">here</a>n'
)
If the opening quotation mark of a string is prefaced by an f,
escaped expressions within a string are evaluated. For example, in
earlier examples, the following statement was used to output values
of a calculation:
print(f'{year:>3d} {principal:0.2f}')
Although this is only using simple variable names, any valid
expression can appear. For example:
base_year = 2020
...
print(f'{base_year + year:>4d} {principal:0.2f}')
As an alternative to f-strings, the format() method and % operator
are also sometimes used to format strings. For example:
print('{0:>3d} {1:0.2f}'.format(year, principal))
print('%3d %0.2f' % (year, principal))
More information about string formatting is found in Chapter 9.
31. Strings are stored as sequences of Unicode characters indexed by
integers, starting at zero. Negative indices index from the end of the
string. The length of a string s is computed using len(s). To extract
a single character, use the indexing operator s[i] where i is the
index.
a = 'Hello World'
print(len(a)) # 11
b = a[4] # b = 'o'
c = a[-1] # c = 'd'
To extract a substring, use the slicing operator s[i:j]. This extracts
all characters from s whose index k is in the range i <= k < j. If
either index is omitted, the beginning or end of the string is
assumed, respectively:
c = a[:5] # c = 'Hello'
d = a[6:] # d = 'World'
e = a[3:8] # e = 'lo Wo'
f = a[-5:] # f = 'World'
Strings have a variety of methods for manipulating their contents.
For example, the replace() method performs a simple text
replacement:
g = a.replace('Hello', 'Hello Cruel') # f = 'Hello Cruel
World'
Table 6 shows a few common string methods (Note: here and
elsewhere, arguments enclosed in square brackets are optional).
Table 6: Common String Methods
32. Strings are concatenated with the plus (+) operator:
g = a + 'ly' # g = 'Hello Worldly'
Python never implicitly interprets the contents of a string as
numerical data. Thus, + always concatenates strings:
x = '37'
y = '42'
z = x + y # z = '3742' (String Concatenation)
To perform mathematical calculations, strings first have to be
converted into a numeric value using a function such as int() or
33. float(). For example:
z = int(x) + int(y) # z = 79 (Integer Addition)
Non-string values can be converted into a string representation by
using the str(), repr(), or format() functions. Here’s an example:
s = 'The value of x is ' + str(x)
s = 'The value of x is ' + repr(x)
s = 'The value of x is ' + format(x, '4d')
Although str() and repr() both create strings, their output is often
different. str() produces the output that you get when you use the
print() function, whereas repr() creates a string that you type into
a program to exactly represent the value of an object. For example:
>>> s = 'hellonworld'
>>> print(str(s))
hello
world
>>> print(repr(s))
'hellonworld'
>>>
When debugging, it is advised to use repr(s) when producing
output. repr() shows you more precise information about the value
and its type.
The format() function is used to convert a single value to a string
with a specific formatting applied. For example:
>>> x = 12.34567
>>> format(x, '0.2f')
'12.35'
>>>
The format code given to format() is the same code you would use
with f-strings when producing formatted output. For example, the
above code could be replaced by the following:
34. >>> f'{x:0.2f}'
'12.35'
>>>
File Input and Output
The following program opens a file and reads its contents line by line
as text strings:
with open('data.txt') as file:
for line in file:
print(line, end='') # end='' omits the extra newline
The open() function returns a new file object. The with statement
that precedes it declares a block of statements (or context) where
the file (file) is going to be used. Once control leaves this block,
the file is automatically closed. If you don’t use the with statement,
the code would need to look like this:
file = open('data.txt')
for line in file:
print(line, end='') # end='' omits the extra newline
file.close()
It’s easy to forget the extra step of calling close() so it’s usually
better to use the with statement and have the file closed for you.
The for-loop iterates line-by-line over the file until no more data is
available.
If you want to read the file in its entirety as a string, use the read()
method like this:
with open('data.txt') as file:
data = file.read()
If you want to read a large file in chunks, give a size hint to the
read() method as follows:
35. with open('data.txt') as file:
while (chunk := file.read(10000)):
print(chunk, end='')
The := operator used in this example assigns to a variable and
returns its value so that it can be tested by the while loop to break
out. When the end of a file is reached, read() returns an empty
string. An alternate way to write above the function is using break as
follows:
with open('data.txt') as file:
while True:
chunk = file.read(10000)
if not chunk:
break
print(chunk, end='')
To make the output of a program go to a file, supply a file argument
to the print() function as shown in this example:
with open('out.txt', 'wt') as out:
while year <= numyears:
principal = principal * (1 + rate)
print(f'{year:>3d} {principal:0.2f}', file=out)
year += 1
In addition, file objects support a write() method that can be used
to write string data. For example, the print() function in the
previous example could have been written this way:
out.write(f'{year:3d} {principal:0.2f}n')
By default, files contain text encoded as UTF-8 Unicode. If you’re
working with a different text encoding, use the extra encoding
argument when opening the file. For example:
with open('data.txt', encoding='latin-1') as file:
data = file.read()
36. Sometimes you might want to read data typed interactively at the
keyboard. To do that, use the input() function. For example:
name = input('Enter your name : ')
print('Hello', name)
input() returns all of the typed text up to the terminating newline,
which is not included.
Lists
Lists are an ordered collection of arbitrary objects. You create a list
by enclosing values in square brackets, as follows:
names = [ 'Dave', 'Paula', 'Thomas', 'Lewis' ]
Lists are indexed by integers, starting with zero. Use the indexing
operator to access and modify individual items of the list:
a = names[2] # Returns the third item of the list,
'Thomas'
names[2] = 'Tom' # Changes the third item to 'Tom'
print(names[-1]) # Print the last item ('Lewis')
To append new items to the end of a list, use the append() method:
names.append('Alex')
To insert an item in the list at a specific position, use the insert()
method:
names.insert(2, 'Aya')
To iterate over the items in a list, use a for-loop:
for name in names:
print(name)
37. You can extract or reassign a portion of a list by using the slicing
operator:
b = names[0:2] # b -> ['Dave', 'Paula']
c = names[2:] # c -> ['Aya', 'Tom', 'Lewis', 'Alex']
names[1] = 'Becky' # Replaces 'Paula' with 'Becky'
names[0:2] = ['Dave', 'Mark', 'Jeff'] # Replace the first two
items
# with
['Dave','Mark','Jeff']
Use the plus (+) operator to concatenate lists:
a = ['x','y'] + ['z','z','y'] # Result is
['x','y','z','z','y']
An empty list is created in one of two ways:
names = [] # An empty list
names = list() # An empty list
Specifying [] for an empty list is more idiomatic. list is the name of
the class associated with the list type. It’s more common to see it
used when performing conversions of data to a list. For example:
letters = list('Dave') # letters = ['D', 'a', 'v', 'e']
Most of the time, all of the items in a list are of the same type (for
example, a list of numbers or a list of strings). However, lists can
contain any mix of Python objects, including other lists, as in the
following example:
a = [1, 'Dave', 3.14, ['Mark', 7, 9, [100, 101]], 10]
Items contained in nested lists are accessed by applying more than
one indexing operation as follows:
a[1] # Returns 'Dave'
a[3][2] # Returns 9
a[3][3][1] # Returns 101
38. The following program pcost.py illustrates how to read data into a
list and perform a simple calculation. In this example, lines are
assumed to contain comma-separated values. The program
computes the sum of the product of two columns.
# pcost.py
#
# Reads input lines of the form 'NAME,SHARES,PRICE'.
# For example:
#
# SYM,123,456.78
import sys
if len(sys.argv) != 2:
raise SystemExit(f'Usage: {sys.argv[0]} filename')
rows = []
with open(sys.argv[1], 'rt') as file:
for line in file:
rows.append(line.split(','))
# rows is a list of this form
# [
# ['SYM', '123', '456.78']
# ...
# ]
total = sum([int(row[1]) * float(row[2]) for row in rows ])
print(f'Total cost: {total:0.2f}')
The first line of this program uses the import statement to load the
sys module from the Python library. This module is being loaded in
order to obtain command-line arguments which are found in the list
sys.argv. The initial check makes sure that a filename has been
provided. If not, a SystemExit exception is raised with a helpful error
39. message. In this message, sys.argv[0] contains the name of the
program that’s running.
The open() function uses the filename that was specified on the
command line. The for line in file loop is reading the file line-by-
line. Each line is split into a small list using the comma character as
a delimiter. This list is appended to rows. The final result, rows, is a
list of lists—remember that a list can contain anything including
other lists.
The expression [ int(row[1]) * float(row[2]) for row in rows ]
constructs a new list by looping over all of the lists in rows and
computing the product of the second and third items. This useful
technique for constructing a list is known as a list comprehension.
The same computation could have been expressed more verbosely
as follows:
values = []
for row in rows:
values.append(int(row[1]) * float(row[2]))
total = sum(values)
As a general rule, list comprehensions are a preferred technique for
performing simple calculations. The built-in sum() function computes
the sum for all items in a sequence.
Tuples
To create simple data structures, you can pack a collection of values
together into an immutable object known as a tuple. You create a
tuple by enclosing a group of values in parentheses like this:
holding = ('GOOG', 100, 490.10)
address = ('www.python.org', 80)
For completeness, 0- and 1-element tuples can be defined, but have
special syntax:
40. a = () # 0-tuple (empty tuple)
b = (item,) # 1-tuple (note the trailing comma)
The values in a tuple can be extracted by numerical index just like a
list. However, it is more common to unpack tuples into a set of
variables like this:
name, shares, price = holding
host, port = address
Although tuples support most of the same operations as lists (such
as indexing, slicing, and concatenation), the elements of a tuple
cannot be changed after creation (that is, you cannot replace,
delete, or append new elements to an existing tuple). This reflects
the fact that a tuple is best viewed as a single immutable object
consisting of several parts, not as a collection of distinct objects like
a list.
Tuples and lists are often used together to represent data. For
example, this program shows how you might read a file consisting of
different columns of data separated by commas:
# File containing lines of the form "name,shares,price"
filename = 'portfolio.csv'
portfolio = []
with open(filename) as file:
for line in file:
row = line.split(',')
name = row[0]
shares = int(row[1])
price = float(row[2])
holding = (name, shares, price)
portfolio.append(holding)
The resulting portfolio list created by this program looks like a two-
dimensional array of rows and columns. Each row is represented by
a tuple and can be accessed as follows:
41. >>> portfolio[0]
('AA', 100, 32.2)
>>> portfolio[1]
('IBM', 50, 91.1)
>>>
Individual items of data can be accessed like this:
>>> portfolio[1][1]
50
>>> portfolio[1][2]
91.1
>>>
Here’s how to loop over all of the records and unpack fields into a
set of variables:
total = 0.0
for name, shares, price in portfolio:
total += shares * price
Alternatively, you could use a list comprehension to perform this
calculation:
total = sum([shares * price for _, shares, price in
portfolio])
When iterating over tuples, the variable _ is sometimes used to
indicate a discarded value. In the above calculation, it means we’re
ignoring the first item (the name).
Sets
A set is an unordered collection of unique objects and is used to find
distinct values or to manage problems related to membership. To
create a set, enclose a collection of values in curly braces or give an
existing collection of items to set(). For example: