| List of Figures |
|
xvii | |
| List of Tables |
|
xix | |
| Preface |
|
xxi | |
| Content and Structure |
|
xxiii | |
| Acknowledgements |
|
xxv | |
| About the Authors |
|
xxvii | |
| 1 Introduction to Python Programming |
|
1 | (8) |
|
|
|
1 | (1) |
|
|
|
2 | (1) |
|
1.3 Limitations of Python |
|
|
2 | (1) |
|
|
|
3 | (1) |
|
|
|
3 | (2) |
|
1.5.1 From the Command Line |
|
|
3 | (1) |
|
1.5.2 From the Integrated Development Environment (IDLE) |
|
|
4 | (1) |
|
1.6 Internal Working of Python |
|
|
5 | (1) |
|
|
|
5 | (2) |
|
|
|
7 | (1) |
|
|
|
7 | (1) |
|
|
|
7 | (2) |
| 2 Basics of Python Programming |
|
9 | (24) |
|
|
|
9 | (1) |
|
|
|
9 | (1) |
|
2.2.1 Rules to Declare the Identifier |
|
|
9 | (1) |
|
2.2.2 Characteristics of the Identifier |
|
|
10 | (1) |
|
|
|
10 | (1) |
|
|
|
11 | (1) |
|
2.5 Fundamental Data Types |
|
|
12 | (3) |
|
|
|
12 | (1) |
|
2.5.2 Floating Point Numbers |
|
|
13 | (1) |
|
|
|
13 | (1) |
|
|
|
14 | (1) |
|
|
|
14 | (1) |
|
|
|
15 | (1) |
|
|
|
16 | (7) |
|
|
|
16 | (1) |
|
2.7.2 Floating Point: Float() |
|
|
17 | (1) |
|
2.7.3 Complex Numbers: Complex() |
|
|
17 | (1) |
|
|
|
18 | (1) |
|
|
|
18 | (1) |
|
2.7.6 Bytes Data Type: Bytes() |
|
|
19 | (1) |
|
2.7.7 Byte Array Data Type: Bytearray() |
|
|
19 | (1) |
|
2.7.8 List Data Type: List[ ] |
|
|
20 | (1) |
|
2.7.9 Tuple Data Type: Tuple() |
|
|
20 | (1) |
|
2.7.10 Range Data Type: Range() |
|
|
21 | (1) |
|
2.7.11 Set Data Type: Set{} |
|
|
22 | (1) |
|
2.7.12 Frozenset Data Type: Frozenset() |
|
|
22 | (1) |
|
2.7.13 Dictionary Data Type: Dict{} |
|
|
22 | (1) |
|
2.7.14 None Data Type: None |
|
|
23 | (1) |
|
|
|
23 | (1) |
|
|
|
24 | (2) |
|
2.10 Evaluate: Eval() Function |
|
|
26 | (1) |
|
2.11 Command Line Arguments |
|
|
26 | (1) |
|
2.12 Print: Print() Function |
|
|
27 | (3) |
|
|
|
30 | (1) |
|
|
|
30 | (1) |
|
|
|
30 | (1) |
|
|
|
31 | (2) |
| 3 Operators in Python |
|
33 | (14) |
|
|
|
33 | (1) |
|
|
|
33 | (10) |
|
3.2.1 Arithmetic Operators |
|
|
34 | (1) |
|
3.2.2 Relational Operators |
|
|
35 | (1) |
|
|
|
36 | (2) |
|
|
|
38 | (2) |
|
3.2.5 Assignment Operators |
|
|
40 | (1) |
|
3.2.6 Ternary Operator or Conditional Operator |
|
|
41 | (1) |
|
|
|
42 | (5) |
|
3.2.7.1 Identity Operator |
|
|
42 | (1) |
|
3.2.7.2 Membership Operator |
|
|
43 | (1) |
|
|
|
43 | (2) |
|
|
|
45 | (1) |
|
|
|
45 | (1) |
|
|
|
45 | (2) |
| 4 Control Flow in Python |
|
47 | (12) |
|
|
|
47 | (1) |
|
4.2 Conditional Statements |
|
|
47 | (3) |
|
|
|
48 | (1) |
|
|
|
48 | (1) |
|
4.2.3 If-Elif-Else Statement |
|
|
48 | (2) |
|
|
|
50 | (3) |
|
|
|
50 | (2) |
|
|
|
52 | (1) |
|
|
|
53 | (1) |
|
|
|
53 | (2) |
|
|
|
53 | (1) |
|
|
|
54 | (1) |
|
|
|
55 | (1) |
|
4.5 Loops with Else Block |
|
|
55 | (1) |
|
|
|
56 | (1) |
|
|
|
56 | (1) |
|
|
|
57 | (2) |
| 5 Strings |
|
59 | (24) |
|
|
|
59 | (1) |
|
5.2 Multiline String Literals |
|
|
59 | (1) |
|
5.3 Accessing Characters of String |
|
|
60 | (3) |
|
|
|
60 | (1) |
|
5.3.2 By Using Slice Operator |
|
|
61 | (4) |
|
5.3.2.1 Behavior of Slice Operator |
|
|
61 | (1) |
|
5.3.2.2 Slice Operator Case Study |
|
|
62 | (1) |
|
5.4 Mathematical Operators for Strings |
|
|
63 | (1) |
|
|
|
63 | (1) |
|
|
|
64 | (1) |
|
|
|
65 | (7) |
|
5.7.1 Comparison of Strings |
|
|
65 | (1) |
|
5.7.2 Removing Spaces from a String |
|
|
65 | (1) |
|
5.7.3 Finding Sub-strings |
|
|
66 | (1) |
|
|
|
66 | (1) |
|
5.7.5 Methods to Count Sub-string in the Main String |
|
|
67 | (1) |
|
5.7.6 Replacing a String with Another String |
|
|
68 | (1) |
|
5.7.7 Splitting of Strings |
|
|
68 | (1) |
|
5.7.8 Method to Join the Strings |
|
|
69 | (1) |
|
5.7.9 Methods to Change the Case of a String |
|
|
69 | (1) |
|
5.7.10 Method to Check the Start and End Part of String |
|
|
70 | (1) |
|
5.7.11 Methods for Checking the Type of Characters |
|
|
70 | (2) |
|
5.8 Formatting of the Strings |
|
|
72 | (8) |
|
5.8.1 Case 1: Formatting (Default, Positional, and Keyword Arguments) |
|
|
72 | (1) |
|
5.8.2 Case 2: Formatting of Numbers |
|
|
72 | (1) |
|
5.8.3 Case 3: Formatting for Signed Numbers |
|
|
73 | (1) |
|
5.8.4 Case 4: Number Formatting with Alignment |
|
|
73 | (1) |
|
5.8.5 Case 5: String Formatting with Format() |
|
|
74 | (1) |
|
5.8.6 Case 6: Truncating Strings Using Format() Method |
|
|
74 | (1) |
|
5.8.7 Case 7: Formatting Dictionary Members Using Format() Method |
|
|
74 | (1) |
|
5.8.8 Case 8: Formatting Class Members Using Format() Method |
|
|
75 | (1) |
|
5.8.9 Case 9: Dynamic Formatting Using Format() |
|
|
75 | (1) |
|
5.8.10 Case 10: Dynamic Float Format Template |
|
|
75 | (1) |
|
5.8.11 Case 11: Formatting Date Values |
|
|
76 | (1) |
|
5.8.12 Case 12: Formatting Complex Numbers |
|
|
76 | (4) |
|
|
|
80 | (1) |
|
|
|
81 | (1) |
|
|
|
81 | (2) |
| 6 Data Structures in Python |
|
83 | (32) |
|
|
|
83 | (1) |
|
|
|
83 | (12) |
|
6.2.1 Creation of List Objects |
|
|
84 | (1) |
|
6.2.1.1 With Dynamic Input |
|
|
84 | (1) |
|
6.2.1.2 With List() Function |
|
|
84 | (1) |
|
6.2.1.3 With Split() Function |
|
|
84 | (1) |
|
6.2.2 Accessing Elements of a List |
|
|
84 | (1) |
|
6.2.2.1 By Using an Index |
|
|
85 | (1) |
|
6.2.2.2 By Using a Slice Operator |
|
|
85 | (1) |
|
|
|
85 | (1) |
|
6.2.4 Traversing the Elements of a List |
|
|
85 | (1) |
|
6.2.4.1 By Using the While Loop |
|
|
86 | (1) |
|
6.2.4.2 By Using the For Loop |
|
|
86 | (1) |
|
6.2.5 Important Functions of a List |
|
|
86 | (4) |
|
6.2.5.1 To Get Information about a List |
|
|
86 | (1) |
|
6.2.5.2 Manipulating Elements of a List |
|
|
87 | (2) |
|
6.2.5.3 Ordering Elements of a List |
|
|
89 | (1) |
|
6.2.6 Aliasing and Cloning of List Objects |
|
|
90 | (1) |
|
6.2.6.1 By Using the Slice Operator |
|
|
91 | (1) |
|
6.2.6.2 By Using the Copy() Function |
|
|
91 | (1) |
|
6.2.7 Using Mathematical Operators for List Objects |
|
|
91 | (1) |
|
6.2.7.1 Concatenation Operator(+) |
|
|
91 | (1) |
|
6.2.7.2 Repetition Operator(*) |
|
|
91 | (1) |
|
6.2.8 Comparing List Objects |
|
|
92 | (1) |
|
6.2.9 Membership Operators |
|
|
92 | (1) |
|
|
|
93 | (1) |
|
|
|
93 | (1) |
|
6.2.12 List Comprehensions |
|
|
94 | (1) |
|
|
|
95 | (5) |
|
|
|
95 | (1) |
|
6.3.2 Accessing Elements of a Tuple |
|
|
96 | (1) |
|
6.3.2.1 By Using the Index |
|
|
96 | (1) |
|
6.3.2.2 By Using the Slice Operator |
|
|
96 | (1) |
|
6.3.3 Tuple vs Immutability |
|
|
96 | (1) |
|
6.3.4 Mathematical Operators for a Tuple |
|
|
96 | (1) |
|
6.3.5 Important Functions of a Tuple |
|
|
97 | (1) |
|
6.3.6 Tuple Packing and Unpacking |
|
|
98 | (1) |
|
6.3.7 Tuple Comprehension |
|
|
99 | (1) |
|
|
|
100 | (5) |
|
6.4.1 Creation of Set Objects |
|
|
100 | (1) |
|
6.4.2 Important Functions of a Set |
|
|
101 | (2) |
|
6.4.3 Mathematical Operations on a Set |
|
|
103 | (1) |
|
|
|
104 | (1) |
|
|
|
105 | (6) |
|
6.5.1 Creating a Dictionary |
|
|
105 | (1) |
|
6.5.2 Accessing Data from a Dictionary |
|
|
106 | (1) |
|
6.5.3 Updating a Dictionary |
|
|
106 | (1) |
|
6.5.4 Deleting Elements from a Dictionary |
|
|
107 | (1) |
|
6.5.5 Important Functions of a Dictionary |
|
|
108 | (3) |
|
6.5.6 Dictionary Comprehension |
|
|
111 | (1) |
|
|
|
111 | (1) |
|
|
|
112 | (1) |
|
|
|
112 | (3) |
| 7 Functions |
|
115 | (20) |
|
|
|
115 | (1) |
|
|
|
115 | (1) |
|
|
|
115 | (1) |
|
7.2.2 User-Defined Functions |
|
|
115 | (1) |
|
|
|
116 | (2) |
|
7.4 Arguments in a Function |
|
|
118 | (3) |
|
7.4.1 Positional Arguments |
|
|
118 | (1) |
|
|
|
119 | (1) |
|
|
|
119 | (1) |
|
7.4.4 Variable Length Arguments |
|
|
119 | (2) |
|
|
|
121 | (1) |
|
|
|
121 | (1) |
|
|
|
122 | (1) |
|
|
|
122 | (1) |
|
|
|
123 | (1) |
|
|
|
123 | (1) |
|
|
|
124 | (1) |
|
|
|
125 | (1) |
|
|
|
126 | (1) |
|
|
|
126 | (1) |
|
|
|
127 | (1) |
|
|
|
128 | (3) |
|
7.14.1 Calling of a Same Function Using and without Using a Decorator |
|
|
129 | (1) |
|
7.14.2 Decorator Chaining |
|
|
130 | (1) |
|
|
|
131 | (2) |
|
|
|
133 | (1) |
|
|
|
133 | (1) |
|
|
|
134 | (1) |
| 8 Modules |
|
135 | (12) |
|
|
|
135 | (1) |
|
|
|
136 | (1) |
|
|
|
137 | (1) |
|
|
|
137 | (1) |
|
|
|
138 | (2) |
|
|
|
140 | (2) |
|
|
|
142 | (3) |
|
|
|
145 | (1) |
|
|
|
145 | (1) |
|
|
|
145 | (1) |
|
|
|
146 | (1) |
| 9 Basic Concepts of Object-Oriented Programming |
|
147 | (20) |
|
|
|
147 | (1) |
|
|
|
147 | (1) |
|
|
|
148 | (1) |
|
|
|
149 | (1) |
|
|
|
149 | (1) |
|
|
|
150 | (10) |
|
|
|
150 | (3) |
|
9.6.1.1 Declaring Instance Variables |
|
|
150 | (1) |
|
9.6.1.2 Accessing Instance Variables |
|
|
151 | (1) |
|
9.6.1.3 Deleting Instance Variables |
|
|
152 | (1) |
|
|
|
153 | (6) |
|
9.6.2.1 Declaration of Static Variables |
|
|
153 | (1) |
|
9.6.2.2 Accessing Static Variables |
|
|
154 | (3) |
|
9.6.2.3 Deleting Static Variables |
|
|
157 | (2) |
|
|
|
159 | (1) |
|
|
|
160 | (2) |
|
|
|
160 | (1) |
|
|
|
161 | (1) |
|
|
|
162 | (1) |
|
9.8 Setter and Getter Methods |
|
|
162 | (1) |
|
9.9 Passing Members of One Class to Other Class |
|
|
163 | (1) |
|
|
|
164 | (1) |
|
|
|
164 | (1) |
|
|
|
164 | (3) |
| 10 Advanced Concepts of Object-Oriented Programming |
|
167 | (36) |
|
|
|
167 | (1) |
|
|
|
167 | (2) |
|
|
|
169 | (1) |
|
|
|
170 | (1) |
|
10.5 Finding the Number of References of an Object |
|
|
171 | (1) |
|
|
|
171 | (1) |
|
|
|
172 | (5) |
|
10.7.1 By Composition (HAS-A Relationship) |
|
|
172 | (2) |
|
10.7.2 By Inheritance (IS-A Relationship) |
|
|
174 | (3) |
|
10.8 Aggregation vs Composition |
|
|
177 | (1) |
|
|
|
178 | (4) |
|
10.9.1 Single Inheritance |
|
|
178 | (1) |
|
10.9.2 Multilevel Inheritance |
|
|
179 | (1) |
|
10.9.3 Hierarchical Inheritance |
|
|
179 | (1) |
|
10.9.4 Multiple Inheritances |
|
|
180 | (1) |
|
10.9.5 Hybrid Inheritance |
|
|
181 | (1) |
|
10.9.6 Cyclic Inheritance |
|
|
181 | (1) |
|
10.10 Method Resolution Order (MRO) |
|
|
182 | (1) |
|
|
|
183 | (4) |
|
10.11.1 Calling Method of a Specific Super Class |
|
|
184 | (3) |
|
|
|
187 | (7) |
|
10.12.1 Duck Typing Philosophy |
|
|
187 | (2) |
|
|
|
189 | (3) |
|
10.12.2.1 Operator Overloading |
|
|
189 | (2) |
|
10.12.2.2 Method Overloading |
|
|
191 | (1) |
|
10.12.2.3 Constructor Overloading |
|
|
192 | (1) |
|
|
|
192 | (5) |
|
10.12.3.1 Method Overriding |
|
|
192 | (2) |
|
|
|
194 | (2) |
|
|
|
196 | (1) |
|
|
|
197 | (2) |
|
10.15.1 Concrete Class vs Abstract Class vs Interface |
|
|
197 | (1) |
|
10.15.2 Public, Protected, and Private Attributes |
|
|
198 | (1) |
|
|
|
199 | (1) |
|
|
|
200 | (1) |
|
|
|
200 | (1) |
|
|
|
200 | (3) |
| 11 Exception Handling |
|
203 | (14) |
|
|
|
203 | (1) |
|
|
|
203 | (1) |
|
|
|
204 | (1) |
|
11.4 Default Exception Handling |
|
|
205 | (1) |
|
11.5 Customized Exception Handling: Using Try-Except |
|
|
205 | (1) |
|
11.6 Multiple Except Blocks |
|
|
206 | (3) |
|
11.6.1 Multiple Exceptions in a Single Except Block |
|
|
208 | (1) |
|
11.6.2 Default Except Block |
|
|
208 | (1) |
|
|
|
209 | (1) |
|
11.7.1 Control Flow in Try-Except-Finally |
|
|
210 | (1) |
|
11.8 Nested Try-Except-Finally Block |
|
|
210 | (3) |
|
11.8.1 Control Flow in Nested Try-Except-Finally Block |
|
|
211 | (1) |
|
11.8.2 Else Block with Try-Except-Finally |
|
|
212 | (1) |
|
|
|
213 | (1) |
|
11.10 Raise User-Defined Exception |
|
|
213 | (1) |
|
|
|
214 | (1) |
|
|
|
215 | (1) |
|
|
|
215 | (1) |
|
|
|
215 | (2) |
| 12 File Handling |
|
217 | (18) |
|
|
|
217 | (4) |
|
12.1.1 Opening and Closing a File |
|
|
217 | (1) |
|
12.1.2 Attributes of a File Object |
|
|
218 | (1) |
|
12.1.3 Writing and Reading Data to Text Files |
|
|
218 | (3) |
|
|
|
221 | (1) |
|
12.3 The Seek() and Tell() Methods |
|
|
221 | (1) |
|
|
|
221 | (1) |
|
|
|
221 | (1) |
|
12.4 Testing the Existence of a File |
|
|
222 | (2) |
|
12.5 Handling Binary Data |
|
|
224 | (1) |
|
|
|
224 | (1) |
|
12.6.1 Writing Data to a CSV File |
|
|
224 | (1) |
|
12.6.2 Reading Data from a CSV File |
|
|
225 | (1) |
|
12.7 Zipping and Unzipping Files |
|
|
225 | (2) |
|
12.7.1 To Create a Zip File |
|
|
226 | (1) |
|
12.7.2 To Perform Unzip Operations |
|
|
226 | (1) |
|
|
|
227 | (2) |
|
12.8.1 Operations on a Directory |
|
|
227 | (2) |
|
12.9 To Get Information about a File |
|
|
229 | (1) |
|
12.9.1 Displaying Statistics of a File |
|
|
230 | (1) |
|
12.9.2 To Print Specified Properties |
|
|
230 | (1) |
|
12.10 Pickling and Unpickling of Objects |
|
|
230 | (2) |
|
12.10.1 Reading and Writing the State-of-Objects |
|
|
231 | (1) |
|
12.10.2 Writing Multiple Objects |
|
|
231 | (1) |
|
|
|
232 | (1) |
|
|
|
232 | (1) |
|
|
|
233 | (2) |
| 13 Multithreading |
|
235 | (12) |
|
|
|
235 | (1) |
|
13.2 Ways of Creating a Thread |
|
|
235 | (3) |
|
13.2.1 Creation of a Thread without Using any Class |
|
|
236 | (1) |
|
13.2.2 Creation of a Thread by Extending the Thread Class |
|
|
236 | (1) |
|
13.2.3 Creation of a Thread without Extending the Thread Class |
|
|
237 | (1) |
|
13.3 Setting and Getting Name of a Thread |
|
|
238 | (1) |
|
13.4 Thread Identification Number |
|
|
239 | (1) |
|
13.5 Active_count() Function |
|
|
239 | (1) |
|
|
|
240 | (1) |
|
|
|
240 | (1) |
|
|
|
241 | (2) |
|
|
|
243 | (2) |
|
|
|
245 | (1) |
|
|
|
245 | (1) |
|
|
|
246 | (1) |
| 14 Synchronization |
|
247 | (14) |
|
|
|
247 | (5) |
|
14.1.1 By Using the Lock Concept |
|
|
248 | (1) |
|
|
|
249 | (1) |
|
14.1.3 By Using Semaphore |
|
|
250 | (2) |
|
14.2 Inter-Thread Communication |
|
|
252 | (4) |
|
14.2.1 By Using Event Objects |
|
|
252 | (1) |
|
14.2.2 By Using a Condition Object |
|
|
253 | (2) |
|
|
|
255 | (1) |
|
|
|
256 | (1) |
|
14.3.1 First-In-First-Out Queue |
|
|
256 | (1) |
|
14.3.2 Last-In-First-Out Queue |
|
|
256 | (1) |
|
|
|
256 | (1) |
|
|
|
257 | (2) |
|
|
|
259 | (1) |
|
|
|
259 | (1) |
|
|
|
259 | (2) |
| 15 Regular Expressions and Web Scraping |
|
261 | (16) |
|
|
|
261 | (7) |
|
|
|
262 | (2) |
|
15.1.2 Pre-defined Character Classes |
|
|
264 | (2) |
|
|
|
266 | (2) |
|
15.2 Functions of Re Module |
|
|
268 | (4) |
|
|
|
268 | (1) |
|
|
|
268 | (1) |
|
|
|
269 | (1) |
|
|
|
269 | (1) |
|
|
|
269 | (1) |
|
|
|
270 | (1) |
|
|
|
270 | (1) |
|
|
|
270 | (1) |
|
|
|
271 | (1) |
|
|
|
271 | (1) |
|
|
|
272 | (2) |
|
|
|
274 | (1) |
|
|
|
275 | (1) |
|
|
|
275 | (2) |
| 16 Database Programming |
|
277 | (10) |
|
|
|
277 | (1) |
|
|
|
277 | (1) |
|
|
|
277 | (7) |
|
16.3.1 Python Database Programming |
|
|
278 | (1) |
|
16.3.2 Working with the Oracle Database |
|
|
279 | (3) |
|
16.3.3 Working with the MySQL Database |
|
|
282 | (2) |
|
|
|
284 | (1) |
|
|
|
285 | (1) |
|
|
|
285 | (2) |
| Appendix A: Mini Projects |
|
287 | (14) |
| Appendix B: Socket Programming in Python |
|
301 | (4) |
| Appendix C: Parallel Processing in Python |
|
305 | (4) |
| Index |
|
309 | |