Preface to the Second Edition i
1 THE FOUNDATIONS 1
1.1 What is Python? 1
1.2 Setting up Python 2
1.3 First Python Program 4
1.4 Variables and Data Types 6
1.5 Input and Output 9
1.6 Comments and Documentation 11
1.7 Basic Operators 11
1.8 Exercises and Solutions 15
2 CONTROL STRUCTURES 21
2.1 Introduction 21
2.2 Conditional Statements 22
2.3 Loops 26
2.4 List Comprehensions 29
2.5 Exercises and Solutions 30
3 FUNCTIONS & MODULAR PROGRAMMING 43
3.1 Functions 43
3.2 Defining and Calling Functions 44
3.3 Parameters and Arguments 45
3.4 Return Values 47
3.5 Scope of Variables 48
3.6 Lambda Functions 50
3.7 Modules and Packages 51
3.8 Recursion 55
3.9 Functional Programming 57
3.10 Exercises and Solutions 58
4 DATA COLLECTIONS 67
4.1 Data Structures 67
4.2 Collection Types 67
4.3 Lists 69
4.4 Tuples 73
4.5 Sets 75
4.6 Dictionaries 79
4.7 Iterating through Collections 83
4.8 Exercises and Solutions 86
5 LIBRARIES & MODULES 97
5.1 Standard Library 97
5.2 Standard Modules 98
5.3 os and sys 103
5.4 pickle and shelve 104
5.5 Importing Modules 105
5.6 Popular Python Libraries 107
5.7 Exercises and Solutions 120
6 FILES & EXCEPTIONS 135
6.1 File Handling 135
6.2 Working with Files 136
6.3 Paths and Directories 141
6.4 Exception Handling 142
6.5 Practical Applications 146
6.6 Exercises and Solutions 148
7 PLOTTING CURVES WITH PYTHON 157
7.1 Plotting in Python 157
7.2 matplotlib 158
7.3 Plot Types 160
7.4 Mathematical Functions 164
7.5 Customizing Plots 169
7.6 Advanced Plotting 174
7.7 Real-World Data 180
7.8 Exercises and Solutions 184
8 OBJECT-ORIENTED PROGRAMMING 193
8.1 Introduction to OOP 193
8.2 Core Concepts 195
8.3 Classes and Objects 196
8.4 Class and Instance Data 198
8.5 Encapsulation 200
8.6 Inheritance 203
8.7 Polymorphism 206
8.8 Abstraction 209
8.9 Special Methods 211
8.10 Practical Projects 214
8.11 Exercises and Solutions 223
9 BASIC ALGORITHMS 245
9.1 Introduction 245
9.2 Understanding Algorithms 246
9.3 Algorithmic Complexity 248
9.4 Search Algorithms 249
9.5 Sorting Algorithms 253
9.6 Exercises and Solutions 258
10 ADVANCED ALGORITHMS & DATA STRUCTURES 267
10.1 Introduction 267
10.2 Recursion 268
10.3 Divide and Conquer 272
10.4 Trees and Graphs 276
10.5 Hash Tables 283
10.6 Exercises and Solutions 289
11 BUILDING GRAPHICAL INTERFACES WITH TKINTER 303
11.1 GUI Programming 303
11.2 tkinter 305
11.3 Widgets 307
11.4 Layout Management 313
11.5 Events and Bindings 317
11.6 Sample Application 321
11.7 Advanced Features 325
11.8 Exercises and Solutions 329
12 FINAL PROJECTS AND BEYOND 361
12.1 Software Development Life Cycle 361
12.2 Learning Through Projects 362
12.3 Foundational Projects 362
12.4 Web Development 388
12.5 Game Development 402
12.6 Data Analysis 422
12.7 Conclusion 434
Index 435