Appendix A. Further Reading
There are a large number of books and online resources available forlearning and programming Python. However, if like this book, yourfocus is on the use of Python 3, finding reliable information is madea bit more difficult simply due to the sheer volume of existingmaterial written for earlier Python versions.
In this appendix, we provide a few selected links to material that maybe particularly useful in the context of Python 3 programming and therecipes contained in this book. This is by no means an exhaustivelist of resources, so you should definitely check to see if newtitles or more up-to-date editions of these books have been published.
Online Resources
http://docs.python.org It goes without saying that Pythonsown online documentation is an excellent resource if you need todelve into the finer details of the language and modules. Just makesure youre looking at the documentation for Python 3 and not earlier versions. http://www.python.org/dev/peps Python Enhancement Proposals (PEPs) areinvaluable if you want to understand the motivation for adding newfeatures to the Python language as well as subtle implementationdetails. This is especially true for some of the more advancedlanguage features. In writing this book, the PEPs were often moreuseful than the official documentation. http://pyvideo.org This is a large collection of video presentations and tutorials from past PyCon conferences, user group meetings, and more.It can be an invaluable resource for learning about modern Pythondevelopment. Many of the videos feature Python core developers talkingabout the new features being added in Python 3. http://code.activestate.com/recipes/langs/python The ActiveStatePython recipes site has long been a resource for finding the solutionto thousands of specific programming problems. As of this writing,it contains approximately 300 recipes specific to Python 3. Youllfind that many of its recipes either expand upon topics coveredin this book or focus on more narrowly defined tasks.As such, its a good companion. http://stackoverflow.com/questions/tagged/python Stack Overflowcurrently has more than 175,000 questions tagged as Python-related(and almost 5000 questions specific to Python 3). Although thequality of the questions and answers varies, there is a lot of goodmaterial to be found.
Books for Learning Python
The following books provide an introduction to Python witha focus on Python 3:
- Learning Python , 4th Edition, by Mark Lutz, OReilly & Associates (2009).
- The Quick Python Book , 2nd Edition, by Vernon Ceder, Manning (2010).
- Python Programming for the Absolute Beginner , 3rd Edition, by MichaelDawson, Course Technology PTR (2010).
- Beginning Python: From Novice to Professional , 2nd Edition, by MagnusLie Hetland, Apress (2008).
- Programming in Python 3 , 2nd Edition, by Mark Summerfield, Addison-Wesley (2010).
Advanced Books
The following books provide more advanced coverage and include Python 3topics:
- Programming Python , 4th Edition, by Mark Lutz, OReilly & Associates (2010).
- Python Essential Reference , 4th Edition, by David Beazley, Addison-Wesley (2009).
- Core Python Applications Programming , 3rd Edition, by Wesley Chun, Prentice Hall (2012).
- The Python Standard Library by Example , by Doug Hellmann, Addison-Wesley (2011).
- Python 3 Object Oriented Programming , by Dusty Phillips, Packt Publishing (2010).
- Porting to Python 3 , by Lennart Regebro, CreateSpace (2011), http://python3porting.com.
About the Authors
David Beazley is an independent software developer and book author living in the city of Chicago. He primarily works on programming tools, provide custom software development, and teach practical programming courses for software developers, scientists, and engineers. He is best known for his work with the Python programming language, for which he has created several open-source packages (e.g., Swig and PLY) and authored the acclaimed Python Essential Reference. He also has significant experience with systems programming in C, C++, and assembly language.
Brian K. Jones is a system administrator in the department of computer science at Princeton University.
Chapter 1. Data Structures and Algorithms
Python provides a variety of useful built-in data structures, such as lists, sets, anddictionaries. For the most part, the use of these structures is straightforward. However,common questions concerning searching, sorting, ordering, and filtering often arise. Thus,the goal of this chapter is to discuss common data structures and algorithms involvingdata. In addition, treatment is given to the various data structurescontained in the collections
module.
1.1. Unpacking a Sequence into Separate Variables
Problem
You have an N-element tuple or sequence that you would like to unpack into acollection of N variables.
Solution
Any sequence (or iterable) can be unpacked into variables using a simpleassignment operation. The only requirement is that the number ofvariables and structure match the sequence. For example:
>>>
p
=
(
4
,
5
)
>>>
x
,
y
=
p
>>>
x
4
>>>
y
5
>>>
>>>
data
=
[
'ACME'
,
50
,
91.1
,
(
2012
,
12
,
21
)
]
>>>
name
,
shares
,
price
,
date
=
data
>>>
name
'ACME'
>>>
date
(2012, 12, 21)
>>>
name
,
shares
,
price
,
(
year
,
mon
,
day
)
=
data
>>>
name
'ACME'
>>>
year
2012
>>>
mon
12
>>>
day
21
>>>
If there is a mismatch in the number of elements, youll get an error.For example:
>>>
p
=
(
4
,
5
)
>>>
x
,
y
,
z
=
p
Traceback (most recent call last):
File
""
, line
1
, in
ValueError
:
need more than 2 values to unpack
>>>
Discussion
Unpacking actually works with any object that happens to be iterable,not just tuples or lists. This includes strings, files, iterators,and generators. For example:
>>>
s
=
'Hello'
>>>
a
,
b
,
c
,