Table of Contents:
PYTHON BASICS………………………………………………………………………………………………. 1
- Python – Overview……………………………………………………………………………………….. 2
- Python – History………………………………………………………………………………………….. 4
- Python – Features………………………………………………………………………………………… 6
- Python vs C++…………………………………………………………………………………………….. 9
- Python – Hello World………………………………………………………………………………….. 12
- Python – Application Areas……………………………………………………………………………. 15
- Python – Interpreter……………………………………………………………………………………. 18
- Python – Environment Setup…………………………………………………………………………. 23
Install Python on MacOS……………………………………………………………………………………………………. 26
Install Python from Source Code………………………………………………………………………………………… 27
Python Command Line Options………………………………………………………………………………………….. 30
Python Environment Variables…………………………………………………………………………………………… 31
- Python – Virtual Environment………………………………………………………………………… 34
- Python – Basic Syntax………………………………………………………………………………….. 37
Python Keywords………………………………………………………………………………………………………………. 37
Python Identifiers……………………………………………………………………………………………………………… 39
Python Indents………………………………………………………………………………………………………………….. 40
Python Statements……………………………………………………………………………………………………………. 41
- Python – Variables……………………………………………………………………………………… 43
Naming Convention…………………………………………………………………………………………………………… 44
Assignment Statement………………………………………………………………………………………………………. 45
Multiple Assignments………………………………………………………………………………………………………… 46
- Python – Data Types……………………………………………………………………………………. 49
Number Type……………………………………………………………………………………………………………………. 49
Sequence Types………………………………………………………………………………………………………………… 50
Strings in Python……………………………………………………………………………………………………………….. 50
List in Python…………………………………………………………………………………………………………………….. 51
Tuples in Python……………………………………………………………………………………………………………….. 52
Dictionary Type…………………………………………………………………………………………………………………. 53
Set Type……………………………………………………………………………………………………………………………. 54
- Python – Type Casting…………………………………………………………………………………. 56
Implicit Casting in Python………………………………………………………………………………………………….. 56
int() Function…………………………………………………………………………………………………………………….. 57
float() Function…………………………………………………………………………………………………………………. 59
str() Function…………………………………………………………………………………………………………………….. 60
Conversion of Sequence types…………………………………………………………………………………………… 62
- Python – Unicode System……………………………………………………………………………… 64
- Python – Literals………………………………………………………………………………………… 66
Integer Literal……………………………………………………………………………………………………………………. 66
Float Literal………………………………………………………………………………………………………………………. 67
Complex Literal…………………………………………………………………………………………………………………. 67
String Literal……………………………………………………………………………………………………………………… 68
List Literal…………………………………………………………………………………………………………………………. 69
Tuple Literal……………………………………………………………………………………………………………………… 69
Dictionary Literal………………………………………………………………………………………………………………. 70
- Python – Operators…………………………………………………………………………………….. 71
Types of Operators……………………………………………………………………………………………………………. 71
- Python – Arithmetic Operators………………………………………………………………………. 72
Python – Addition Operator (+)………………………………………………………………………………………….. 72
Python – Subtraction Operator (-)………………………………………………………………………………………. 73
Python – Multiplication Operator (*)………………………………………………………………………………….. 74
Python – Division Operator (/)…………………………………………………………………………………………… 75
Python – Modulus Operator (%)…………………………………………………………………………………………. 77
Python – Exponent Operator (**)………………………………………………………………………………………. 78
Python – Floor Division Operator (//)…………………………………………………………………………………. 80
Python – Complex Number Arithmetic……………………………………………………………………………….. 80
- Python – Assignment Operators……………………………………………………………………… 83
- Python – Augmented Addition Operator (+=)………………………………………………………. 84
Python – Augmented Subtraction Operator (-=)………………………………………………………………….. 85
Python – Augmented Multiplication Operator (*=)……………………………………………………………… 85
Python – Augmented Division Operator (/=)……………………………………………………………………….. 86
Python – Augmented Modulus Operator (%=)…………………………………………………………………….. 87
Python – Augmented Exponent Operator (**=)………………………………………………………………….. 88
Python – Augmented Floor division Operator (//=)……………………………………………………………… 89
- Python – Comparison Operators……………………………………………………………………… 90
- Python – Logical Operators……………………………………………………………………………. 96
- Python – Bitwise Operators…………………………………………………………………………. 100
Python – Bitwise AND Operator (&)………………………………………………………………………………….. 100
Python – Bitwise OR Operator (|)…………………………………………………………………………………….. 101
Python – Binary XOR Operator (^)……………………………………………………………………………………. 102
Python – Binary NOT Operator (~)……………………………………………………………………………………. 102
Python – Left Shift Operator (<<)……………………………………………………………………………………… 103
Python – Right Shift Operator (>>)……………………………………………………………………………………. 103
- Python – Membership Operators…………………………………………………………………… 105
- Python – Identity Operators…………………………………………………………………………. 108
- Python – Comments………………………………………………………………………………….. 110
- Python – User Input…………………………………………………………………………………… 111
input() Function………………………………………………………………………………………………………………. 111
Numeric Input…………………………………………………………………………………………………………………. 113
- Python – Numbers……………………………………………………………………………………. 117
Python – Integers……………………………………………………………………………………………………………. 117
Python – Floating Point Numbers…………………………………………………………………………………….. 120
Python – Complex Numbers…………………………………………………………………………………………….. 123
- Python – Booleans……………………………………………………………………………………. 126
PYTHON CONTROL STATEMENTS…………………………………………………………………… 127
- Python – Control Flow………………………………………………………………………………… 128
- Python – Decision Making…………………………………………………………………………… 130
Python – The if Statement……………………………………………………………………………………………….. 130
- Python – The if-else Statement……………………………………………………………………… 133
Python – elif Statement…………………………………………………………………………………………………… 135
Nested If Statements……………………………………………………………………………………………………….. 137
- Python – Match-Case Statement……………………………………………………………………. 140
Combined Cases……………………………………………………………………………………………………………… 141
List as the Argument……………………………………………………………………………………………………….. 142
Using “if” in “Case” Clause……………………………………………………………………………………………….. 142
- Python – The for Loop………………………………………………………………………………… 144
Using “for” with a String………………………………………………………………………………………………….. 144
Using “for” with a Tuple…………………………………………………………………………………………………… 145
Using “for” with a List……………………………………………………………………………………………………… 146
Using “for” with a Range Object……………………………………………………………………………………….. 146
Using “for” Loop with Sequence Index……………………………………………………………………………… 149
Using “for” with Dictionaries……………………………………………………………………………………………. 149
- Python – The for-else Loop………………………………………………………………………….. 152
Nested Loops………………………………………………………………………………………………………………….. 153
- Python – The while Loop…………………………………………………………………………….. 155
The Infinite Loop……………………………………………………………………………………………………………… 157
The while-else Loop………………………………………………………………………………………………………… 158
- Python – The break Statement……………………………………………………………………… 160
Loop Control Statements…………………………………………………………………………………………………. 160
The break Statement……………………………………………………………………………………………………….. 160
- Python – The continue Statement………………………………………………………………….. 164
- Python – The pass Statement……………………………………………………………………….. 167
PYTHON FUNCTIONS & MODULES…………………………………………………………………. 168
- Python – Functions……………………………………………………………………………………. 169
Types of Python Functions……………………………………………………………………………………………….. 169
Python – Defining a Function…………………………………………………………………………………………… 169
Calling a Function……………………………………………………………………………………………………………. 170
Pass by Reference vs Value………………………………………………………………………………………………. 171
Function Arguments………………………………………………………………………………………………………… 173
Function with Return Value……………………………………………………………………………………………… 174
Types of Function Arguments…………………………………………………………………………………………… 174
Order of Arguments………………………………………………………………………………………………………… 175
- Python – Default Arguments………………………………………………………………………… 176
- Python – Keyword Arguments………………………………………………………………………. 178
- Python – Keyword-Only Arguments……………………………………………………………….. 180
- Python – Positional Arguments…………………………………………………………………….. 181
- Python – Positional-Only Arguments………………………………………………………………. 183
- Python – Arbitrary Arguments………………………………………………………………………. 185
- Python – Variable Scope……………………………………………………………………………… 188
globals() Function……………………………………………………………………………………………………………. 188
locals() Function……………………………………………………………………………………………………………… 189
Namespace Conflict………………………………………………………………………………………………………… 190
- Python – Function Annotations…………………………………………………………………….. 193
- Python – Modules…………………………………………………………………………………….. 196
Built-in Modules……………………………………………………………………………………………………………… 196
User-Defined Modules…………………………………………………………………………………………………….. 197
Create a Module……………………………………………………………………………………………………………… 197
The import Statement……………………………………………………………………………………………………… 198
The from … import Statement…………………………………………………………………………………………. 199
The from…import * Statement………………………………………………………………………………………… 199
The import … as Statement……………………………………………………………………………………………… 199
Module Attributes…………………………………………………………………………………………………………… 200
The name Attribute………………………………………………………………………………………………….. 201
The reload() Function………………………………………………………………………………………………………. 202
- Python – Built-in Functions………………………………………………………………………….. 204
Built-in Mathematical Functions………………………………………………………………………………………. 206
abs() function………………………………………………………………………………………………………………….. 207
max() function………………………………………………………………………………………………………………… 208
min() function…………………………………………………………………………………………………………………. 209
pow() function………………………………………………………………………………………………………………… 210
round() Function……………………………………………………………………………………………………………… 211
sum() function………………………………………………………………………………………………………………… 212
PYTHON STRINGS………………………………………………………………………………………… 214
- Python – Strings……………………………………………………………………………………….. 215
- Python – Slicing Strings………………………………………………………………………………. 217
- Python – Modify Strings……………………………………………………………………………… 222
- Python – String Concatenation……………………………………………………………………… 225
- Python – String Formatting………………………………………………………………………….. 227
String Formatting Operator……………………………………………………………………………………………… 227
Using the format() Method……………………………………………………………………………………………… 230
Using f-string Formatting…………………………………………………………………………………………………. 232
Using String Template Class……………………………………………………………………………………………… 234
- Python – Escape Characters…………………………………………………………………………. 236
- Python – String Methods…………………………………………………………………………….. 240
Case Conversion Methods……………………………………………………………………………………………….. 240
Alignment Methods………………………………………………………………………………………………………… 246
Find and Replace Methods………………………………………………………………………………………………. 252
Boolean String Methods………………………………………………………………………………………………….. 260
Split and Join Methods…………………………………………………………………………………………………….. 274
Translation Methods……………………………………………………………………………………………………….. 285
- Python – String Exercises…………………………………………………………………………….. 289
PYTHON LISTS…………………………………………………………………………………………….. 291
- Python – Lists………………………………………………………………………………………….. 292
Python – List Operations………………………………………………………………………………………………….. 292
- Python – Access List Items…………………………………………………………………………… 293
- Python – Change List Items………………………………………………………………………….. 295
- Python – Add List Items………………………………………………………………………………. 297
- Python – Remove List Items…………………………………………………………………………. 298
Using the remove() Method…………………………………………………………………………………………….. 298
Using the pop() Method…………………………………………………………………………………………………… 298
Using the “del” Keyword………………………………………………………………………………………………….. 298
- Python – Loop Lists……………………………………………………………………………………. 300
- Python – List Comprehension……………………………………………………………………….. 301
List Comprehension Technique………………………………………………………………………………………… 301
Nested Loops in List Comprehension………………………………………………………………………………… 302
Condition in List Comprehension……………………………………………………………………………………… 302
- Python – Sort Lists…………………………………………………………………………………….. 303
- Python – Copy Lists…………………………………………………………………………………… 305
- Python – Join Lists…………………………………………………………………………………….. 307
- Python – List Methods……………………………………………………………………………….. 309
Python – Clear a List………………………………………………………………………………………………………… 309
Python – Counting the List Items……………………………………………………………………………………… 310
Python – Extending a List…………………………………………………………………………………………………. 311
Python – Finding the Index of a List Item………………………………………………………………………….. 311
Python – Reverse a List……………………………………………………………………………………………………. 312
- Python – List Exercises……………………………………………………………………………….. 313
PYTHON TUPLES………………………………………………………………………………………….. 315
- Python – Tuples……………………………………………………………………………………….. 316
Python – Tuple Operations………………………………………………………………………………………………. 316
- Python – Access Tuple Items………………………………………………………………………… 317
Extracting a Subtuple from a Tuple…………………………………………………………………………………… 317
- Python – Update Tuples……………………………………………………………………………… 319
How to Update a Python Tuple?………………………………………………………………………………………. 319
- Python – Unpack Tuple Items……………………………………………………………………….. 321
Using “*” to Unpack a Tuple…………………………………………………………………………………………….. 321
- Python – Loop Tuples…………………………………………………………………………………. 323
- Python – Join Tuples………………………………………………………………………………….. 324
- Python – Tuple Methods…………………………………………………………………………….. 326
Finding the Index of a Tuple Item……………………………………………………………………………………… 326
Counting Tuple Items………………………………………………………………………………………………………. 326
- Python – Tuple Exercises…………………………………………………………………………….. 328
PYTHON SETS……………………………………………………………………………………………… 330
- Python – Sets………………………………………………………………………………………….. 331
set() Function………………………………………………………………………………………………………………….. 331
- Python – Access Set Items…………………………………………………………………………… 334
- Python – Add Set Items………………………………………………………………………………. 335
add() Method………………………………………………………………………………………………………………….. 335
update() Method…………………………………………………………………………………………………………….. 335
union() Method………………………………………………………………………………………………………………. 336
- Python – Remove Set Items…………………………………………………………………………. 338
remove() Method……………………………………………………………………………………………………………. 338
discard() Method…………………………………………………………………………………………………………….. 338
pop() Method…………………………………………………………………………………………………………………. 339
clear() Method………………………………………………………………………………………………………………… 340
difference_update() Method……………………………………………………………………………………………. 340
difference() Method………………………………………………………………………………………………………… 341
intersection_update() Method…………………………………………………………………………………………. 341
intersection() Method……………………………………………………………………………………………………… 342
symmetric_difference_update() method………………………………………………………………………….. 343
symmetric_difference() Method………………………………………………………………………………………. 343
- Python – Loop Sets……………………………………………………………………………………. 345
- Python – Join Sets…………………………………………………………………………………….. 346
Using the “|” Operator…………………………………………………………………………………………………….. 346
Using the union() Method……………………………………………………………………………………………….. 346
Using the update() Method……………………………………………………………………………………………… 346
Using the unpacking Operator…………………………………………………………………………………………. 347
- Python – Copy Sets……………………………………………………………………………………. 348
- Python – Set Operators………………………………………………………………………………. 349
Union Operator (|)………………………………………………………………………………………………………….. 349
Intersection Operator (&)………………………………………………………………………………………………… 349
Difference Operator (–)…………………………………………………………………………………………………… 350
Symmetric Difference Operator (^)………………………………………………………………………………….. 351
- Python – Set Methods………………………………………………………………………………… 353
- Python – Set Exercises……………………………………………………………………………….. 354
PYTHON DICTIONARIES………………………………………………………………………………… 356
- Python – Dictionaries…………………………………………………………………………………. 357
Python – Dictionary Operators…………………………………………………………………………………………. 358
- Python – Access Dictionary Items………………………………………………………………….. 359
Using the “[ ]” Operator…………………………………………………………………………………………………… 359
Using the get() Method……………………………………………………………………………………………………. 359
- Python – Change Dictionary Items…………………………………………………………………. 361
- Python – Add Dictionary Items……………………………………………………………………… 363
Using the “[ ]” Operator…………………………………………………………………………………………………… 363
Using the update() Method……………………………………………………………………………………………… 364
Using the Unpack Operator……………………………………………………………………………………………… 366
Using the Union Operator (|)…………………………………………………………………………………………… 366
Using “|=” Operator………………………………………………………………………………………………………… 367
- Python – Remove Dictionary Items………………………………………………………………… 368
Using del Keyword…………………………………………………………………………………………………………… 368
Using pop() Method………………………………………………………………………………………………………… 369
Using popitem() Method…………………………………………………………………………………………………. 369
Using clear() Method………………………………………………………………………………………………………. 370
- Python – Dictionary View Objects………………………………………………………………….. 371
items() Method……………………………………………………………………………………………………………….. 371
keys() Method…………………………………………………………………………………………………………………. 372
values() Method……………………………………………………………………………………………………………… 372
- Python – Loop Dictionaries………………………………………………………………………….. 374
- Python – Copy Dictionaries………………………………………………………………………….. 377
- Python – Nested Dictionaries……………………………………………………………………….. 379
- Python – Dictionary Methods……………………………………………………………………….. 381
Dictionary clear() Method……………………………………………………………………………………………….. 381
Dictionary copy() Method………………………………………………………………………………………………… 382
Dictionary fromkeys() Method…………………………………………………………………………………………. 383
Dictionary get() Method………………………………………………………………………………………………….. 384
Dictionary has_key() Method…………………………………………………………………………………………… 384
Dictionary items() Method………………………………………………………………………………………………. 385
Dictionary keys() Method………………………………………………………………………………………………… 386
Dictionary setdefault() Method………………………………………………………………………………………… 386
Dictionary update() Method…………………………………………………………………………………………….. 387
Dictionary values() Method……………………………………………………………………………………………… 388
- Python – Dictionary Exercises……………………………………………………………………….. 389
PYTHON ARRAYS…………………………………………………………………………………………. 391
- Python – Arrays……………………………………………………………………………………….. 392
- Python – Access Array Items………………………………………………………………………… 394
Changing Array Items………………………………………………………………………………………………………. 394
- Python – Add Array Items……………………………………………………………………………. 395
The append() Method……………………………………………………………………………………………………… 395
The insert() Method………………………………………………………………………………………………………… 395
The extend() Method………………………………………………………………………………………………………. 396
- Python – Remove Array Items………………………………………………………………………. 397
array.remove() Method…………………………………………………………………………………………………… 397
array.pop() Method…………………………………………………………………………………………………………. 397
- Python – Loop Arrays…………………………………………………………………………………. 399
- Python – Copy Arrays…………………………………………………………………………………. 401
- Python – Reverse Arrays…………………………………………………………………………….. 403
- Python – Sort Arrays………………………………………………………………………………….. 405
Using a Sorting Algorithm………………………………………………………………………………………………… 405
Using the sort() Method from List…………………………………………………………………………………….. 405
Using the Built-in sorted() Function………………………………………………………………………………….. 406
- Python – Join Arrays………………………………………………………………………………….. 407
- Python – Array Methods…………………………………………………………………………….. 409
array.reverse() Method……………………………………………………………………………………………………. 409
array.count() Method……………………………………………………………………………………………………… 409
array.index() method………………………………………………………………………………………………………. 410
array.fromlist() Method…………………………………………………………………………………………………… 410
array.tofile() Method………………………………………………………………………………………………………. 411
array.fromfile() Method…………………………………………………………………………………………………… 411
- Python – Array Exercises…………………………………………………………………………….. 413
PYTHON FILE HANDLING………………………………………………………………………………. 415
- Python – File Handling……………………………………………………………………………….. 416
The open() Function………………………………………………………………………………………………………… 416
File Opening Modes………………………………………………………………………………………………………… 417
- Python – Write to File………………………………………………………………………………… 419
Writing in Binary Mode……………………………………………………………………………………………………. 419
Appending to a File…………………………………………………………………………………………………………. 420
Using the “w+” Mode………………………………………………………………………………………………………. 420
- Python – Read Files…………………………………………………………………………………… 422
Reading in Binary Mode…………………………………………………………………………………………………… 423
Read Integer Data from File…………………………………………………………………………………………….. 423
Read Float Data from File………………………………………………………………………………………………… 423
Using the “r+” Mode……………………………………………………………………………………………………….. 424
Python – Simultaneous Read/Write…………………………………………………………………………………. 425
The seek() Method………………………………………………………………………………………………………….. 425
- Python – Renaming and Deleting Files…………………………………………………………….. 427
rename() Method……………………………………………………………………………………………………………. 427
remove() Method……………………………………………………………………………………………………………. 427
- Python – Directories………………………………………………………………………………….. 428
The mkdir() Method………………………………………………………………………………………………………… 428
The chdir() Method…………………………………………………………………………………………………………. 428
The getcwd() Method……………………………………………………………………………………………………… 429
The rmdir() Method………………………………………………………………………………………………………… 429
- Python – File Methods……………………………………………………………………………….. 430
The close() Method…………………………………………………………………………………………………………. 431
The flush() Method………………………………………………………………………………………………………….. 431
The fileno() Method………………………………………………………………………………………………………… 432
isatty() Method……………………………………………………………………………………………………………….. 433
The next() function………………………………………………………………………………………………………….. 434
The read() Method………………………………………………………………………………………………………….. 435
The readline() Method…………………………………………………………………………………………………….. 436
The readlines() Method…………………………………………………………………………………………………… 437
The seek() Method………………………………………………………………………………………………………….. 438
The tell() Method……………………………………………………………………………………………………………. 439
The truncate() Method……………………………………………………………………………………………………. 440
The write() Method…………………………………………………………………………………………………………. 441
The writelines() Method………………………………………………………………………………………………….. 443
- Python – OS File/Directory Methods………………………………………………………………. 445
os.closerange() Method…………………………………………………………………………………………………… 446
os.dup() Method……………………………………………………………………………………………………………… 447
os.fdatasync() Method…………………………………………………………………………………………………….. 448
os.fdopen() Method………………………………………………………………………………………………………… 450
os.fsync() Method…………………………………………………………………………………………………………… 451
os.ftruncate() Method…………………………………………………………………………………………………….. 452
os.lseek() Method……………………………………………………………………………………………………………. 454
os.open() Method……………………………………………………………………………………………………………. 455
os.read() Method……………………………………………………………………………………………………………. 456
os.write() Method…………………………………………………………………………………………………………… 457
PYTHON OBJECT ORIENTED PROGRAMMING………………………………………………….. 459
- Python – OOP Concepts……………………………………………………………………………… 460
Procedural Oriented Approach………………………………………………………………………………………… 460
Python – OOP Concepts…………………………………………………………………………………………………… 460
- Python – Object and Classes………………………………………………………………………… 464
- Python – Class Attributes……………………………………………………………………………. 466
Class Variables………………………………………………………………………………………………………………… 467
- Python – Class Methods……………………………………………………………………………… 468
- Python – Static Methods…………………………………………………………………………….. 470
- Python – Constructors………………………………………………………………………………… 471
Python – Instance Methods……………………………………………………………………………………………… 473
- Python – Access Modifiers…………………………………………………………………………… 475
Name Mangling………………………………………………………………………………………………………………. 477
Python – Property Object………………………………………………………………………………………………… 477
Getters and Setter Methods…………………………………………………………………………………………….. 477
- Python – Inheritance…………………………………………………………………………………. 480
Python – Multiple Inheritance………………………………………………………………………………………….. 481
Method Resolution Order (MRO)……………………………………………………………………………………… 483
- Python – Polymorphism……………………………………………………………………………… 484
- Python – Method Overriding………………………………………………………………………… 486
Base Overridable Methods………………………………………………………………………………………………. 488
- Python – Method Overloading……………………………………………………………………… 489
- Python – Dynamic Binding…………………………………………………………………………… 492
Duck Typing…………………………………………………………………………………………………………………….. 493
- Python – Dynamic Typing……………………………………………………………………………. 495
- Python – Abstraction…………………………………………………………………………………. 497
- Python – Encapsulation………………………………………………………………………………. 499
- Python – Interfaces…………………………………………………………………………………… 502
- Python – Packages…………………………………………………………………………………….. 504
Define Package List………………………………………………………………………………………………………….. 505
Package Installation………………………………………………………………………………………………………… 506
- Python – Inner Classes……………………………………………………………………………….. 508
- Python – Anonymous Class and Objects…………………………………………………………… 510
- Python – Singleton Class……………………………………………………………………………… 512
- Python – Wrapper Classes…………………………………………………………………………… 513
- Python – Enums……………………………………………………………………………………….. 514
- Python – Reflection…………………………………………………………………………………… 517
The type() Function…………………………………………………………………………………………………………. 517
The isinstance() Function…………………………………………………………………………………………………. 518
The issubclass() Function…………………………………………………………………………………………………. 519
The callable() Function…………………………………………………………………………………………………….. 519
The getattr() Function……………………………………………………………………………………………………… 520
The setattr() Function……………………………………………………………………………………………………… 521
The hasattr() Function……………………………………………………………………………………………………… 521
The dir() Function……………………………………………………………………………………………………………. 522
PYTHON ERRORS AND EXCEPTIONS……………………………………………………………….. 524
- Python – Syntax Errors……………………………………………………………………………….. 525
What is Exception?………………………………………………………………………………………………………….. 526
- Python – Exceptions Handling………………………………………………………………………. 528
- Python – The try-except Block………………………………………………………………………. 530
- Python – The try-finally Block……………………………………………………………………….. 531
Exception with Arguments………………………………………………………………………………………………. 532
- Python – Raising Exceptions…………………………………………………………………………. 533
- Python – Exception Chaining………………………………………………………………………… 535
- Python – Nested try Block…………………………………………………………………………… 538
- Python – User-Defined Exceptions…………………………………………………………………. 541
- Python – Logging………………………………………………………………………………………. 542
Logging Levels…………………………………………………………………………………………………………………. 542
Logging Configuration……………………………………………………………………………………………………… 543
Variable Data in Logging Message……………………………………………………………………………………. 544
- Python – Assertions…………………………………………………………………………………… 545
The assert Statement………………………………………………………………………………………………………. 545
- Python – Built-in Exceptions………………………………………………………………………… 547
PYTHON MULTITHREADING………………………………………………………………………….. 552
- Python – Multithreading…………………………………………………………………………….. 553
- Python – Thread Life cycle…………………………………………………………………………… 554
Python – The _thread Module…………………………………………………………………………………………. 554
Python – The threading Module………………………………………………………………………………………. 554
- Python – Creating a Thread………………………………………………………………………….. 556
- Python – Starting a Thread………………………………………………………………………….. 558
- Python – Joining the Threads……………………………………………………………………….. 559
- Python – Naming the Threads………………………………………………………………………. 560
The daemon Property……………………………………………………………………………………………………… 560
- Python – Thread Scheduling…………………………………………………………………………. 562
- Python – Thread Pools……………………………………………………………………………….. 565
What is a Thread Pool?……………………………………………………………………………………………………. 565
The Future Class……………………………………………………………………………………………………………… 565
The ThreadPoolExecutor Class…………………………………………………………………………………………. 566
- Python – Main Thread………………………………………………………………………………… 568
- Python – Thread Priority…………………………………………………………………………….. 570
- Python – Daemon Threads…………………………………………………………………………… 573
- Python – Synchronizing Threads……………………………………………………………………. 576
PYTHON SYNCHRONIZATION………………………………………………………………………… 578
- Python – Inter-Thread Communication……………………………………………………………. 579
The Event Object…………………………………………………………………………………………………………….. 579
The Condition Object………………………………………………………………………………………………………. 581
- Python – Thread Deadlock…………………………………………………………………………… 584
The Lock Object………………………………………………………………………………………………………………. 584
The Semaphore Object……………………………………………………………………………………………………. 586
- Python – Interrupting a Thread……………………………………………………………………… 589
PYTHON NETWORKING………………………………………………………………………………… 591
- Python – Network Programming……………………………………………………………………. 592
- Python – Socket Programming……………………………………………………………………… 594
What are Sockets?…………………………………………………………………………………………………………… 594
Python – The socket Module……………………………………………………………………………………………. 595
Server Socket Methods……………………………………………………………………………………………………. 595
Client Socket Methods…………………………………………………………………………………………………….. 596
Python – Socket Server……………………………………………………………………………………………………. 596
Python – Socket Client…………………………………………………………………………………………………….. 597
Python – File Transfer with Socket Module………………………………………………………………………. 599
Python – The socketserver Module………………………………………………………………………………….. 601
- Python – URL Processing…………………………………………………………………………….. 604
The urllib.parse Module…………………………………………………………………………………………………… 604
The urllib.request Module……………………………………………………………………………………………….. 606
The Request Object…………………………………………………………………………………………………………. 607
The urllib.error Module…………………………………………………………………………………………………… 609
- Python – Generics…………………………………………………………………………………….. 610
PYTHON MISCELLANEOUS……………………………………………………………………………. 612
- Python – Date and Time……………………………………………………………………………… 613
What are Tick Intervals?………………………………………………………………………………………………….. 613
What is TimeTuple?…………………………………………………………………………………………………………. 613
Getting the Current Time…………………………………………………………………………………………………. 615
Getting the Formatted Time…………………………………………………………………………………………….. 615
Getting the Calendar for a Month…………………………………………………………………………………….. 616
The time Module…………………………………………………………………………………………………………….. 616
Time altzone() Method……………………………………………………………………………………………………. 618
Time asctime() Method……………………………………………………………………………………………………. 618
Time perf_counter() Method…………………………………………………………………………………………… 619
Time ctime() Method………………………………………………………………………………………………………. 620
Time gmtime() Method……………………………………………………………………………………………………. 621
Time localtime() Method…………………………………………………………………………………………………. 621
Time mktime() Method……………………………………………………………………………………………………. 622
Time sleep() Method……………………………………………………………………………………………………….. 623
Time strftime() Method…………………………………………………………………………………………………… 624
Time strptime() Method………………………………………………………………………………………………….. 625
Time time() Method………………………………………………………………………………………………………… 627
Time tzset() Method………………………………………………………………………………………………………… 628
The calendar Module………………………………………………………………………………………………………. 630
datetime module…………………………………………………………………………………………………………….. 631
date……………………………………………………………………………………………………………………………….. 632
time……………………………………………………………………………………………………………………………….. 636
datetime…………………………………………………………………………………………………………………………. 639
timedelta………………………………………………………………………………………………………………………… 643
- Python – Maths……………………………………………………………………………………….. 647
Functions in Math Module………………………………………………………………………………………………. 647
Theoretic and Representation Functions………………………………………………………………………….. 650
Power and Logarithmic Functions…………………………………………………………………………………….. 676
Trigonometric Functions………………………………………………………………………………………………….. 685
Angular Conversion Functions………………………………………………………………………………………….. 695
Hyperbolic Functions………………………………………………………………………………………………………. 698
Special Functions…………………………………………………………………………………………………………….. 705
Mathematical Constants………………………………………………………………………………………………….. 709
- Python – Iterators…………………………………………………………………………………….. 713
Asynchronous Iterator…………………………………………………………………………………………………….. 716
- Python – Generators………………………………………………………………………………….. 718
Asynchronous Generator…………………………………………………………………………………………………. 720
- Python – Closures…………………………………………………………………………………….. 723
Nested Functions…………………………………………………………………………………………………………….. 723
What is a Closure?…………………………………………………………………………………………………………… 724
nonlocal Keyword……………………………………………………………………………………………………………. 724
- Python – Decorators………………………………………………………………………………….. 726
@classmethod Decorator………………………………………………………………………………………………… 728
@staticmethod Decorator……………………………………………………………………………………………….. 729
@property Decorator……………………………………………………………………………………………………… 730
- Python – Recursion……………………………………………………………………………………. 732
- Python – Regular Expressions……………………………………………………………………….. 735
Raw Strings…………………………………………………………………………………………………………………….. 735
Metacharacters………………………………………………………………………………………………………………. 736
re.match() Function…………………………………………………………………………………………………………. 737
re.search() Function………………………………………………………………………………………………………… 738
Matching Vs Searching…………………………………………………………………………………………………….. 739
re.findall() Function…………………………………………………………………………………………………………. 740
re.sub() Function…………………………………………………………………………………………………………….. 741
re.compile() Function………………………………………………………………………………………………………. 742
re.finditer() Function……………………………………………………………………………………………………….. 743
Use Cases of Python Regex………………………………………………………………………………………………. 744
- Python – PIP……………………………………………………………………………………………. 745
Install a Package……………………………………………………………………………………………………………… 745
Use requirements.txt………………………………………………………………………………………………………. 747
pip uninstall……………………………………………………………………………………………………………………. 747
pip list…………………………………………………………………………………………………………………………….. 748
pip show…………………………………………………………………………………………………………………………. 749
pip freeze……………………………………………………………………………………………………………………….. 749
pip download………………………………………………………………………………………………………………….. 749
pip search……………………………………………………………………………………………………………………….. 750
pip config………………………………………………………………………………………………………………………… 750
- Python – Database Access…………………………………………………………………………… 751
The sqlite3 Module…………………………………………………………………………………………………………. 752
The Connection Object……………………………………………………………………………………………………. 752
The Cursor Object……………………………………………………………………………………………………………. 753
Creating a Database Table……………………………………………………………………………………………….. 753
INSERT Operation……………………………………………………………………………………………………………. 754
READ Operation………………………………………………………………………………………………………………. 755
Update Operation…………………………………………………………………………………………………………… 756
DELETE Operation…………………………………………………………………………………………………………… 757
Performing Transactions………………………………………………………………………………………………….. 757
COMMIT Operation…………………………………………………………………………………………………………. 758
ROLLBACK Operation………………………………………………………………………………………………………. 758
The PyMySQL Module……………………………………………………………………………………………………… 759
Handling Errors……………………………………………………………………………………………………………….. 760
- Python – Weak References………………………………………………………………………….. 761
The callback Function………………………………………………………………………………………………………. 762
Finalizing Objects…………………………………………………………………………………………………………….. 763
WeakKeyDictionary…………………………………………………………………………………………………………. 764
WeakValueDictionary……………………………………………………………………………………………………… 765
- Python – Serialization………………………………………………………………………………… 768
Pickle Protocols………………………………………………………………………………………………………………. 768
dump() and load()……………………………………………………………………………………………………………. 768
dumps() and loads()………………………………………………………………………………………………………… 769
Pickler Class……………………………………………………………………………………………………………………. 770
Unpickler Class………………………………………………………………………………………………………………… 770
- Python – Templating………………………………………………………………………………….. 771
substitute()……………………………………………………………………………………………………………………… 771
safe_substitute()…………………………………………………………………………………………………………….. 772
is_valid()…………………………………………………………………………………………………………………………. 773
get_identifiers()………………………………………………………………………………………………………………. 773
- Python – Output Formatting………………………………………………………………………… 775
String Formatting Operator……………………………………………………………………………………………… 775
The format() Method………………………………………………………………………………………………………. 775
f-strings………………………………………………………………………………………………………………………….. 776
Template Strings……………………………………………………………………………………………………………… 776
The textwrap Module……………………………………………………………………………………………………… 777
The shorten() Function…………………………………………………………………………………………………….. 779
The pprint Module………………………………………………………………………………………………………….. 780
- Python – Performance Measurement……………………………………………………………… 784
- Python – Data Compression…………………………………………………………………………. 787
The zlib Module………………………………………………………………………………………………………………. 787
The gzip Module……………………………………………………………………………………………………………… 788
The bz2 Module………………………………………………………………………………………………………………. 790
The lzma Module…………………………………………………………………………………………………………….. 791
The zipfile Module…………………………………………………………………………………………………………… 793
The tarfile Module…………………………………………………………………………………………………………… 796
Command Line Interface………………………………………………………………………………………………….. 798
- Python – CGI Programming………………………………………………………………………….. 799
What is CGI?……………………………………………………………………………………………………………………. 799
Web Browsing…………………………………………………………………………………………………………………. 799
CGI Architecture Diagram………………………………………………………………………………………………… 800
Web Server Support and Configuration……………………………………………………………………………. 800
First CGI Program……………………………………………………………………………………………………………. 801
HTTP Header…………………………………………………………………………………………………………………… 802
CGI Environment Variables………………………………………………………………………………………………. 802
GET and POST Methods…………………………………………………………………………………………………… 804
Passing Information using GET method…………………………………………………………………………….. 804
Simple URL Example – Get Method………………………………………………………………………………….. 804
Simple FORM Example – GET Method………………………………………………………………………………. 805
Passing Radio Button Data to CGI Program……………………………………………………………………….. 808
Passing Text Area Data to CGI Program…………………………………………………………………………….. 809
Passing Drop Down Box Data to CGI Program…………………………………………………………………… 810
Using Cookies in CGI………………………………………………………………………………………………………… 811
How It Works?………………………………………………………………………………………………………………… 811
Setting up Cookies…………………………………………………………………………………………………………… 811
Retrieving Cookies…………………………………………………………………………………………………………… 812
File Upload Example………………………………………………………………………………………………………… 813
How To Raise a “File Download” Dialog Box ?…………………………………………………………………… 814
- Python – XML Processing…………………………………………………………………………….. 815
What is XML?………………………………………………………………………………………………………………….. 815
Parsing XML with SAX APIs………………………………………………………………………………………………. 817
Parsing XML with DOM APIs…………………………………………………………………………………………….. 820
ElementTree XML API……………………………………………………………………………………………………… 822
Create an XML File…………………………………………………………………………………………………………… 822
Parse an XML File……………………………………………………………………………………………………………. 824
Modify an XML file………………………………………………………………………………………………………….. 825
- Python – GUI Programming…………………………………………………………………………. 826
Tkinter Programming………………………………………………………………………………………………………. 826
Tkinter Widgets………………………………………………………………………………………………………………. 828
Tkinter Button…………………………………………………………………………………………………………………. 829
Tkinter Canvas………………………………………………………………………………………………………………… 832
Tkinter Checkbutton……………………………………………………………………………………………………….. 835
Tkinter Entry…………………………………………………………………………………………………………………… 838
Tkinter Frame…………………………………………………………………………………………………………………. 842
Tkinter Label…………………………………………………………………………………………………………………… 844
Tkinter Listbox………………………………………………………………………………………………………………… 847
Tkinter Menubutton………………………………………………………………………………………………………… 851
Tkinter Menu………………………………………………………………………………………………………………….. 854
Tkinter Message……………………………………………………………………………………………………………… 858
Tkinter Radiobutton………………………………………………………………………………………………………… 860
Tkinter Scale…………………………………………………………………………………………………………………… 864
Tkinter Scrollbar……………………………………………………………………………………………………………… 868
Tkinter Text…………………………………………………………………………………………………………………….. 871
Tkinter Toplevel………………………………………………………………………………………………………………. 876
Tkinter Spinbox……………………………………………………………………………………………………………….. 879
Tkinter PanedWindow…………………………………………………………………………………………………….. 882
Tkinter LabelFrame…………………………………………………………………………………………………………. 885
Tkinter tkMessageBox……………………………………………………………………………………………………… 887
Standard Attributes…………………………………………………………………………………………………………. 888
Tkinter Dimensions…………………………………………………………………………………………………………. 888
Tkinter Colors…………………………………………………………………………………………………………………. 889
Tkinter Fonts…………………………………………………………………………………………………………………… 890
Tkinter Anchors………………………………………………………………………………………………………………. 891
Tkinter Relief styles…………………………………………………………………………………………………………. 892
Tkinter Bitmaps………………………………………………………………………………………………………………. 893
Tkinter Cursors……………………………………………………………………………………………………………….. 894
Geometry Management………………………………………………………………………………………………….. 895
Tkinter pack() Method…………………………………………………………………………………………………….. 896
Tkinter grid() Method………………………………………………………………………………………………………. 897
Tkinter place() Method……………………………………………………………………………………………………. 898
SimpleDialog…………………………………………………………………………………………………………………… 900
The FileDialog Module…………………………………………………………………………………………………….. 902
ttk module………………………………………………………………………………………………………………………. 904
Combobox Widget…………………………………………………………………………………………………………… 905
Progressbar…………………………………………………………………………………………………………………….. 906
Notebook……………………………………………………………………………………………………………………….. 908
Treeview…………………………………………………………………………………………………………………………. 909
Sizegrip…………………………………………………………………………………………………………………………… 911
Separator……………………………………………………………………………………………………………………….. 912
- Python – Command-Line Arguments………………………………………………………………. 914
The getopt Module………………………………………………………………………………………………………….. 916
The argparse Module………………………………………………………………………………………………………. 918
- Python – Docstrings…………………………………………………………………………………… 922
- Python – JSON…………………………………………………………………………………………. 926
JSONEncoder Class………………………………………………………………………………………………………….. 927
JSONDEcoder class………………………………………………………………………………………………………….. 929
JSON with Files/Streams………………………………………………………………………………………………….. 929
- Python – Sending Emails…………………………………………………………………………….. 931
xxvii
smptlib.SMTP() Function…………………………………………………………………………………………………. 931
The smtpd Module………………………………………………………………………………………………………….. 932
Using gmail SMTP……………………………………………………………………………………………………………. 934
- Python – Further Extensions………………………………………………………………………… 936
Pre-Requisites for Writing Extensions………………………………………………………………………………. 936
First look at a Python Extension……………………………………………………………………………………….. 936
The Header File Python.h…………………………………………………………………………………………………. 936
The C Functions………………………………………………………………………………………………………………. 936
The Method Mapping Table…………………………………………………………………………………………….. 937
The Initialization Function……………………………………………………………………………………………….. 938
Building and Installing Extensions…………………………………………………………………………………….. 940
Importing Extensions………………………………………………………………………………………………………. 940
Passing Function Parameters…………………………………………………………………………………………… 941
The PyArg_ParseTuple Function……………………………………………………………………………………….. 942
Returning Values…………………………………………………………………………………………………………….. 943
The Py_BuildValue Function…………………………………………………………………………………………….. 944
- Python – Tools / Utilities…………………………………………………………………………….. 946
The dis Module……………………………………………………………………………………………………………….. 946
The pdb Module……………………………………………………………………………………………………………… 947
The profile Module………………………………………………………………………………………………………….. 948
The tabnanny Module……………………………………………………………………………………………………… 949
- Python – GUIs………………………………………………………………………………………….. 950
IDLE………………………………………………………………………………………………………………………………… 950
Jupyter Notebook……………………………………………………………………………………………………………. 951
VS Code………………………………………………………………………………………………………………………….. 953
PyCharm…………………………………………………………………………………………………………………………. 955