About ................................................................................................................................................................................... 1
Chapter 1: Getting started with Python Language ...................................................................................... 2
Section 1.1: Getting Started ........................................................................................................................................... 2
Section 1.2: Creating variables and assigning values ................................................................................................ 6
Section 1.3: Block Indentation ....................................................................................................................................... 9
Section 1.4: Datatypes ................................................................................................................................................. 11
Section 1.5: Collection Types ...................................................................................................................................... 15
Section 1.6: IDLE - Python GUI .................................................................................................................................... 19
Section 1.7: User Input ................................................................................................................................................. 20
Section 1.8: Built in Modules and Functions .............................................................................................................. 21
Section 1.9: Creating a module ................................................................................................................................... 25
Section 1.10: Installation of Python 2.7.x and 3.x ....................................................................................................... 26
Section 1.11: String function - str() and repr() ........................................................................................................... 28
Section 1.12: Installing external modules using pip ................................................................................................... 29
Section 1.13: Help Utility ............................................................................................................................................... 30
Chapter 2: Python Data Types ............................................................................................................................ 32
Section 2.1: String Data Type ..................................................................................................................................... 32
Section 2.2: Set Data Types ....................................................................................................................................... 32
Section 2.3: Numbers data type ................................................................................................................................ 32
Section 2.4: List Data Type ......................................................................................................................................... 33
Section 2.5: Dictionary Data Type ............................................................................................................................. 33
Section 2.6: Tuple Data Type ..................................................................................................................................... 33
Chapter 3: Indentation ............................................................................................................................................. 34
Section 3.1: Simple example ....................................................................................................................................... 34
Section 3.2: How Indentation is Parsed ..................................................................................................................... 34
Section 3.3: Indentation Errors ................................................................................................................................... 35
Chapter 4: Comments and Documentation .................................................................................................. 36
Section 4.1: Single line, inline and multiline comments ............................................................................................ 36
Section 4.2: Programmatically accessing docstrings .............................................................................................. 36
Section 4.3: Write documentation using docstrings ................................................................................................ 37
Chapter 5: Date and Time ...................................................................................................................................... 40
Section 5.1: Parsing a string into a timezone aware datetime object .................................................................... 40
Section 5.2: Constructing timezone-aware datetimes ............................................................................................ 40
Section 5.3: Computing time dierences .................................................................................................................. 42
Section 5.4: Basic datetime objects usage ............................................................................................................... 42
Section 5.5: Switching between time zones .............................................................................................................. 43
Section 5.6: Simple date arithmetic ........................................................................................................................... 43
Section 5.7: Converting timestamp to datetime ...................................................................................................... 44
Section 5.8: Subtracting months from a date accurately ....................................................................................... 44
Section 5.9: Parsing an arbitrary ISO 8601 timestamp with minimal libraries ...................................................... 44
Section 5.10: Get an ISO 8601 timestamp .................................................................................................................. 45
Section 5.11: Parsing a string with a short time zone name into a timezone aware datetime object ................ 45
Section 5.12: Fuzzy datetime parsing (extracting datetime out of a text) ............................................................ 46
Section 5.13: Iterate over dates .................................................................................................................................. 47
Chapter 6: Date Formatting .................................................................................................................................. 48
Section 6.1: Time between two date-times ............................................................................................................... 48
Section 6.2: Outputting datetime object to string .................................................................................................... 48
, Section 6.3: Parsing string to datetime object ......................................................................................................... 48
Chapter 7: Enum .......................................................................................................................................................... 49
Section 7.1: Creating an enum (Python 2.4 through 3.3) ......................................................................................... 49
Section 7.2: Iteration ................................................................................................................................................... 49
Chapter 8: Set ............................................................................................................................................................... 50
Section 8.1: Operations on sets .................................................................................................................................. 50
Section 8.2: Get the unique elements of a list .......................................................................................................... 51
Section 8.3: Set of Sets ................................................................................................................................................ 51
Section 8.4: Set Operations using Methods and Builtins ......................................................................................... 51
Section 8.5: Sets versus multisets .............................................................................................................................. 53
Chapter 9: Simple Mathematical Operators ................................................................................................. 55
Section 9.1: Division ..................................................................................................................................................... 55
Section 9.2: Addition .................................................................................................................................................... 56
Section 9.3: Exponentiation ........................................................................................................................................ 57
Section 9.4: Trigonometric Functions ........................................................................................................................ 58
Section 9.5: Inplace Operations ................................................................................................................................. 59
Section 9.6: Subtraction .............................................................................................................................................. 59
Section 9.7: Multiplication ........................................................................................................................................... 59
Section 9.8: Logarithms .............................................................................................................................................. 60
Section 9.9: Modulus ................................................................................................................................................... 60
Chapter 10: Bitwise Operators ............................................................................................................................. 62
Section 10.1: Bitwise NOT ............................................................................................................................................ 62
Section 10.2: Bitwise XOR (Exclusive OR) .................................................................................................................. 63
Section 10.3: Bitwise AND ............................................................................................................................................ 64
Section 10.4: Bitwise OR .............................................................................................................................................. 64
Section 10.5: Bitwise Left Shift .................................................................................................................................... 64
Section 10.6: Bitwise Right Shift .................................................................................................................................. 65
Section 10.7: Inplace Operations ................................................................................................................................ 65
Chapter 11: Boolean Operators ............................................................................................................................ 66
Section 11.1: `and` and `or` are not guaranteed to return a boolean ...................................................................... 66
Section 11.2: A simple example ................................................................................................................................... 66
Section 11.3: Short-circuit evaluation ......................................................................................................................... 66
Section 11.4: and ........................................................................................................................................................... 67
Section 11.5: or .............................................................................................................................................................. 67
Section 11.6: not ............................................................................................................................................................ 68
Chapter 12: Operator Precedence ...................................................................................................................... 69
Section 12.1: Simple Operator Precedence Examples in python ............................................................................. 69
Chapter 13: Variable Scope and Binding ......................................................................................................... 70
Section 13.1: Nonlocal Variables ................................................................................................................................. 70
Section 13.2: Global Variables .................................................................................................................................... 70
Section 13.3: Local Variables ...................................................................................................................................... 71
Section 13.4: The del command ................................................................................................................................. 72
Section 13.5: Functions skip class scope when looking up names ......................................................................... 73
Section 13.6: Local vs Global Scope ........................................................................................................................... 74
Section 13.7: Binding Occurrence ............................................................................................................................... 76
Chapter 14: Conditionals ......................................................................................................................................... 77
Section 14.1: Conditional Expression (or "The Ternary Operator") ......................................................................... 77
Section 14.2: if, elif, and else ....................................................................................................................................... 77
Section 14.3: Truth Values ........................................................................................................................................... 77
, Section 14.4: Boolean Logic Expressions ................................................................................................................... 78
Section 14.5: Using the cmp function to get the comparison result of two objects ............................................. 80
Section 14.6: Else statement ....................................................................................................................................... 80
Section 14.7: Testing if an object is None and assigning it ...................................................................................... 81
Section 14.8: If statement ............................................................................................................................................ 81
Chapter 15: Comparisons ........................................................................................................................................ 82
Section 15.1: Chain Comparisons ................................................................................................................................ 82
Section 15.2: Comparison by `is` vs `==` ...................................................................................................................... 83
Section 15.3: Greater than or less than ...................................................................................................................... 84
Section 15.4: Not equal to ........................................................................................................................................... 84
Section 15.5: Equal To ................................................................................................................................................. 85
Section 15.6: Comparing Objects ............................................................................................................................... 85
Chapter 16: Loops ....................................................................................................................................................... 87
Section 16.1: Break and Continue in Loops ................................................................................................................ 87
Section 16.2: For loops ................................................................................................................................................ 89
Section 16.3: Iterating over lists .................................................................................................................................. 89
Section 16.4: Loops with an "else" clause .................................................................................................................. 90
Section 16.5: The Pass Statement .............................................................................................................................. 92
Section 16.6: Iterating over dictionaries .................................................................................................................... 93
Section 16.7: The "half loop" do-while ........................................................................................................................ 94
Section 16.8: Looping and Unpacking ....................................................................................................................... 94
Section 16.9: Iterating dierent portion of a list with dierent step size ............................................................... 95
Section 16.10: While Loop ............................................................................................................................................ 96
Chapter 17: Arrays ...................................................................................................................................................... 98
Section 17.1: Access individual elements through indexes ....................................................................................... 98
Section 17.2: Basic Introduction to Arrays ................................................................................................................. 98
Section 17.3: Append any value to the array using append() method .................................................................. 99
Section 17.4: Insert value in an array using insert() method ................................................................................... 99
Section 17.5: Extend python array using extend() method ..................................................................................... 99
Section 17.6: Add items from list into array using fromlist() method ..................................................................... 99
Section 17.7: Remove any array element using remove() method ..................................................................... 100
Section 17.8: Remove last array element using pop() method ............................................................................ 100
Section 17.9: Fetch any element through its index using index() method ........................................................... 100
Section 17.10: Reverse a python array using reverse() method ........................................................................... 100
Section 17.11: Get array buer information through buer_info() method ........................................................ 101
Section 17.12: Check for number of occurrences of an element using count() method .................................... 101
Section 17.13: Convert array to string using tostring() method ............................................................................ 101
Section 17.14: Convert array to a python list with same elements using tolist() method .................................. 101
Section 17.15: Append a string to char array using fromstring() method ........................................................... 101
Chapter 18: Multidimensional arrays .............................................................................................................. 102
Section 18.1: Lists in lists ............................................................................................................................................ 102
Section 18.2: Lists in lists in lists in.. .......................................................................................................................... 102
Chapter 19: Dictionary ............................................................................................................................................ 104
Section 19.1: Introduction to Dictionary ................................................................................................................... 104
Section 19.2: Avoiding KeyError Exceptions ........................................................................................................... 105
Section 19.3: Iterating Over a Dictionary ................................................................................................................. 105
Section 19.4: Dictionary with default values ........................................................................................................... 106
Section 19.5: Merging dictionaries ........................................................................................................................... 107
Section 19.6: Accessing keys and values ................................................................................................................ 107
Section 19.7: Accessing values of a dictionary ....................................................................................................... 108
, Section 19.8: Creating a dictionary .......................................................................................................................... 108
Section 19.9: Creating an ordered dictionary ......................................................................................................... 109
Section 19.10: Unpacking dictionaries using the ** operator ................................................................................. 109
Section 19.11: The trailing comma ............................................................................................................................ 110
Section 19.12: The dict() constructor ........................................................................................................................ 110
Section 19.13: Dictionaries Example ......................................................................................................................... 110
Section 19.14: All combinations of dictionary values .............................................................................................. 111
Chapter 20: List ......................................................................................................................................................... 112
Section 20.1: List methods and supported operators ............................................................................................ 112
Section 20.2: Accessing list values .......................................................................................................................... 117
Section 20.3: Checking if list is empty ..................................................................................................................... 118
Section 20.4: Iterating over a list ............................................................................................................................. 118
Section 20.5: Checking whether an item is in a list ................................................................................................ 119
Section 20.6: Any and All .......................................................................................................................................... 119
Section 20.7: Reversing list elements ...................................................................................................................... 120
Section 20.8: Concatenate and Merge lists ............................................................................................................ 120
Section 20.9: Length of a list .................................................................................................................................... 121
Section 20.10: Remove duplicate values in list ....................................................................................................... 121
Section 20.11: Comparison of lists ............................................................................................................................ 122
Section 20.12: Accessing values in nested list ........................................................................................................ 122
Section 20.13: Initializing a List to a Fixed Number of Elements ........................................................................... 123
Chapter 21: List comprehensions ...................................................................................................................... 125
Section 21.1: List Comprehensions ........................................................................................................................... 125
Section 21.2: Conditional List Comprehensions ...................................................................................................... 127
Section 21.3: Avoid repetitive and expensive operations using conditional clause ............................................ 129
Section 21.4: Dictionary Comprehensions ............................................................................................................... 130
Section 21.5: List Comprehensions with Nested Loops .......................................................................................... 131
Section 21.6: Generator Expressions ........................................................................................................................ 133
Section 21.7: Set Comprehensions ........................................................................................................................... 135
Section 21.8: Refactoring filter and map to list comprehensions ......................................................................... 135
Section 21.9: Comprehensions involving tuples ...................................................................................................... 136
Section 21.10: Counting Occurrences Using Comprehension ............................................................................... 137
Section 21.11: Changing Types in a List .................................................................................................................... 137
Section 21.12: Nested List Comprehensions ............................................................................................................ 137
Section 21.13: Iterate two or more list simultaneously within list comprehension .............................................. 138
Chapter 22: List slicing (selecting parts of lists) ....................................................................................... 139
Section 22.1: Using the third "step" argument ........................................................................................................ 139
Section 22.2: Selecting a sublist from a list ............................................................................................................ 139
Section 22.3: Reversing a list with slicing ................................................................................................................ 139
Section 22.4: Shifting a list using slicing .................................................................................................................. 139
Chapter 23: groupby() ............................................................................................................................................ 141
Section 23.1: Example 4 ............................................................................................................................................. 141
Section 23.2: Example 2 ............................................................................................................................................ 141
Section 23.3: Example 3 ............................................................................................................................................ 142
Chapter 24: Linked lists ......................................................................................................................................... 144
Section 24.1: Single linked list example ................................................................................................................... 144
Chapter 25: Linked List Node ............................................................................................................................. 148
Section 25.1: Write a simple Linked List Node in python ....................................................................................... 148
Chapter 26: Filter ...................................................................................................................................................... 149