Académique Documents
Professionnel Documents
Culture Documents
1ovice to Expert
BIRMINGHAM - MUMBAI
Python: Journey from 1ovice to (xpert
All rights reserved. No part of this course may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this course to ensure the accuracy
of the information presented. However, the information contained in this course
is sold without warranty, either express or implied. Neither the authors, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this course.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this course by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
Livery Place
35 Livery Street
ISBN 978-1-78712-076-1
www.packtpub.com
Credits
Reviewers
Simone Burol Production Coordinator
Melwyn Dsa
Julio Vicente Trigo Guijarro
Veit Heller
AMahdy AbdElAziz
Grigoriy Beziuk
Krishna Bharadwaj
Justin Cano
Anthony Petitbois
Claudio Rodriguez
Randall Degges
Dave de Fijter
I. de Hoogt
Preface
Python is a dynamic programming language. It is known for its high readability
DQGKHQFHLWLVRIWHQWKHUVWODQJXDJHOHDUQHGE\QHZSURJUDPPHUV3\WKRQEHLQJ
multi-paradigm, it can be used to achieve the same thing in different ways and it is
compatible across different platforms. Coding in Python minimizes development
time and increases productivity in comparison to other languages. Clean,
maintainable code is easy to both read and write using Python's clear, concise syntax.
[i]
Preface
Module 3, Mastering Python, This module is an authoritative guide that will help
you learn new advanced methods in a clear and contextualized way. It starts off
E\FUHDWLQJDSURMHFWVSHFLFHQYLURQPHQWXVLQJYHQYLQWURGXFLQJ\RXWRGLIIHUHQW
Pythonic syntax and common pitfalls before moving on to cover the functional
features in Python. It covers how to create different decorators, generators, and
metaclasses. It also introduces you to functools.wraps and coroutines and how
they work. Later on you will learn to use asyncio module for asynchronous clients
and servers. You will also get familiar with different testing systems such as
py.test, doctest, and unittest, and debugging tools such as Python debugger and
faulthandler. You will learn to optimize application performance so that it works
HIFLHQWO\DFURVVPXOWLSOHPDFKLQHVDQG3\WKRQYHUVLRQV)LQDOO\LWZLOOWHDFK\RX
how to access C functions with a simple Python call. By the end of the module, you
will be able to write more advanced scripts and take on bigger challenges.
You are encouraged to follow the examples in this module. In order to do so, you
will need a computer, an Internet connection, and a browser. The module is written
in Python 3.4, but it should also work with any Python 3.* version. It has written
instructions on how to install Python on the three main operating systems used
today: Windows, Mac, and Linux. This module also explained how to install all
the extra libraries used in the various examples and provided suggestions if the
UHDGHUQGVDQ\LVVXHVGXULQJWKHLQVWDOODWLRQRIDQ\RIWKHP1RSDUWLFXODUHGLWRULV
required to type the code; however, module suggest that those who are interested in
following the examples should consider adopting a proper coding environment.
Module 2:
All the examples in this module rely on the Python 3 interpreter. Make sure you
are not using Python 2.7 or earlier. At the time of writing, Python 3.4 was the latest
release of Python. Most examples will work on earlier revisions of Python 3, but you
are encouraged to use the latest version to minimize frustration. All of the examples
should run on any operating system supported by Python.
If this is not the case, please report it as a bug. Some of the examples need a working
Internet connection. You'll probably want to have one of these for extracurricular
research and debugging anyway! In addition, some of the examples in this module
rely on third-party libraries that do not ship with Python. These are introduced
within the module at the time they are used, so you do not need to install them in
advance. However, for completeness, here is a list:
[ ii ]
Preface
pip
requests
pillow
bitarray
Module 3:
The only hard requirement for this module is a Python interpreter. A Python 3.5 or
newer interpreter is recommended, but many of the code examples will function
in older Python versions, such as 2.7, with a simple from __future__ import print_
VWDWHPHQWDGGHGDWWKHWRSRIWKHOH$GGLWLRQDOO\&KDSWHU([WHQVLRQVLQ&
&6\VWHP&DOOVDQG&&/LEUDULHVUHTXLUHVD&&FRPSLOHUVXFKDV*&&
9LVXDO6WXGLRRU;&RGH$/LQX[PDFKLQHLVE\IDUWKHHDVLHVWWRH[HFXWHWKH&&
examples, but these should function on Windows and OS X machines without too
much effort as well.
Reader feedback
)HHGEDFNIURPRXUUHDGHUVLVDOZD\VZHOFRPH/HWXVNQRZZKDW\RXWKLQNDERXW
this coursewhat you liked or disliked. Reader feedback is important for us as it
helps us develop titles that you will really get the most out of.
If there is a topic that you have expertise in and you are interested in either writing
or contributing to a course, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt course, we have a number of things to
help you to get the most from your purchase.
[ iii ]
Preface
<RXFDQGRZQORDGWKHFRGHOHVE\IROORZLQJWKHVHVWHSV
1. Log in or register to our website using your e-mail address and password.
2. Hover the mouse pointer on the SUPPORT tab at the top.
3. Click on Code Downloads & Errata.
4. Enter the name of the course in the Search box.
5. 6HOHFWWKHFRXUVHIRUZKLFK\RX
UHORRNLQJWRGRZQORDGWKHFRGHOHV
6. Choose from the drop-down menu where you purchased this course from.
7. Click on Code Download.
2QFHWKHOHLVGRZQORDGHGSOHDVHPDNHVXUHWKDW\RXXQ]LSRUH[WUDFWWKHIROGHU
using the latest version of:
:LQ5$5=LSIRU:LQGRZV
=LSHJL=LS8Q5DU;IRU0DF
=LS3HD=LSIRU/LQX[
The code bundle for the course is also hosted on GitHub at https://github.com/
PacktPublishing/Python-Journey-from-Novice-to-Expert. We also have
other code bundles from our rich catalog of books and videos available at https://
github.com/PacktPublishing/. Check them out!
[ iv ]
Preface
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes
GRKDSSHQ,I\RXQGDPLVWDNHLQRQHRIRXUFRXUVHVPD\EHDPLVWDNHLQWKHWH[W
or the codewe would be grateful if you could report this to us. By doing so, you
can save other readers from frustration and help us improve subsequent versions
RIWKLVFRXUVH,I\RXQGDQ\HUUDWDSOHDVHUHSRUWWKHPE\YLVLWLQJhttp://www.
packtpub.com/submit-errata, selecting your course, clicking on the Errata
Submission Form link, and entering the details of your errata. Once your errata are
YHULHG\RXUVXEPLVVLRQZLOOEHDFFHSWHGDQGWKHHUUDWDZLOOEHXSORDGHGWRRXU
website or added to any list of existing errata under the Errata section of that title.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all
media. At Packt, we take the protection of our copyright and licenses very seriously.
If you come across any illegal copies of our works in any form on the Internet, please
provide us with the location address or website name immediately so that we can
pursue a remedy.
We appreciate your help in protecting our authors and our ability to bring you
valuable content.
Questions
If you have a problem with any aspect of this course, you can contact us at
questions@packtpub.com, and we will do our best to address the problem.
[v]
Module 1: Learning Python
i
Table of Contents
ii
Table of Contents
iii
Table of Contents
iv
Table of Contents
Case study 553
Exercises 561
Summary 562
Chapter 6: Python Data Structures 563
Empty objects 563
Tuples and named tuples 565
Dictionaries 568
Lists 575
Sets 581
Extending built-ins 585
Queues 590
Case study 596
Exercises 602
Summary 603
Chapter 7: Python Object-oriented Shortcuts 605
Python built-in functions 605
An alternative to method overloading 613
Functions are objects too 621
Case study 627
Exercises 634
Summary 635
Chapter 8: Strings and Serialization 637
Strings 637
Regular expressions 652
Serializing objects 660
Case study 668
Exercises 673
Summary 675
Chapter 9: The Iterator Pattern 677
Design patterns in brief 677
Iterators 678
Comprehensions 681
Generators 687
Coroutines 692
Case study 699
Exercises 706
Summary 707
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
ix