Introduction To Computing and Problem Solving With Python.
Introduction To Computing and Problem Solving With Python.
OK
PYTHON
BO
Jeeva Jose
Assistant Professor, Department of Computer Applications
NA
Baselios Poulose II Catholicos College[BPC]
Piravom - 686 664, Ernakulam District, Kerala, India
P. Sojan Lal
Professor, Department of Computer Science and Engineering
Mar Baselios Institute of Technology and Science[MBITS]
Kothamangalam - 686 693, Ernakulam District, Kerala, India
AN
KH
S
OK
Price : ` 295/-
BO
Introduction to Computing and Problem Solving with PYTHON
Jeeva Jose & P. Sojan Lal
ISBN: 978-93-82609-81-0
AN
Edition: 2016
Published by:
KHANNA BOOK PUBLISHING CO. (P) LTD.
4C/4344, Ansari Road, Darya Ganj, New Delhi-110 002
Phone: 011-23244447-48 Mobile: +91-9910909320
KH
E-mail: [email protected]
OK
Jeeva Jose completed PhD in Computer Science from Mahatma Gandhi University,
Kerala, India and is a faculty member at BPC College, Kerala. Her passion is teaching
and areas of interests include World Wide Web, Data Mining and Cyber laws. She has
been in higher education for the last 15 years and has completed three research projects
funded by UGC and KSCSTE. She has published more than twenty research papers in
various refereed journals and conference proceedings. She has edited three books
and has given many invited talks in various conferences. She is a recipient of ACM-W
BO
Scholarship provided by Association for Computing Machinery, New York.
P. Sojan Lal was awarded PhD from Cochin University of Science and Technology,
Kerala, India. He is Professor-in-charge of Academic Research, Department of
Computer Science and Engineering, MBITS, Kerala, India and Research Supervisor
for PhD programs of University of Petroleum & Energy Studies, Dehra Dun, India as
well as School of Computer Sciences, Mahatma Gandhi University, Kerala, India. He
has 29 years of academic and industrial experience with 60 publications inclusive
of two technical books. His joint publications are recorded with World Records
NA
India (2014) for most number of papers in several international technical journals
within short duration. He has also obtained MBA from Strathclyde Business School, Scotland, UK and
is a Fellow of The Institution of Engineers (FIE-India). He was the District Operating Board Member
for ASME, Middle East and Africa region. He is a member of ISTE, ASME, IEEE, CSI and Engineering
Council (UK). He is listed in Marquis whos Who in the World since 2009 as the biographical reference
representing the worlds most accomplished individuals.
AN
KH
S
Preface
OK
Python is a widely used general-purpose, high-level programming language. This is an Open Source
Software and its source code is available with a license in which the Copyright holder provides the
rights to study, change, and distribute the software to anyone and for any purpose. This can run on
any hardware platform (PC, Mac, Sun Sparc, etc.) or software platform (Linux, MacOS, Unix, Windows,
etc.). Its design philosophy emphasizes code readability, and its syntax allows programmers to
express concepts in least lines of code than in languages like C++ or Java. Python supports multiple
programming paradigms, including object-oriented, imperative and functional programming or
BO
procedural styles. It features a dynamic type system and automatic memory management and has a
large and comprehensive standard library.
Python was conceived in the late 1980s and its implementation was started in December 1989 by
Guido van Rossum at Centrum Wiskunde & Informatica which is a research center in Netherlands .The
non-profit organization Python Software Foundation fosters development of the Python community
and is responsible for various processes within the Python community, including developing the core
Python distribution, managing intellectual rights, developer conferences including PyCon, and raising
funds. The softwares like YouTube, DropBox, Instagram, etc. are to name a few which is written in
Python.
NA
Python is used in many application domains. The Python Package Index lists thousands of third
party modules for Python. Pythons standard library and the community-contributed modules allow
for endless possibilities. In Web and Internet development, Python offers many choices of Web
development like Django and Pyramid for Frame work, Flask and Bottle for Micro-frameworks and
advanced content management systems such as Plone & Django CMS. In addition to this, Pythons
standard library supports many Internet Protocols such as HTML, XML, JSON, E-mail processing, FTP,
IMAP etc. Python is in education presently from post secondary and Government is utilizing this for
Administration, Homeland Security, Public Safety, Traffic Control, Urban Infrastructure etc. In Business
AN
Python is using in domains such as Consumer Goods Industry, Aviation, Medical, Industrial, Financial
services, GIS & Mapping, Marine and Lighting. Python is used in areas of Customer Relationship
Management (CRM), Content & Document Management, Energy Conservation, E-Commerce, Enterprise
Resource Planning (ERP), Knowledge Management, Manufacturing, Product Development, Project
Management, Quality Control, Six Sigma, Lean, Relational Online Analytical Processing (ROLAP), Risk
Management, Simulation etc. In Network Programming, Python is used to control Firmware updates,
bringing desktop applications to the Internet etc. In Software, Python plays a right role in Accessibility,
Code Generation, Computer Graphics, Cross-platform Development, Data Mining, Documentation
KH
Development, E-mail, Embedded Systems, Functional Testing, Groupware, Legacy System Integration,
Rapid Application Development, RSS aggregator, User Interface, Visual Effects etc.
Python is widely used in areas such as Biology, Geography, Language Processing, Astrology etc.
Based on the application, many packages and libraries are developed in Python. SciPy is a collection of
packages for Mathematics, Science, and Engineering. Pandas is a data analysis and modeling library.
IPython is a powerful interactive shell that features easy editing and recording of a work session, and
supports visualizations and parallel computing.
|x| Python
S
This book will help every student, teacher and researcher to understand the computing basics and
advanced Python Programming language. The simple style of presentation makes this a friend for self
learners.
OK
The first two Chapters introduce the reader on digital computers and programming. The third
Chapter gives an introduction to Python which includes reserved keywords, identifiers, variables
and operators. The fourth Chapter gives detailed explanation about data types and their operations.
Chapter 5 illustrates flow control techniques which include decision making and looping. Functions
are covered in Chapter 6. Chapter 7 explains built-in modules, user defined modules, packages, time,
calendar and datetime modules. File handling is covered in Chapter 8. The entire concept of Object
Oriented Programming is explained in Chapter 9. Various Exception Handling techniques are explained
in Chapter 10. The Chapters 11 & 12 cover advanced topics like Python Regular Expressions and
BO
Database Programming in detail.
All Chapters have worked out programs, illustrations, review and frequently asked interview
questions. More than 300 solved lab exercises available in this book is tested in Python 3.4.3 version
for Windows. Please note that the syntax in early versions of Python and UNIX version of Python is
slightly different from Python 3.4.3 for Windows.
NA
AN
KH
S
Contents
OK
Acknowledgement...........................................................................................................................iii
Foreword..........................................................................................................................................v
About the Authors.......................................................................................................................... vii
Preface............................................................................................................................................ ix
CHAPTER 1: Introduction to Computers............................................................................................1
BO
1.1 Introduction..............................................................................................................1
1.2 Components of Digital Computer.............................................................................2
1.3 Hardware and Software............................................................................................3
1.4 Operating System......................................................................................................4
1.5 Drivers.......................................................................................................................6
1.6 Languages of Computer............................................................................................7
1.7 Translators (Compiler, Interpreter and Assembler)...................................................7
1.8 Personal Computer...................................................................................................8
NA
1.9 Internet and E-commerce ........................................................................................9
1.10 Search Engines..........................................................................................................9
1.11 Conclusion.................................................................................................................9
1.12 Review Questions....................................................................................................10
CHAPTER 2: Program Logic and Flow Charts....................................................................................11
2.1 Introduction to Program Logic................................................................................11
2.2 Methodology of Problem Solving............................................................................12
AN
2.3 Flowcharts...............................................................................................................13
2.4 Flow Chart Symbols.................................................................................................14
2.5 Exercise ..................................................................................................................15
2.6 Conclusion . ............................................................................................................17
2.7 Review Questions . .................................................................................................17
CHAPTER 3: Introduction to Python................................................................................................18
KH
S
3.7 Indentation in Python.............................................................................................23
3.8 Multi-Line Statements.............................................................................................23
3.9 Multiple Statement Group (Suite)...........................................................................24
OK
3.10 Quotes in Python....................................................................................................24
3.11 Input, Output and Import Functions ......................................................................24
3.11.1 Displaying the Output .............................................................................24
3.11.2 Reading the Input......................................................................................25
3.11.3 Import function ........................................................................................26
3.12 Operators................................................................................................................26
3.12.1 Arithmetic Operators................................................................................27
BO
3.12.2 Comparison Operators..............................................................................28
3.12.3 Assignment Operators...............................................................................28
3.12.4 Bitwise Operators......................................................................................30
3.12.5 Logical Operators......................................................................................31
3.12.6 Membership Operators.............................................................................31
3.12.7 Identity Operators.....................................................................................32
3.12.8 Operator Precedence................................................................................32
3.13 Conclusion...............................................................................................................33
NA
3.14 Review Questions....................................................................................................33
CHAPTER 4: Data Types and Operations..........................................................................................34
4.1 Numbers.................................................................................................................34
4.1.1 Mathematical Functions...........................................................................35
4.1.2 Trigonometric Functions...........................................................................37
4.1.3 Random Number Functions......................................................................38
4.2 Strings.....................................................................................................................39
AN
S
4.6 Dictionary................................................................................................................68
4.6.1 Built-in Dictionary Functions.....................................................................69
4.6.2 Built-in Dictionary Methods......................................................................71
OK
4.7 Data Type Conversion.............................................................................................74
4.8 Solved Lab Exercises................................................................................................76
4.9 Conclusion...............................................................................................................82
4.10 Review Questions....................................................................................................82
CHAPTER 5: Flow Control................................................................................................................86
5.1 Decision Making......................................................................................................86
BO
5.1.1 if statement...............................................................................................86
5.1.2 if.else statement....................................................................................88
5.1.3 if...elif...else statement.............................................................. 89
5.1.4 Nested if statement..................................................................................90
5.2 Loops.......................................................................................................................91
5.2.1 for loop.....................................................................................................91
5.2.2 for loop with else.................................................................................93
5.2.3 while loop.................................................................................................94
5.2.4 while loop with else statement.................................................................95
NA
5.3 Nested Loops..........................................................................................................96
5.4 Control Statements.................................................................................................98
5.4.1 break statement.....................................................................................98
5.4.2 continue statement........................................................................... 100
5.4.3 pass statement.................................................................................... 101
5.5 Types of Loops..................................................................................................... 101
5.5.1 Infinite Loop........................................................................................... 101
AN
S
6.3.3 Default Arguments................................................................................. 134
6.3.4 Variable-Length Arguments................................................................... 134
6.4 Anonymous Functions (Lambda Functions)......................................................... 135
OK
6.4.1 Uses of lambda function........................................................................ 136
6.5 Recursive Functions............................................................................................. 137
6.6 Function with more than one return value.......................................................... 138
6.7 Solved Lab Exercises............................................................................................. 139
6.8 Conclusion............................................................................................................ 147
6.9 Review Questions................................................................................................. 148
CHAPTER 7: Modules and Packages.............................................................................................. 149
BO
7.1 Built-in Modules................................................................................................... 149
7.2 Creating Modules................................................................................................. 156
7.3 import Statement................................................................................................ 157
7.3.1 import with renaming............................................................................ 157
7.3.2 fromimport statement........................................................................ 158
7.3.3 import all names.................................................................................... 158
7.4 Locating Modules ................................................................................................ 159
7.4.1 PYTHONPATH variable............................................................................ 159
NA
7.5 Namespaces and Scope....................................................................................... 159
7.6 The dir() function................................................................................................. 160
7.7 The reload() function........................................................................................... 161
7.8 Packages in Python.............................................................................................. 162
7.8.1 Importing modules from a Package....................................................... 162
7.9 Date and Time Modules....................................................................................... 163
7.9.1 The time Module................................................................................ 163
AN
S
8.5 File Methods........................................................................................................ 191
8.6 Renaming a File.................................................................................................... 193
8.7 Deleting a File...................................................................................................... 193
OK
8.8 Directories in Python........................................................................................... 194
8.8.1 mkdir() method...................................................................................... 194
8.8.2 chdir() method....................................................................................... 194
8.8.3 getcwd() method.................................................................................... 195
8.8.4 rmdir() method...................................................................................... 195
8.9 Solved Lab Exercises............................................................................................. 195
8.10 Conclusion............................................................................................................ 203
BO
8.11 Review Questions................................................................................................. 204
CHAPTER 9: Object Oriented Programming................................................................................... 205
9.1 Class Definition.................................................................................................... 206
9.2 Creating Objects................................................................................................... 207
9.3 Built-in Attribute Methods................................................................................... 208
9.4 Built-in Class Attributes........................................................................................ 209
9.5 Destructors in Python.......................................................................................... 211
9.6 Encapsulation....................................................................................................... 211
NA
9.7 Data Hiding........................................................................................................... 212
9.8 Inheritance .......................................................................................................... 212
9.8.1 Deriving a Child Class............................................................................. 213
9.8.2 Multilevel Inheritance............................................................................ 214
9.8.3 Multiple Inheritance.............................................................................. 216
9.8.4 Invoking the Base Class Constructor...................................................... 218
9.9 Method Overriding.............................................................................................. 219
AN
S
10.3 Exception with Arguments................................................................................... 240
10.4 Raising an Exception............................................................................................ 241
10.5 User-defined Exception........................................................................................ 242
OK
10.6 Assertions in Python............................................................................................ 243
10.7 Conclusion............................................................................................................ 243
10.8 Review Questions................................................................................................. 244
CHAPTER 11: Regular Expressions................................................................................................. 245
11.1 The match() function........................................................................................... 245
11.2 The search() function........................................................................................... 247
BO
11.3 Search and Replace.............................................................................................. 247
11.4 Regular Expression Modifiers: Option Flags......................................................... 248
11.5 Regular Expression Patterns................................................................................. 248
11.6 Character Classes................................................................................................. 250
11.7 Special Character Classes..................................................................................... 250
11.8 Repetition Cases................................................................................................... 251
11.9 findall() method................................................................................................... 251
11.10 compile() method................................................................................................ 251
11.11 Solved Lab Exercises............................................................................................. 252
NA
11.12 Conclusion............................................................................................................ 263
11.13 Review Questions................................................................................................. 263
CHAPTER 12: Database Programming........................................................................................... 264
12.1 Connecting to a Database.................................................................................... 265
12.2 Creating Tables..................................................................................................... 266
12.3 INSERT Operation................................................................................................. 266
AN