0% found this document useful (0 votes)
8 views11 pages

Python Programming Fundamentals Guide

The document is a comprehensive guide on computer and Python programming, covering topics from basic concepts to advanced programming techniques. It includes chapters on Python syntax, data types, control statements, functions, and object-oriented programming. Each chapter features learning outcomes, summaries, key terms, and review questions to reinforce understanding.

Uploaded by

chiranib.dhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

Python Programming Fundamentals Guide

The document is a comprehensive guide on computer and Python programming, covering topics from basic concepts to advanced programming techniques. It includes chapters on Python syntax, data types, control statements, functions, and object-oriented programming. Each chapter features learning outcomes, summaries, key terms, and review questions to reinforce understanding.

Uploaded by

chiranib.dhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Contents

About the Authors ii


Preface vii
Visual Walkthrough x
Acknowledgements xiii
1. Introduction to Computer and Python Programming 1
Learning Outcomes 1
Chapter Outline 1
1.1 Introduction 1
1.2 What is a Computer? 2
1.2.1 Input/Output (I/O) Unit 2
1.2.2 Central Processing Unit (CPU) 2
1.2.3 Memory Unit 2
1.3 Overview of Programming Languages 3
1.3.1 Machine Language 3
1.3.2 Assembly Language 3
1.3.3 High-level Language 4
1.4 History of Python 5
1.4.1 Why Python? 5
1.4.2 Installing Python in Windows 6
1.4.3 Starting Python in Different Execution Modes 9
1.5 Installing Python in Ubuntu 14
1.6 Executing Python Programs 15
1.6.1 Writing the First Python Program in Script Mode 16
1.7 Commenting in Python 18
1.8 Internal Working of Python 19
1.9 Python Implementations 19
1.9.1 Jython 20
1.9.2 IronPython 20

00-PY-CH_Prelims.indd xv 10/30/2017 10:13:42 AM


xvi Contents

1.9.3 Stackless Python 20


1.9.4 PyPy 20
Summary 20
Key Terms 21
Review Questions 21
A. Multiple Choice Questions 21
B. True or False 22
C. Exercise Questions 22
D. Programming Assignments 22
2. Basics of Python Programming 23
Chapter Outline 23
Learning Outcomes 23
2.1 Introduction 24
2.2 Python Character Set 24
2.3 Token 24
2.3.1 Literal 25
2.3.2 Value and Type on Literals 25
2.3.3 Keywords 26
2.3.4 Operator 26
2.3.5 Delimiter 26
2.3.6 Identifier/Variable 26
2.4 Python Core Data Type 27
2.4.1 Integer 27
2.4.2 Floating Point Number 29
2.4.3 Complex Number 29
2.4.4 Boolean Type 30
2.4.5 String Type 30
2.5 The print() Function 31
2.5.1 The print()Function with end Argument 33
2.6 Assigning Value to a Variable 34
2.6.1 More on Assigning Values to Variables 34
2.6.2 Scope of a Variable 35
2.7 Multiple Assignments 35
2.8 Writing Simple Programs in Python 37
2.9 The input() Function 38
2.9.1 Reading String from the Console 38
2.10 The eval() Function 41
2.10.1 Apply eval() to input() Function 42
2.11 Formatting Number and Strings 43
2.11.1 Formatting Floating Point Numbers 44
2.11.2 Justifying Format 45
2.11.3 Integer Formatting 45

00-PY-CH_Prelims.indd xvi 10/30/2017 10:13:42 AM


Contents xvii

2.11.4 Formatting String 46


2.11.5 Formatting as a Percentage 46
2.11.6 Formatting Scientific Notation 47
2.12 Python Inbuilt Functions 47
2.12.1 The ord and chr Functions 50
Summary 51
Key Terms 51
Review Questions 52
A. Multiple Choice Questions 52
B. True or False 53
C. Exercise Questions 53
D. Programming Assignments 54
3. Operators and Expressions 55
Chapter Outline 55
Learning Outcomes 55
3.1 Introduction 55
3.2 Operators and Expressions 56
3.3 Arithmetic Operators 56
3.3.1 Unary Operators 56
3.3.2 Binary Operators 57
3.4 Operator Precedence and Associativity 66
3.4.1 Example of Operator Precedence 67
3.4.2 Associativity 67
3.5 Changing Precedence and Associativity
of Arithmetic Operators 68
3.6 Translating Mathematical Formulae into Equivalent Python Expressions 70
3.7 Bitwise Operator 71
3.7.1 The Bitwise AND (&) Operator 72
3.7.2 The Bitwise OR (|) Operator 73
3.7.3 The Bitwise XOR (^) Operator 74
3.7.4 The Right Shift (>>) Operator 76
3.7.5 The Left Shift (<<) Operator 77
3.8 The Compound Assignment Operator 78
Mini Project: Goods Service Tax (GST) Calculator 79
Summary 81
Key Terms 81
Review Questions 81
A. Multiple Choice Questions 81
B. True or False 82
C. Exercise Questions 83
D. Programming Assignments 85

00-PY-CH_Prelims.indd xvii 10/30/2017 10:13:42 AM


xviii Contents

4. Decision Statements 86
Chapter Outline 86
Learning Outcomes 86
4.1 Introduction 86
4.2 Boolean Type 87
4.3 Boolean Operators 88
4.3.1 The not Operator 88
4.3.2 The and Operator 88
4.3.3 The or Operator 89
4.4 Using Numbers with Boolean Operators 89
4.5 Using String with Boolean Operators 90
4.6 Boolean Expressions and Relational Operators 90
4.7 Decision Making Statements 92
4.7.1 The if Statements 92
4.7.2 The if-else Statement 94
4.7.3 Nested if Statements 98
4.7.4 Multi-way if-elif-else Statements 99
4.8 Conditional Expressions 103
Mini Project: Finding the Number of Days in a Month 105
Summary 106
Key Terms 107
Review Questions 107
A. Multiple Choice Questions 107
B. True or False 109
C. Exercise Questions 110
D. Programming Assignments 110
5. Loop Control Statements 111
Chapter Outline 111
Learning Outcomes 111
5.1 Introduction 111
5.2 The while Loop 112
5.2.1 Details of while Loop 112
5.2.2 Flowchart for while Loop 113
5.2.3 Some More Programs on while Loop 115
5.3 The range() Function 117
5.3.1 Examples of range() Function 117
5.4 The for Loop 118
5.4.1 Details of for Loop 118
5.4.2 Some More Programs on for Loop 119
5.5 Nested Loops 123
5.5.1 Some More Programs on Nested Loops 124
5.6 The break Statement 127

00-PY-CH_Prelims.indd xviii 10/30/2017 10:13:42 AM


Contents xix

5.7 The continue Statement 129


Mini Project: Generate Prime Numbers using Charles Babbage Function 131
Summary 133
Key Terms 133
Review Questions 133
A. Multiple Choice Questions 133
B. True or False 135
C. Exercise Questions 136
D. Programming Assignments 137
6. Functions 138
Chapter Outline 138
Learning Outcomes 138
6.1 Introduction 138
6.2 Syntax and Basics of a Function 139
6.3 Use of a Function 140
6.4 Parameters and Arguments in a Function 141
6.4.1 Positional Arguments 143
6.4.2 Keyword Arguments 144
6.4.3 Parameter with Default Values 145
6.5 The Local and Global Scope of a Variable 147
6.5.1 Reading Global Variables from a Local Scope 148
6.5.2 Local and Global Variables with the Same Name 149
6.5.3 The Global Statement 149
6.6 The return Statement 150
6.6.1 Returning Multiple Values 153
6.6.2 Assign Returned Multiple Values to Variable(s) 154
6.7 Recursive Functions 154
6.8 The Lambda Function 155
Mini Project: Calculation of Compound Interest and Yearly Analysis of Interest and Principal
Amount 156
Summary 159
Key Terms 159
Review Questions 159
A. Multiple Choice Questions 159
B. True or False 162
C. Exercise Questions 162
D. Programming Assignments 163
7. Strings 164
Learning Outcomes 164
Chapter Outline 164
7.1 Introduction 165
7.2 The str class 165

00-PY-CH_Prelims.indd xix 10/30/2017 10:13:42 AM


xx Contents

7.3 Basic Inbuilt Python Functions for String 165


7.4 The index[] Operator 165
7.4.1 Accessing Characters via Negative Index 166
7.5 Traversing String with for and while Loop 167
7.5.1 Traversing with a while Loop 168
7.6 Immutable Strings 168
7.7 The String Operators 169
7.7.1 The String Slicing Operator [start: end] 169
7.7.2 String Slicing with Step Size 170
7.7.3 The String +, * and in Operators 171
7.8 String Operations 172
7.8.1 String Comparison 172
7.8.2 The String .format() Method() 173
7.8.3 The split() Method 174
7.8.4 Testing String 175
7.8.5 Searching Substring in a String 176
7.8.6 Methods to Convert a String into Another String 177
7.8.7 Stripping Unwanted Characters from a String 179
7.8.8 Formatting String 180
7.8.9 Some Programs on String 181
Mini Project: Conversion of HexDecimal Number into its Equivalent Binary Number 185
Summary 188
Key Terms 188
Review Questions 188
A. Multiple Choice Questions 188
B. True or False 190
C. Exercise Questions 190
D. Programming Assignments 191
8. Lists 192
Learning Outcomes 192
Chapter Outline 192
8.1 Introduction 193
8.2 Creating Lists 193
8.3 Accessing the Elements of a List 194
8.4 Negative List Indices 194
8.5 List Slicing [Start: end] 195
8.6 List Slicing with Step Size 196
8.6.1 Some More Complex Examples of List Slicing 196
8.7 Python Inbuilt Functions for Lists 196
8.8 The List Operator 198
8.9 List Comprehensions 201
8.9.1 Some More Examples of List Comprehension 202

00-PY-CH_Prelims.indd xx 10/30/2017 10:13:42 AM


Contents xxi

8.10 List Methods 204


8.11 List and Strings 208
8.12 Splitting a String in List 208
8.13 Passing List to a Function 209
8.14 Returning List from a Function 211
Summary 219
Key Terms 219
Review Questions 219
A. Multiple Choice Questions 219
B. True or False 221
C. Exercise Questions 221
D. Programming Assignments 222
9. List Processing: Searching and Sorting 224
Chapter Outline 224
Learning Outcomes 224
9.1 Introduction 224
9.2 Searching Techniques 225
9.2.1 Linear/Sequential Search 225
9.2.2 The Binary Search 227
9.3 Introduction to Sorting 231
9.3.1 Types of Sorting 231
9.3.2 Bubble Sort 232
9.3.3 Selection Sort 234
9.3.4 Insertion Sort 237
9.3.5 Quick Sort 238
9.3.6 Merge Sort 243
Mini Project: Sorting Based on the Length of Each Element 247
Summary 249
Key Terms 249
Review Questions 249
A. Multiple Choice Questions 249
B. True or False 250
C. Exercise Questions 250
D. Programming Assignments 251
10. Object-Oriented Programming: Class, Objects and Inheritance 252
Learning Outcomes 252
Chapter Outline 252
10.1 Introduction 253
10.2 Defining Classes 253
10.2.1 Adding Attributes to a Class 254
10.2.2 Accessing Attributes of a Class 255
10.2.3 Assigning Value to an Attribute 255

00-PY-CH_Prelims.indd xxi 10/30/2017 10:13:42 AM


xxii Contents

10.3 The Self-parameter and Adding Methods to a Class 256


10.3.1 Adding Methods to a Class 256
10.3.2 The Self-parameter 256
10.3.3 Defining Self-parameter and Other Parameters in a Class Method 257
10.3.4 The Self-parameter with Instance Variable 258
10.3.5 The Self-parameter with Method 259
10.4 Display Class Attributes and Methods 260
10.5 Special Class Attributes 261
10.6 Accessibility 262
10.7 The __init__ Method (Constructor) 263
10.7.1 Attributes and __init__ Method 264
10.7.2 More Programs on __init__ Method 265
10.8 Passing an Object as Parameter to a Method 265
10.9 __del__() (Destructor Method) 267
10.10 Class Membership Tests 269
10.11 Method Overloading in Python 269
10.12 Operator Overloading 271
10.12.1 Special Methods 272
10.12.2 Special Methods for Arithmetic Operations 272
10.12.3 Special Methods for Comparing Types 273
10.12.4 Reference Equality and Object Equality 274
10.12.5 Special Methods for Overloading Inbuilt Functions 276
10.13 Inheritance 276
10.14 Types of Inheritance 277
10.15 The Object Class 278
10.16 Inheritance in Detail 278
10.17 Subclass Accessing Attributes of Parent Class 280
10.18 Multilevel Inheritance in Detail 281
10.19 Multiple Inheritance in Detail 282
10.19.1 More Practical Examples on Inheritance 283
10.20 Using super() 285
10.20.1 Super to Call Super Class Constructor 286
10.21 Method Overriding 287
10.22 Precaution: Overriding Methods in Multiple Inheritance 289
Mini Project: Arithmetic Operations on Complex Numbers 290
Summary 294
Key Terms 294
Review Questions 295
A. Multiple Choice Questions 295
B. True or False 297
C. Exercise Questions 298
D. Programming Assignments 299

00-PY-CH_Prelims.indd xxii 10/30/2017 10:13:42 AM


Contents xxiii

11. Tuples, Sets and Dictionaries 301


Chapter Outline 301
Learning Outcomes 301
11.1 Introduction to Tuples 301
11.1.1 Creating Tuples 302
11.1.2 The tuple()Function 302
11.1.3 Inbuilt Functions for Tuples 303
11.1.4 Indexing and Slicing 303
11.1.5 Operations on Tuples 304
11.1.6 Passing Variable Length Arguments to Tuples 304
11.1.7 Lists and Tuples 305
11.1.8 Sort Tuples 306
11.1.9 Traverse Tuples from a List 306
11.1.10 The zip() Function 306
11.1.11 The Inverse zip(*) Function 308
11.1.12 More Examples on zip(*) Function 308
11.1.13 More Programs on Tuples 309
11.2 Sets 309
11.2.1 Creating Sets 309
11.2.2 The Set in and not in Operator 310
11.2.3 The Python Set Class 310
11.2.4 Set Operations 312
11.3 Dictionaries 313
11.3.1 Need of Dictionaries 313
11.3.2 Basics of Dictionaries 313
11.3.3 Creating a Dictionary 314
11.3.4 Adding and Replacing Values 315
11.3.5 Retrieving Values 316
11.3.6 Formatting Dictionaries 317
11.3.7 Deleting Items 317
11.3.8 Comparing Two Dictionaries 317
11.3.9 The Methods of Dictionary Class 318
11.3.10 Traversing Dictionaries 319
11.3.11 Nested Dictionaries 320
11.3.12 Traversing Nested Dictionaries 320
11.3.13 Simple Programs on Dictionary 322
11.3.14 Polynomials as Dictionaries 325
Mini Project: Orange Cap Calculator 326
Summary 328
Key Terms 328
Review Questions 329
A. Multiple Choice Questions 329

00-PY-CH_Prelims.indd xxiii 10/30/2017 10:13:42 AM


xxiv Contents

B. True or False 330


C. Exercise Questions 331
D. Programming Assignments 332
12. Graphics Programming: Drawing with Turtle Graphics 333
Chapter Outline 333
Learning Outcomes 333
12.1 Introduction 333
12.2 Getting Started with the Turtle Module 334
12.3 Moving the Turtle in any Direction 335
12.3.1 Programs to Draw Different Shapes 338
12.4 Moving Turtle to Any Location 339
12.5 The color, bgcolor, circle and Speed Method of Turtle 341
12.6 Drawing with Colors 343
12.7 Drawing Basic Shapes using Iterations 344
12.8 Changing Color Dynamically Using List 347
12.9 Turtles to Create Bar Charts 347
Mini Project: Turtle Racing Game 349
Summary 353
Key Terms 353
Review Questions 353
A. Multiple Choice Questions 353
B. True or False 354
C. Exercise Questions 354
D. Programming Assignments 355
13. File Handling 356
Chapter Outline 356
Learning Outcomes 356
13.1 Introduction 356
13.2 Need of File Handling 357
13.3 Text Input and Output 357
13.3.1 Opening a File 357
13.3.2 Writing Text to a File 358
13.3.3 Closing a File 360
13.3.4 Writing Numbers to a File 360
13.3.5 Reading Text from a File 362
13.3.6 Reading Numbers from a File 363
13.3.7 Reading Multiple Items on one Line 365
13.3.8 Appending Data 370
13.4 The seek() Function 370
13.5 Binary Files 372
13.5.1 Reading Binary Files 373

00-PY-CH_Prelims.indd xxiv 10/30/2017 10:13:42 AM


Contents xxv

13.6 Accessing and Manipulating Files and Directories on a Disk 373


Mini Project: Extracting Data from a File and Performing Some Basic Mathematical
Operations on It 374
Summary 376
Key Terms 376
Review Questions 377
A. Multiple Choice Questions 377
B. True or False 378
C. Exercise Questions 378
D. Programming Assignments 379

Appendix I: Project for Creating a Phone Book Directory 380


Appendix II: Importing Modules in Python 388
Appendix III: Python Keywords 390
Appendix IV: ASCII Table 391

Index 393

00-PY-CH_Prelims.indd xxv 10/30/2017 10:13:42 AM

You might also like