Python Training Course Table of contents

Table of Contents:

PYTHON BASICS………………………………………………………………………………………………. 1

  1. Python – Overview……………………………………………………………………………………….. 2
  2. Python – History………………………………………………………………………………………….. 4
  3. Python – Features………………………………………………………………………………………… 6
  4. Python vs C++…………………………………………………………………………………………….. 9
  5. Python – Hello World………………………………………………………………………………….. 12
  6. Python – Application Areas……………………………………………………………………………. 15
  7. Python – Interpreter……………………………………………………………………………………. 18
  8. Python – Environment Setup…………………………………………………………………………. 23

Install Python on MacOS……………………………………………………………………………………………………. 26

Install Python from Source Code………………………………………………………………………………………… 27

Python Command Line Options………………………………………………………………………………………….. 30

Python Environment Variables…………………………………………………………………………………………… 31

  1. Python – Virtual Environment………………………………………………………………………… 34
  2. Python – Basic Syntax………………………………………………………………………………….. 37

Python Keywords………………………………………………………………………………………………………………. 37

Python Identifiers……………………………………………………………………………………………………………… 39

Python Indents………………………………………………………………………………………………………………….. 40

Python Statements……………………………………………………………………………………………………………. 41

  1. Python – Variables……………………………………………………………………………………… 43

Naming Convention…………………………………………………………………………………………………………… 44

Assignment Statement………………………………………………………………………………………………………. 45

Multiple Assignments………………………………………………………………………………………………………… 46

  1. 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

  1. Python – Type Casting…………………………………………………………………………………. 56

Implicit Casting in Python………………………………………………………………………………………………….. 56

int() Function…………………………………………………………………………………………………………………….. 57

float() Function…………………………………………………………………………………………………………………. 59

str() Function…………………………………………………………………………………………………………………….. 60

Conversion of Sequence types…………………………………………………………………………………………… 62

  1. Python – Unicode System……………………………………………………………………………… 64
  2. Python – Literals………………………………………………………………………………………… 66

Integer Literal……………………………………………………………………………………………………………………. 66

Float Literal………………………………………………………………………………………………………………………. 67

Complex Literal…………………………………………………………………………………………………………………. 67

String Literal……………………………………………………………………………………………………………………… 68

List Literal…………………………………………………………………………………………………………………………. 69

Tuple Literal……………………………………………………………………………………………………………………… 69

Dictionary Literal………………………………………………………………………………………………………………. 70

  1. Python – Operators…………………………………………………………………………………….. 71

Types of Operators……………………………………………………………………………………………………………. 71

  1. 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

  1. Python – Assignment Operators……………………………………………………………………… 83
  2. 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

  1. Python – Comparison Operators……………………………………………………………………… 90
  2. Python – Logical Operators……………………………………………………………………………. 96
  3. 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

  1. Python – Membership Operators…………………………………………………………………… 105
  2. Python – Identity Operators…………………………………………………………………………. 108
  3. Python – Comments………………………………………………………………………………….. 110
  4. Python – User Input…………………………………………………………………………………… 111

input() Function………………………………………………………………………………………………………………. 111

Numeric Input…………………………………………………………………………………………………………………. 113

  1. Python – Numbers……………………………………………………………………………………. 117

Python – Integers……………………………………………………………………………………………………………. 117

Python – Floating Point Numbers…………………………………………………………………………………….. 120

Python – Complex Numbers…………………………………………………………………………………………….. 123

  1. Python – Booleans……………………………………………………………………………………. 126

PYTHON CONTROL STATEMENTS…………………………………………………………………… 127

  1. Python – Control Flow………………………………………………………………………………… 128
  2. Python – Decision Making…………………………………………………………………………… 130

Python – The if Statement……………………………………………………………………………………………….. 130

  1. Python – The if-else Statement……………………………………………………………………… 133

Python – elif Statement…………………………………………………………………………………………………… 135

Nested If Statements……………………………………………………………………………………………………….. 137

  1. Python – Match-Case Statement……………………………………………………………………. 140

Combined Cases……………………………………………………………………………………………………………… 141

List as the Argument……………………………………………………………………………………………………….. 142

Using “if” in “Case” Clause……………………………………………………………………………………………….. 142

  1. 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

  1. Python – The for-else Loop………………………………………………………………………….. 152

Nested Loops………………………………………………………………………………………………………………….. 153

  1. Python – The while Loop…………………………………………………………………………….. 155

The Infinite Loop……………………………………………………………………………………………………………… 157

The while-else Loop………………………………………………………………………………………………………… 158

  1. Python – The break Statement……………………………………………………………………… 160

Loop Control Statements…………………………………………………………………………………………………. 160

The break Statement……………………………………………………………………………………………………….. 160

  1. Python – The continue Statement………………………………………………………………….. 164
  2. Python – The pass Statement……………………………………………………………………….. 167

PYTHON FUNCTIONS & MODULES…………………………………………………………………. 168

  1. 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

  1. Python – Default Arguments………………………………………………………………………… 176
  2. Python – Keyword Arguments………………………………………………………………………. 178
  3. Python – Keyword-Only Arguments……………………………………………………………….. 180
  4. Python – Positional Arguments…………………………………………………………………….. 181
  5. Python – Positional-Only Arguments………………………………………………………………. 183
  6. Python – Arbitrary Arguments………………………………………………………………………. 185
  7. Python – Variable Scope……………………………………………………………………………… 188

globals() Function……………………………………………………………………………………………………………. 188

locals() Function……………………………………………………………………………………………………………… 189

Namespace Conflict………………………………………………………………………………………………………… 190

  1. Python – Function Annotations…………………………………………………………………….. 193
  2. 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

  1. 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

  1. Python – Strings……………………………………………………………………………………….. 215
  2. Python – Slicing Strings………………………………………………………………………………. 217
  3. Python – Modify Strings……………………………………………………………………………… 222
  4. Python – String Concatenation……………………………………………………………………… 225
  5. Python – String Formatting………………………………………………………………………….. 227

String Formatting Operator……………………………………………………………………………………………… 227

Using the format() Method……………………………………………………………………………………………… 230

Using f-string Formatting…………………………………………………………………………………………………. 232

Using String Template Class……………………………………………………………………………………………… 234

  1. Python – Escape Characters…………………………………………………………………………. 236
  2. 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

  1. Python – String Exercises…………………………………………………………………………….. 289

PYTHON LISTS…………………………………………………………………………………………….. 291

  1. Python – Lists………………………………………………………………………………………….. 292

Python – List Operations………………………………………………………………………………………………….. 292

  1. Python – Access List Items…………………………………………………………………………… 293
  2. Python – Change List Items………………………………………………………………………….. 295
  3. Python – Add List Items………………………………………………………………………………. 297
  4. Python – Remove List Items…………………………………………………………………………. 298

Using the remove() Method…………………………………………………………………………………………….. 298

Using the pop() Method…………………………………………………………………………………………………… 298

Using the “del” Keyword………………………………………………………………………………………………….. 298

  1. Python – Loop Lists……………………………………………………………………………………. 300
  2. Python – List Comprehension……………………………………………………………………….. 301

List Comprehension Technique………………………………………………………………………………………… 301

Nested Loops in List Comprehension………………………………………………………………………………… 302

Condition in List Comprehension……………………………………………………………………………………… 302

  1. Python – Sort Lists…………………………………………………………………………………….. 303
  2. Python – Copy Lists…………………………………………………………………………………… 305
  3. Python – Join Lists…………………………………………………………………………………….. 307
  4. 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

  1. Python – List Exercises……………………………………………………………………………….. 313

PYTHON TUPLES………………………………………………………………………………………….. 315

  1. Python – Tuples……………………………………………………………………………………….. 316

Python – Tuple Operations………………………………………………………………………………………………. 316

  1. Python – Access Tuple Items………………………………………………………………………… 317

Extracting a Subtuple from a Tuple…………………………………………………………………………………… 317

  1. Python – Update Tuples……………………………………………………………………………… 319

How to Update a Python Tuple?………………………………………………………………………………………. 319

  1. Python – Unpack Tuple Items……………………………………………………………………….. 321

Using “*” to Unpack a Tuple…………………………………………………………………………………………….. 321

  1. Python – Loop Tuples…………………………………………………………………………………. 323
  2. Python – Join Tuples………………………………………………………………………………….. 324
  3. Python – Tuple Methods…………………………………………………………………………….. 326

Finding the Index of a Tuple Item……………………………………………………………………………………… 326

Counting Tuple Items………………………………………………………………………………………………………. 326

  1. Python – Tuple Exercises…………………………………………………………………………….. 328

PYTHON SETS……………………………………………………………………………………………… 330

  1. Python – Sets………………………………………………………………………………………….. 331

set() Function………………………………………………………………………………………………………………….. 331

  1. Python – Access Set Items…………………………………………………………………………… 334
  2. Python – Add Set Items………………………………………………………………………………. 335

add() Method………………………………………………………………………………………………………………….. 335

update() Method…………………………………………………………………………………………………………….. 335

union() Method………………………………………………………………………………………………………………. 336

  1. 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

  1. Python – Loop Sets……………………………………………………………………………………. 345
  2. Python – Join Sets…………………………………………………………………………………….. 346

Using the “|” Operator…………………………………………………………………………………………………….. 346

Using the union() Method……………………………………………………………………………………………….. 346

Using the update() Method……………………………………………………………………………………………… 346

Using the unpacking Operator…………………………………………………………………………………………. 347

  1. Python – Copy Sets……………………………………………………………………………………. 348
  2. Python – Set Operators………………………………………………………………………………. 349

Union Operator (|)………………………………………………………………………………………………………….. 349

Intersection Operator (&)………………………………………………………………………………………………… 349

Difference Operator (–)…………………………………………………………………………………………………… 350

Symmetric Difference Operator (^)………………………………………………………………………………….. 351

  1. Python – Set Methods………………………………………………………………………………… 353
  2. Python – Set Exercises……………………………………………………………………………….. 354

PYTHON DICTIONARIES………………………………………………………………………………… 356

  1. Python – Dictionaries…………………………………………………………………………………. 357

Python – Dictionary Operators…………………………………………………………………………………………. 358

  1. Python – Access Dictionary Items………………………………………………………………….. 359

Using the “[ ]” Operator…………………………………………………………………………………………………… 359

Using the get() Method……………………………………………………………………………………………………. 359

  1. Python – Change Dictionary Items…………………………………………………………………. 361
  2. 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

  1. Python – Remove Dictionary Items………………………………………………………………… 368

Using del Keyword…………………………………………………………………………………………………………… 368

Using pop() Method………………………………………………………………………………………………………… 369

Using popitem() Method…………………………………………………………………………………………………. 369

Using clear() Method………………………………………………………………………………………………………. 370

  1. Python – Dictionary View Objects………………………………………………………………….. 371

items() Method……………………………………………………………………………………………………………….. 371

keys() Method…………………………………………………………………………………………………………………. 372

values() Method……………………………………………………………………………………………………………… 372

  1. Python – Loop Dictionaries………………………………………………………………………….. 374
  2. Python – Copy Dictionaries………………………………………………………………………….. 377
  3. Python – Nested Dictionaries……………………………………………………………………….. 379
  4. 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

  1. Python – Dictionary Exercises……………………………………………………………………….. 389

PYTHON ARRAYS…………………………………………………………………………………………. 391

  1. Python – Arrays……………………………………………………………………………………….. 392
  2. Python – Access Array Items………………………………………………………………………… 394

Changing Array Items………………………………………………………………………………………………………. 394

  1. Python – Add Array Items……………………………………………………………………………. 395

The append() Method……………………………………………………………………………………………………… 395

The insert() Method………………………………………………………………………………………………………… 395

The extend() Method………………………………………………………………………………………………………. 396

  1. Python – Remove Array Items………………………………………………………………………. 397

array.remove() Method…………………………………………………………………………………………………… 397

array.pop() Method…………………………………………………………………………………………………………. 397

  1. Python – Loop Arrays…………………………………………………………………………………. 399
  2. Python – Copy Arrays…………………………………………………………………………………. 401
  3. Python – Reverse Arrays…………………………………………………………………………….. 403
  4. Python – Sort Arrays………………………………………………………………………………….. 405

Using a Sorting Algorithm………………………………………………………………………………………………… 405

Using the sort() Method from List…………………………………………………………………………………….. 405

Using the Built-in sorted() Function………………………………………………………………………………….. 406

  1. Python – Join Arrays………………………………………………………………………………….. 407
  2. 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

  1. Python – Array Exercises…………………………………………………………………………….. 413

PYTHON FILE HANDLING………………………………………………………………………………. 415

  1. Python – File Handling……………………………………………………………………………….. 416

The open() Function………………………………………………………………………………………………………… 416

File Opening Modes………………………………………………………………………………………………………… 417

  1. Python – Write to File………………………………………………………………………………… 419

Writing in Binary Mode……………………………………………………………………………………………………. 419

Appending to a File…………………………………………………………………………………………………………. 420

Using the “w+” Mode………………………………………………………………………………………………………. 420

  1. 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

  1. Python – Renaming and Deleting Files…………………………………………………………….. 427

rename() Method……………………………………………………………………………………………………………. 427

remove() Method……………………………………………………………………………………………………………. 427

  1. Python – Directories………………………………………………………………………………….. 428

The mkdir() Method………………………………………………………………………………………………………… 428

The chdir() Method…………………………………………………………………………………………………………. 428

The getcwd() Method……………………………………………………………………………………………………… 429

The rmdir() Method………………………………………………………………………………………………………… 429

  1. 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

  1. 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

  1. Python – OOP Concepts……………………………………………………………………………… 460

Procedural Oriented Approach………………………………………………………………………………………… 460

Python – OOP Concepts…………………………………………………………………………………………………… 460

  1. Python – Object and Classes………………………………………………………………………… 464
  2. Python – Class Attributes……………………………………………………………………………. 466

Class Variables………………………………………………………………………………………………………………… 467

  1. Python – Class Methods……………………………………………………………………………… 468
  2. Python – Static Methods…………………………………………………………………………….. 470
  3. Python – Constructors………………………………………………………………………………… 471

Python – Instance Methods……………………………………………………………………………………………… 473

  1. Python – Access Modifiers…………………………………………………………………………… 475

Name Mangling………………………………………………………………………………………………………………. 477

Python – Property Object………………………………………………………………………………………………… 477

Getters and Setter Methods…………………………………………………………………………………………….. 477

  1. Python – Inheritance…………………………………………………………………………………. 480

Python – Multiple Inheritance………………………………………………………………………………………….. 481

Method Resolution Order (MRO)……………………………………………………………………………………… 483

  1. Python – Polymorphism……………………………………………………………………………… 484
  2. Python – Method Overriding………………………………………………………………………… 486

Base Overridable Methods………………………………………………………………………………………………. 488

  1. Python – Method Overloading……………………………………………………………………… 489
  2. Python – Dynamic Binding…………………………………………………………………………… 492

Duck Typing…………………………………………………………………………………………………………………….. 493

  1. Python – Dynamic Typing……………………………………………………………………………. 495
  2. Python – Abstraction…………………………………………………………………………………. 497
  3. Python – Encapsulation………………………………………………………………………………. 499
  4. Python – Interfaces…………………………………………………………………………………… 502
  5. Python – Packages…………………………………………………………………………………….. 504

Define Package List………………………………………………………………………………………………………….. 505

Package Installation………………………………………………………………………………………………………… 506

  1. Python – Inner Classes……………………………………………………………………………….. 508
  2. Python – Anonymous Class and Objects…………………………………………………………… 510
  3. Python – Singleton Class……………………………………………………………………………… 512
  4. Python – Wrapper Classes…………………………………………………………………………… 513
  5. Python – Enums……………………………………………………………………………………….. 514
  6. 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

  1. Python – Syntax Errors……………………………………………………………………………….. 525

What is Exception?………………………………………………………………………………………………………….. 526

  1. Python – Exceptions Handling………………………………………………………………………. 528
  2. Python – The try-except Block………………………………………………………………………. 530
  3. Python – The try-finally Block……………………………………………………………………….. 531

Exception with Arguments………………………………………………………………………………………………. 532

  1. Python – Raising Exceptions…………………………………………………………………………. 533
  2. Python – Exception Chaining………………………………………………………………………… 535
  3. Python – Nested try Block…………………………………………………………………………… 538
  4. Python – User-Defined Exceptions…………………………………………………………………. 541
  5. Python – Logging………………………………………………………………………………………. 542

Logging Levels…………………………………………………………………………………………………………………. 542

Logging Configuration……………………………………………………………………………………………………… 543

Variable Data in Logging Message……………………………………………………………………………………. 544

  1. Python – Assertions…………………………………………………………………………………… 545

The assert Statement………………………………………………………………………………………………………. 545

  1. Python – Built-in Exceptions………………………………………………………………………… 547

PYTHON MULTITHREADING………………………………………………………………………….. 552

  1. Python – Multithreading…………………………………………………………………………….. 553
  2. Python – Thread Life cycle…………………………………………………………………………… 554

Python – The _thread Module…………………………………………………………………………………………. 554

Python – The threading Module………………………………………………………………………………………. 554

  1. Python – Creating a Thread………………………………………………………………………….. 556
  2. Python – Starting a Thread………………………………………………………………………….. 558
  3. Python – Joining the Threads……………………………………………………………………….. 559
  4. Python – Naming the Threads………………………………………………………………………. 560

The daemon Property……………………………………………………………………………………………………… 560

  1. Python – Thread Scheduling…………………………………………………………………………. 562
  2. Python – Thread Pools……………………………………………………………………………….. 565

What is a Thread Pool?……………………………………………………………………………………………………. 565

The Future Class……………………………………………………………………………………………………………… 565

The ThreadPoolExecutor Class…………………………………………………………………………………………. 566

  1. Python – Main Thread………………………………………………………………………………… 568
  2. Python – Thread Priority…………………………………………………………………………….. 570
  3. Python – Daemon Threads…………………………………………………………………………… 573
  4. Python – Synchronizing Threads……………………………………………………………………. 576

PYTHON SYNCHRONIZATION………………………………………………………………………… 578

  1. Python – Inter-Thread Communication……………………………………………………………. 579

The Event Object…………………………………………………………………………………………………………….. 579

The Condition Object………………………………………………………………………………………………………. 581

  1. Python – Thread Deadlock…………………………………………………………………………… 584

The Lock Object………………………………………………………………………………………………………………. 584

The Semaphore Object……………………………………………………………………………………………………. 586

  1. Python – Interrupting a Thread……………………………………………………………………… 589

PYTHON NETWORKING………………………………………………………………………………… 591

  1. Python – Network Programming……………………………………………………………………. 592
  2. 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

  1. Python – URL Processing…………………………………………………………………………….. 604

The urllib.parse Module…………………………………………………………………………………………………… 604

The urllib.request Module……………………………………………………………………………………………….. 606

The Request Object…………………………………………………………………………………………………………. 607

The urllib.error Module…………………………………………………………………………………………………… 609

  1. Python – Generics…………………………………………………………………………………….. 610

PYTHON MISCELLANEOUS……………………………………………………………………………. 612

  1. 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

  1. 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

  1. Python – Iterators…………………………………………………………………………………….. 713

Asynchronous Iterator…………………………………………………………………………………………………….. 716

  1. Python – Generators………………………………………………………………………………….. 718

Asynchronous Generator…………………………………………………………………………………………………. 720

  1. Python – Closures…………………………………………………………………………………….. 723

Nested Functions…………………………………………………………………………………………………………….. 723

What is a Closure?…………………………………………………………………………………………………………… 724

nonlocal Keyword……………………………………………………………………………………………………………. 724

  1. Python – Decorators………………………………………………………………………………….. 726

@classmethod Decorator………………………………………………………………………………………………… 728

@staticmethod Decorator……………………………………………………………………………………………….. 729

@property Decorator……………………………………………………………………………………………………… 730

  1. Python – Recursion……………………………………………………………………………………. 732
  2. 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

  1. 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

  1. 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

  1. Python – Weak References………………………………………………………………………….. 761

The callback Function………………………………………………………………………………………………………. 762

Finalizing Objects…………………………………………………………………………………………………………….. 763

WeakKeyDictionary…………………………………………………………………………………………………………. 764

WeakValueDictionary……………………………………………………………………………………………………… 765

  1. Python – Serialization………………………………………………………………………………… 768

Pickle Protocols………………………………………………………………………………………………………………. 768

dump() and load()……………………………………………………………………………………………………………. 768

dumps() and loads()………………………………………………………………………………………………………… 769

Pickler Class……………………………………………………………………………………………………………………. 770

Unpickler Class………………………………………………………………………………………………………………… 770

  1. Python – Templating………………………………………………………………………………….. 771

substitute()……………………………………………………………………………………………………………………… 771

safe_substitute()…………………………………………………………………………………………………………….. 772

is_valid()…………………………………………………………………………………………………………………………. 773

get_identifiers()………………………………………………………………………………………………………………. 773

  1. 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

  1. Python – Performance Measurement……………………………………………………………… 784
  2. 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

  1. 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

  1. 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

  1. 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

  1. Python – Command-Line Arguments………………………………………………………………. 914

The getopt Module………………………………………………………………………………………………………….. 916

The argparse Module………………………………………………………………………………………………………. 918

  1. Python – Docstrings…………………………………………………………………………………… 922
  2. Python – JSON…………………………………………………………………………………………. 926

JSONEncoder Class………………………………………………………………………………………………………….. 927

JSONDEcoder class………………………………………………………………………………………………………….. 929

JSON with Files/Streams………………………………………………………………………………………………….. 929

  1. Python – Sending Emails…………………………………………………………………………….. 931

xxvii

smptlib.SMTP() Function…………………………………………………………………………………………………. 931

The smtpd Module………………………………………………………………………………………………………….. 932

Using gmail SMTP……………………………………………………………………………………………………………. 934

  1. 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

  1. Python – Tools / Utilities…………………………………………………………………………….. 946

The dis Module……………………………………………………………………………………………………………….. 946

The pdb Module……………………………………………………………………………………………………………… 947

The profile Module………………………………………………………………………………………………………….. 948

The tabnanny Module……………………………………………………………………………………………………… 949

  1. Python – GUIs………………………………………………………………………………………….. 950

IDLE………………………………………………………………………………………………………………………………… 950

Jupyter Notebook……………………………………………………………………………………………………………. 951

VS Code………………………………………………………………………………………………………………………….. 953

PyCharm…………………………………………………………………………………………………………………………. 955