Simplified PythoNotes
2nd Edition
Amjad Ali, Ph.D.
With
Tayyaba Saeed
Syeda Zahra Kazmi
Centre for Advanced Studies in
Pure and Applied Mathematics
(CASPAM)
Bahauddin Zakariya University
(BZU)
Multan, 60800 Pakistan.
Please visit: www.TimeRenders.com.pk https://github.com/DrAmjadAli11/
Prime Distributor:
NEW BOOKS N BOOKS, Hussain Arcade, Near Trend Super Store, Gol Bagh Market,
Gulgasht Colony, Multan. 061-6511828, 061-6223080, 0333-6110619 (WhatsApp)
Table of Contents
Chapter 1: Basic Understanding about Computer Programming
A Speech or Conversation with the Reader............................................................................................... 1
Chapter 2: Basic Elements of a Python Program
Chapter Contents ...................................................................................................................................... 9
Skill 0: How to Use a Python IDE for Programming ............................................................................. 10
Skill 1: How to Make Starting and Ending of a Python Program ......................................................... 17
Skill 2: Understanding the Importance of Python Built-in Functions and Library Functions ............... 18
Skill 3: How to Make Simple Output Statements ................................................................................ 19
Skill 4: How to Name and Initialize Variables Explicitly ....................................................................... 23
Skill 5: How to Make Input Statements ............................................................................... 28
Skill 6: How to Make Output Statements for Printing Values ............................................................. 29
Skill 7: How to Deal with Variables of Different Data Types ............................................................... 32
Skill 8: How to Make Statements for Arithmetic Expressions and Assignments ................. 38
Skill 9: Basic Concepts about Memory in Python ................................................................ 50
Skill 10: How to Import Modules/Libraries and Use their Functions .................................................... 58
Skill 11: A Brief Note on Data Types in Python .................................................................................. 65
Excercises ............................................................................................................................................... 70
Chapter 3: Selection Structures
Chapter Contents .................................................................................................................................... 73
Skill 12: Understanding of the Flow Control Structures in Computer Programs .................................. 74
Skill 13: Relational Operators and Boolean Expressions ....................................................................... 75
Skill 14: Single-Selection Structure: using if Statement ........................................................................ 77
Skill 15: Double-Selection Structure: using if-else Statement ............................................................ 79
Skill 16: Multiple-Selection Structure: using if-elif-else Statement .................................................. 82
Skill 17: Multiple-Selection Structure: using Multiple, Nested if-else Statements ............................ 93
Skill 18: Multiple-Selection Structure: using match Statement .......................................................... 94
Skill 19: Logical or Boolean Operators ................................................................................................ 97
Excercises ............................................................................................................................................. 100
Chapter 4: Count-Controlled Repetition Structures
Chapter Contents .................................................................................................................................. 105
Skill 20: Count-Controlled Repetition using for Loop ........................................................................ 105
Skill 21: Generating Sequences and Sums (Series) using Count-Controlled Repetition .................... 115
Skill 22: Count-Controlled Repetition using while Loop .................................................................... 132
Excercises ............................................................................................................................................. 136
Chapter 5: Sentinel-Controlled Repetition Structures
Chapter Contents .................................................................................................................................. 141
Skill 23: Sentinel-Controlled Repetition using while Loop ................................................................. 142
Skill 24: Altering the Flow of Control in a Loop using continue statement ........................................ 149
Skill 25: Altering the Flow of Control in a Loop using break statement ............................................. 150
Skill 26: Sentinel-Controlled Repetition using for Loop ..................................................................... 151
Excercises ............................................................................................................................................. 151
Chapter 6: Functions
Chapter Contents .................................................................................................................................. 153
Skill 27: How to Create User-Defined Functions ................................................................................ 154
Skill 28: Types of Arguments and Parameters of Functions .......................................................... 175
Skill 29: Insight into Passing Arguments to Functions ....................................................................... 178
Excercises ............................................................................................................................................. 181
Chapter 7: Lists and Tuples
Chapter Contents .................................................................................................................................. 183
Skill 30: Creating Lists and Tuples ...................................................................................................... 184
Skill 31: Slicing: Addressing a Range of Elements in a List/Tuple ....................................................... 197
Skill 32: Some Basic Operations on Lists and Tuples ......................................................................... 203
Skill 33: Two Dimensional Lists and Tuples ........................................................................................ 208
Skill 34: Explicit Traversing in Lists and Tuples, and List Comprehension .......................................... 211
Skill 35: Achieving Efficiency: Using Powerful One-Liner Codes and Other Tips/Tricks...................... 215
Skill 36: Descriptive Statistics for Data Science in Python .............................................................. 221
Excercises ............................................................................................................................................. 224
Chapter 8: Array-Oriented Programming with NumPy
Chapter Contents .................................................................................................................................. 227
Skill 37: How to Create One-Dimensional Arrays .............................................................................. 228
Skill 38: How to Create Two-Dimensional Arrays .............................................................................. 242
Skill 39: How to Address NumPy Arrays .......................................................................................... 249
2
Skill 40: Manipulations with NumPy Arrays .................................................................................... 254
Skill 41: Mathematical Operations with NumPy Arrays .................................................................. 263
Skill 42: Using Explicit Loops for Iterative Operations with One-Dimensional Arrays .................. 274
Skill 43: Using Explicit Loops for Iterative Operations with Two-Dimensional Arrays .................. 287
Excercises ............................................................................................................................................. 296
Chapter 9: Dictionaries, Sets, and Strings
Chapter Contents .................................................................................................................................. 299
Skill 44: How to Create Dictionaries and Sets ................................................................................. 299
Skill 45: Some Methods for Dictionaries ......................................................................................... 306
Skill 46: Some Methods for Sets ...................................................................................................... 308
Skill 47: Strings……………………………………………………………………………………………… ………………..310
Chapter 10: Object-Oriented Programming: An Introduction
Chapter Contents .................................................................................................................................. 319
Skill 48: What is Object-Oriented Programming (OOP) .................................................................. 320
Skill 49: Inheritance in OOP ............................................................................................................... 325
Skill 50: Polymorphism in OOP .......................................................................................................... 327