Python
Notes for Professionals
Python
Notes for Professionals
700+ pages
of professional hints and tricks
GoalKicker.com
Free Programming Books
Disclaimer
This is an uno$cial free book created for educational purposes and is
not a$liated with o$cial Python group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
...................................................................................................................................................................................
1
Chapter 1: Getting started with Python Language
......................................................................................
2
...........................................................................................................................................
2
................................................................................................
6
.....................................................................................................................................
.................................................................................................................................................
......................................................................................................................................
....................................................................................................................................
.................................................................................................................................................
..............................................................................................................
...................................................................................................................................
Section 1.1: Getting Started
Section 1.2: Creating variables and assigning values
Section 1.3: Block Indentation
Section 1.4: Datatypes
Section 1.5: Collection Types
Section 1.6: IDLE - Python GUI
Section 1.7: User Input
Section 1.8: Built in Modules and Functions
Section 1.9: Creating a module
Section 1.10: Installation of Python 2.7.x and 3.x
Section 1.11: String function - str() and repr()
Section 1.12: Installing external modules using pip
Section 1.13: Help Utility
.......................................................................................................
...........................................................................................................
...................................................................................................
...............................................................................................................................................
............................................................................................................................
33
Chapter 2: Python Data Types
Section 2.1: String Data Type
Section 2.2: Set Data Types
Section 2.3: Numbers data type
Section 2.4: List Data Type
Section 2.5: Dictionary Data Type
Section 2.6: Tuple Data Type
.....................................................................................................................................
.......................................................................................................................................
................................................................................................................................
.........................................................................................................................................
.............................................................................................................................
.....................................................................................................................................
Chapter 3: Indentation
.............................................................................................................................................
35
Section 3.1: Simple example
Section 3.2: How Indentation is Parsed
Section 3.3: Indentation Errors
.......................................................................................................................................
35
.....................................................................................................................
35
...................................................................................................................................
36
Chapter 4: Comments and Documentation
..................................................................................................
37
Section 4.1: Single line, inline and multiline comments
Section 4.2: Programmatically accessing docstrings
Section 4.3: Write documentation using docstrings
............................................................................................
37
..............................................................................................
................................................................................................
Chapter 5: Date and Time
......................................................................................................................................
41
....................................................................
............................................................................................
..................................................................................................................
...............................................................................................................
..............................................................................................................
Section 5.1: Parsing a string into a timezone aware datetime object
Section 5.2: Constructing timezone-aware datetimes
Section 5.3: Computing time di#erences
Section 5.4: Basic datetime objects usage
Section 5.5: Switching between time zones
Section 5.6: Simple date arithmetic
Section 5.7: Converting timestamp to datetime
Section 5.8: Subtracting months from a date accurately
Section 5.9: Parsing an arbitrary ISO 8601 timestamp with minimal libraries
Section 5.10: Get an ISO 8601 timestamp
Section 5.11: Parsing a string with a short time zone name into a timezone aware datetime object
Section 5.12: Fuzzy datetime parsing (extracting datetime out of a text)
Section 5.13: Iterate over dates
Chapter 6: Date Formatting
..................................................................................................................................
..................................................................................................................................
...........................................................................................................................
..................................................................................................................
......................................................................................................
.......................................................................................
............................................................
......................................................
................
46
Section 6.1: Time between two date-times
Section 6.2: Outputting datetime object to string
...............................................................................................................
....................................................................................................
49
10
11
15
19
21
21
25
26
28
Publicité
29
31
33
33
33
34
34
34
37
38
41
41
43
43
44
44
45
45
45
46
47
48
49
49
Section 6.3: Parsing string to datetime object
.........................................................................................................
49
Chapter 7: Enum
..........................................................................................................................................................
50
Section 7.1: Creating an enum (Python 2.4 through 3.3)
Section 7.2: Iteration
...................................................................................................................................................
50
.........................................................................................
50
Chapter 8: Set
...............................................................................................................................................................
51
Section 8.1: Operations on sets
Section 8.2: Get the unique elements of a list
Section 8.3: Set of Sets
Section 8.4: Set Operations using Methods and Builtins
Section 8.5: Sets versus multisets
..................................................................................................................................
..........................................................................................................
................................................................................................................................................
..............................................................................................................................
54
.........................................................................................
52
Chapter 9: Simple Mathematical Operators
.................................................................................................
56
Section 9.1: Division
Section 9.2: Addition
Section 9.3: Exponentiation
Section 9.4: Trigonometric Functions
Section 9.5: Inplace Operations
Section 9.6: Subtraction
Section 9.7: Multiplication
Section 9.8: Logarithms
Section 9.9: Modulus
.....................................................................................................................................................
....................................................................................................................................................
........................................................................................................................................
........................................................................................................................
59
.................................................................................................................................
..............................................................................................................................................
...........................................................................................................................................
..............................................................................................................................................
...................................................................................................................................................
Chapter 10: Bitwise Operators
.............................................................................................................................
63
Section 10.1: Bitwise NOT
Section 10.2: Bitwise XOR (Exclusive OR)
Section 10.3: Bitwise AND
Section 10.4: Bitwise OR
Section 10.5: Bitwise Left Shift
Section 10.6: Bitwise Right Shift
Section 10.7: Inplace Operations
Chapter 11: Boolean Operators
............................................................................................................................................
63
..................................................................................................................
64
............................................................................................................................................
..............................................................................................................................................
....................................................................................................................................
..................................................................................................................................
................................................................................................................................
............................................................................................................................
Section 11.1: and and or are not guaranteed to return a boolean
Section 11.2: A simple example
Section 11.3: Short-circuit evaluation
Section 11.4: and
Section 11.5: or
Section 11.6: not
...........................................................................................................................................................
..............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................
.........................................................................................................................
......................................................................
67
Chapter 12: Operator Precedence
......................................................................................................................
70
Section 12.1: Simple Operator Precedence Examples in python
.............................................................................
70
Chapter 13: Variable Scope and Binding
.........................................................................................................
71
.................................................................................................................................
....................................................................................................................................
......................................................................................................................................
.................................................................................................................................
Section 13.1: Nonlocal Variables
Section 13.2: Global Variables
Section 13.3: Local Variables
Section 13.4: The del command
Section 13.5: Functions skip class scope when looking up names
Section 13.6: Local vs Global Scope
Section 13.7: Binding Occurrence
...........................................................................................................................
...............................................................................................................................
.........................................................................
74
Publicité
Chapter 14: Conditionals
.........................................................................................................................................
78
Section 14.1: Conditional Expression (or "The Ternary Operator")
Section 14.2: if, elif, and else
Section 14.3: Truth Values
.......................................................................................................................................
...........................................................................................................................................
.........................................................................
78
51
52
52
56
57
58
60
60
60
61
61
65
65
65
66
66
67
67
67
68
68
69
71
71
72
73
75
77
78
78
...................................................................................................................
79
Section 14.4: Boolean Logic Expressions
Section 14.5: Using the cmp function to get the comparison result of two objects
Section 14.6: Else statement
Section 14.7: Testing if an object is None and assigning it
Section 14.8: If statement
Chapter 15: Comparisons
............................................................................................................................................
........................................................................................................................................
................................................................................................................................
.......................................................................................................................................
......................................................................................
.............................................
81
Section 15.1: Chain Comparisons
Section 15.2: Comparison by is vs ==
Section 15.3: Greater than or less than
Section 15.4: Not equal to
Section 15.5: Equal To
Section 15.6: Comparing Objects
......................................................................................................................
84
......................................................................................................................
85
...........................................................................................................................................
.................................................................................................................................................
...............................................................................................................................
Chapter 16: Loops
.......................................................................................................................................................
88
................................................................................................................
................................................................................................................................................
Section 16.1: Break and Continue in Loops
Section 16.2: For loops
Section 16.3: Iterating over lists
Section 16.4: Loops with an "else" clause
Section 16.5: The Pass Statement
Section 16.6: Iterating over dictionaries
Section 16.7: The "half loop" do-while
Section 16.8: Looping and Unpacking
Section 16.9: Iterating di#erent portion of a list with di#erent step size
Section 16.10: While Loop
..................................................................................................................................
..................................................................................................................
..............................................................................................................................
....................................................................................................................
........................................................................................................................
.......................................................................................................................
............................................................................................................................................
97
...............................................................
96
Chapter 17: Arrays
......................................................................................................................................................
99
.......................................................................................
.................................................................................................................
................................................................
................................................................................
...................................................................................
Section 17.1: Access individual elements through indexes
Section 17.2: Basic Introduction to Arrays
Section 17.3: Append any value to the array using append() method
Section 17.4: Insert value in an array using insert() method
Section 17.5: Extend python array using extend() method
Section 17.6: Add items from list into array using fromlist() method
Section 17.7: Remove any array element using remove() method
Section 17.8: Remove last array element using pop() method
Section 17.9: Fetch any element through its index using index() method
Section 17.10: Reverse a python array using reverse() method
Section 17.11: Get array bu#er information through bu#er_info() method
Section 17.12: Check for number of occurrences of an element using count() method
Section 17.13: Convert array to string using tostring() method
Section 17.14: Convert array to a python list with same elements using tolist() method
Section 17.15: Append a string to char array using fromstring() method
..................................................................
.....................................................................
............................................................................
...........................................................
...........................................................................
........................................................
....................................
............................................................................
..................................
102
...........................................................
102
Chapter 18: Multidimensional arrays
..............................................................................................................
103
Publicité
Section 18.1: Lists in lists
Section 18.2: Lists in lists in lists in..
............................................................................................................................................
103
..........................................................................................................................
103
Chapter 19: Dictionary
............................................................................................................................................
105
Section 19.1: Introduction to Dictionary
Section 19.2: Avoiding KeyError Exceptions
Section 19.3: Iterating Over a Dictionary
Section 19.4: Dictionary with default values
Section 19.5: Merging dictionaries
Section 19.6: Accessing keys and values
Section 19.7: Accessing values of a dictionary
...................................................................................................................
...........................................................................................................
.................................................................................................................
106
...........................................................................................................
...........................................................................................................................
................................................................................................................
.......................................................................................................
109
81
82
82
83
83
85
86
86
88
90
90
91
93
94
95
95
99
99
100
100
100
101
101
101
101
101
102
102
102
105
106
107
108
108
Section 19.8: Creating a dictionary
Section 19.9: Creating an ordered dictionary
Section 19.10: Unpacking dictionaries using the ** operator
Section 19.11: The trailing comma
Section 19.12: The dict() constructor
Section 19.13: Dictionaries Example
Section 19.14: All combinations of dictionary values
..........................................................................................................................
.........................................................................................................
.................................................................................
110
............................................................................................................................
........................................................................................................................
.........................................................................................................................
..............................................................................................
Chapter 20: List
.........................................................................................................................................................
113
............................................................................................
..........................................................................................................................
.....................................................................................................................
.............................................................................................................................
................................................................................................
Section 20.1: List methods and supported operators
Section 20.2: Accessing list values
Section 20.3: Checking if list is empty
Section 20.4: Iterating over a list
Section 20.5: Checking whether an item is in a list
Section 20.6: Any and All
Section 20.7: Reversing list elements
Section 20.8: Concatenate and Merge lists
Section 20.9: Length of a list
Section 20.10: Remove duplicate values in list
Section 20.11: Comparison of lists
Section 20.12: Accessing values in nested list
Section 20.13: Initializing a List to a Fixed Number of Elements
..........................................................................................................................................
......................................................................................................................
............................................................................................................
....................................................................................................................................
.......................................................................................................
............................................................................................................................
........................................................................................................
...........................................................................
124
......................................................................................................................
...........................................................................................................................
......................................................................................................
Chapter 21: List comprehensions
Section 21.1: List Comprehensions
Section 21.2: Conditional List Comprehensions
Section 21.3: Avoid repetitive and expensive operations using conditional clause
Section 21.4: Dictionary Comprehensions
Section 21.5: List Comprehensions with Nested Loops
Section 21.6: Generator Expressions
Section 21.7: Set Comprehensions
Section 21.8: Refactoring lter and map to list comprehensions
Section 21.9: Comprehensions involving tuples
Section 21.10: Counting Occurrences Using Comprehension
Section 21.11: Changing Types in a List
Section 21.12: Nested List Comprehensions
Section 21.13: Iterate two or more list simultaneously within list comprehension
...............................................................................................................
..........................................................................................
........................................................................................................................
...........................................................................................................................
.........................................................................
......................................................................................................
...............................................................................
....................................................................................................................
............................................................................................................
..............................................
Publicité
............................................
130
Chapter 22: List slicing (selecting parts of lists)
.......................................................................................
140
Section 22.1: Using the third "step" argument
Section 22.2: Selecting a sublist from a list
Section 22.3: Reversing a list with slicing
Section 22.4: Shifting a list using slicing
........................................................................................................
140
............................................................................................................
................................................................................................................
..................................................................................................................
Chapter 23: groupby()
Section 23.1: Example 4
Section 23.2: Example 2
Section 23.3: Example 3
Chapter 24: Linked lists
............................................................................................................................................
.............................................................................................................................................
............................................................................................................................................
............................................................................................................................................
.........................................................................................................................................
145
Section 24.1: Single linked list example
...................................................................................................................
145
Chapter 25: Linked List Node
.............................................................................................................................
149
Section 25.1: Write a simple Linked List Node in python
.......................................................................................
149
Chapter 26: Filter
......................................................................................................................................................
150
109
110
111
111
111
112
113
118
119
119
120
120
121
121
122
122
123
123
126
126
128
131
132
134
136
136
137
138
138
138
139
140
140
140
142
142
142
143
Section 26.1: Basic use of lter
Section 26.2: Filter without function
Section 26.3: Filter as short-circuit check
Section 26.4: Complementary function: lterfalse, ilterfalse
.................................................................................................................................
........................................................................................................................
...............................................................................................................
..............................................................................
151
Chapter 27: Heapq
...................................................................................................................................................
153
Section 27.1: Largest and smallest items in a collection
Section 27.2: Smallest item in a collection
.......................................................................................
153
..............................................................................................................
.....................................................................................................................................................
.....................................................................................................................................................
Chapter 28: Tuple
Section 28.1: Tuple
Section 28.2: Tuples are immutable
Section 28.3: Packing and Unpacking Tuples
Section 28.4: Built-in Tuple Functions
Section 28.5: Tuple Are Element-wise Hashable and Equatable
Section 28.6: Indexing Tuples
Section 28.7: Reversing Elements
........................................................................................................................
........................................................................................................
.....................................................................................................................
.........................................................................
158
...................................................................................................................................
............................................................................................................................
Chapter 29: Basic Input and Output
Section 29.1: Using the print function
Section 29.2: Input from a File
Section 29.3: Read from stdin
Section 29.4: Using input() and raw_input()
Section 29.5: Function to prompt user for a number
Section 29.6: Printing a string without a newline at the end
...............................................................................................................
......................................................................................................................
.................................................................................................................................
..................................................................................................................................
..........................................................................................................
............................................................................................
.................................................................................
163
Chapter 30: Files & Folders I/O
.........................................................................................................................
165
............................................................................................................................................
..................................................................................................................
....................................................................................................................
.........................................................