Contents
I Preface
II Author
III Table of Contents
1
*args and **kwargs
. . . . . . . . . . . . . . . . . . . . . . . . . .
1.1 Usage of *args .
1.2 Usage of **kwargs . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Using *args and **kwargs to call a function . . . . . . . . . . . . .
1.4 When to use them? .
. . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
2 Debugging
3 Generators
.
Iterable .
Iterator .
.
Iteration .
.
3.1
.
3.2
3.3
.
3.4 Generators .
.
.
.
.
.
.
. .
.
.
.
.
4 Map & Filter
4.1 Map .
4.2
Filter .
. .
.
.
. .
.
.
. .
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
2
3
4
5
5
6
6
7
8
10
10
11
11
11
14
14
15
i
5
set Data Structure
6 Ternary Operators
7 Decorators
Everything in python is an object:
. . . . . . . . . . . . . . . . . .
7.1
7.2 Defining functions within functions: . . . . . . . . . . . . . . . . .
Returning functions from within functions: . . . . . . . . . . . . .
7.3
7.4 Giving a function as an argument to another function:
. . . . . .
7.5 Writing your first decorator:
. . . . . . . . . . . . . . . . . . . . .
7.6 Decorators with Arguments . . . . . . . . . . . . . . . . . . . . . .
8 Global & Return
8.1 Multiple return values . . . . . . . . . . . . . . . . . . . . . . . . .
9 Mutation
10 __slots__ Magic
11 Virtual Environment
12 Collections
.
12.1 defaultdict .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
12.2 counter .
.
. . . . . . . . . . . . . . . . . . . . . . . . . .
.
12.3 deque .
.
. . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
12.4 namedtuple .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
12.5 enum.Enum (Python 3.4+) . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
13 Enumerate
14 Object introspection
14.1 dir .
.
.
14.2 type and id .
.
14.3 inspect module
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
15 Comprehensions
15.1 list comprehensions
. . . . . . . . . . . . . . . . . . . . . . . . .
15.2 dict comprehensions
. . . . . . . . . . . . . . . . . . . . . . . . .
15.3 set comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . .
16 Exceptions
16.1 Handling multiple exceptions:
. . . . . . . . . . . . . . . . . . . .
16
18
20
20
21
22
23
23
27
30
31
33
36
39
41
41
43
43
45
46
49
51
51
52
52
53
53
54
55
56
56
ii
17 Lambdas
18 One-Liners
19 For - Else
19.1 else clause:
.
.
.
20 Python C extensions
.
20.1 CTypes .
.
.
.
20.2 SWIG .
. .
. .
.
20.3 Python/C API
.
. .
.
.
.
.
21 open Function
22 Targeting Python 2+3
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
23 Coroutines
24 Function caching
24.1 Python 3.2+ .
24.2 Python 2+ .
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
25 Context managers
25.1 Implementing Context Manager as a Class: . . . . . . . . . . . . .
25.2 Handling exceptions . . . . . . . . . . . . . . . . . . . . . . . . . .
25.3 Implementing a Context Manager as a Generator . . . . . . . . .
59
61
63
63
65
65
67
69
74
77
80
82
82
83
84
85
86
87
iii
iv
欢迎加入非盈利Python编程学习交流QQ群783462347,群里免费提供500+本Python书籍!
Note: You can donate me for my hardwork if you want to by buying the dona-
tion version of Intermediate Python from Gumroad. Your help would be greatly
appreciated!
You can also sign up to my mailing list so that you remain in sync with any
major updates to this book or my future projects!
1
欢迎加入非盈利Python编程学习交流QQ群783462347,群里免费提供500+本Python书籍!
Part I
PREFACE
Python is an amazing language with a strong and friendly community of pro-
grammers. However, there is a lack of documentation on what to learn after
getting the basics of Python down your throat. Through this book I aim to solve
this problem. I would give you bits of information about some interesting topics
which you can further explore.
The topics which are discussed in this book open up your mind towards some
nice corners of Python language. This book is an outcome of my desire to have
something like this when I was beginning to learn Python.
If you are a beginner, intermediate or even an advanced programmer there is
something for you in this book.
Please note that this book is not a tutorial and does not teach you Python. The
topics are not explained in depth, instead only the minimum required informa-
tion is given.
I am sure you are as excited as I am so let’s start!
Note: This book is a continuous work in progress. If you find anything which
you can further improve (I know you will find a lot of stuff) then kindly submit
a pull request!
2
欢迎加入非盈利Python编程学习交流QQ群783462347,群里免费提供500+本Python书籍!
Part II
AUTHOR
I am Muhammad Yasoob Ullah Khalid. I have been programming extensively
in Python for over 3 years now. I have been involved in a lot of Open Source
projects. I regularly blog about interesting Python topics over at my blog . In
2014 I also spoke at EuroPython which was held in Berlin.
It is the biggest
Python conference in Europe. If you have an interesting Internship opportunity
for me then I would definitely like to hear from you!
3
欢迎加入非盈利Python编程学习交流QQ群783462347,群里免费提供500+本Python书籍!