Gowrishankar S. & Veena A.
...
Book Details
Price
|
3.50 USD |
---|---|
Pages
| 465 p |
File Size
|
14,446 KB |
File Type
|
PDF format |
ISBN-13
| 978-0-8153-9437-2 (Hardback) |
Copyright
| 2019 by Taylor & Francis Group, LLC |
Dr. Gowrishankar S. is currently working as Associate Professor
in the Department of Computer Science and Engineering
at Dr.Ambedkar Institute of Technology, Bengaluru, India.
He earned his PhD in Engineering from Jadavpur University,
Kolkata, India in 2010 and MTech in Software Engineering and
BE in Computer Science and Engineering from Visvesvaraya
Technological University (VTU), Belagavi, India, in the years
2005 and 2003 respectively.
From 2011 to 2014 he worked as senior research scientist and
tech lead at Honeywell Technology Solutions, Bengaluru, India.
He has been awarded with the Technical and Innovation Award
and Individual Excellence Award for his contribution towards
successful delivery of projects at Honeywell Technology Solutions.
He has published several papers in various reputed International Journals and Conferences.
He is serving as editor and reviewer for various prestigious International Journals. He is
also member of IEEE, ACM, CSI and ISTE.
He has delivered many keynote addresses and invited talks throughout India on a variety
of subjects related to Computer Science and Engineering. He was instrumental in organizing
several conferences, workshops and seminars. He has also served on the panel of number
of Academic Bodies of Universities and Autonomous Colleges as a BOS and BOE member.
His current research interests are mainly focused on Data Science, including its technical
aspects as well as its applications and implications. Specifically, he is interested in the applications
of Machine Learning, Data Mining and Big Data analytics in Healthcare. He writes articles
on his personal blog at http://www.gowrishankarnath.com. His Twitter handle is @g_s_nath.
Mrs. Veena A. is currently working as Assistant Professor in the
Department of Computer Science and Engineering at
Dr.Ambedkar Institute of Technology, Bengaluru, India, since January 2016.
She completed her MTech in Computer Science and
Engineering from PESIT, Bengaluru, India and BE in Information
Science and Engineering from Visvesvaraya Technological
University (VTU), Belagavi, India, in the years 2011 and 2004 respectively.
From 2004 to 2006 she worked as Software Developer at Envision
Network Technologies, Bengaluru, India, and worked as member
technical staff from 2006 to 2007 at CDAC, Pune, India.
She has published several papers in various reputed
International Journals and Conferences.
Her current research interests are Machine Learning,
Data Mining and Big Data Analytics in Healthcare.
She is currently pursuing her PhD in Computer Science and Engineering from VTU, Belagavi, India.
Preface
This book presents an intuitive approach to the concepts of Python Programming for
students. It is appropriate for courses generally known as “Introduction to Python
Programming.” We have tried to write a book that assists students in discovering the
power of Python programming. We have taken into account the reality that students taking
“Introduction to Python Programming” course are likely to come from a variety of disciplines.
In addition to Computer Science majors, there tend to be students from other
majors like other engineering streams, physics, chemistry, biology, environmental science,
geography, economics, psychology and business.
This book differs from traditional texts not only in its philosophy but also in its overall
focus, level of activities, development of topics, and attention to programming details. The
emphasis is on understanding Python programming concepts. Reading a programming
book is different from reading a newspaper or a novel. Don’t be discouraged if you have
to read a passage more than once in order to understand it. We recommend that you keep
this book for reference purposes after you finish working through the course. Because
you will likely forget some of the specific details of Python programming language, the
book will serve as a useful reminder. Students will appreciate the many programming
examples within the text. Programs are carefully selected to bring the theoretical concepts
to fruition. Our aim is to get the reader to productivity as quickly as possible without sacrificing
the overall flow quality.
In fact, if you are a novice programmer, with some dedication and hard work you should
be able to learn Python Programming as your first programming language. As we introduce
each new feature of the language, we usually provide a complete program as an
example to illustrate the feature. Just as a picture is worth a thousand words, so is a properly
chosen programming example. We present the material in a way to encourage student
thinking and we show students how to generalize key concepts once they are introduced,
which can be used to solve real-world problems. Programming questions that accompany
the end of each chapter are based on worked examples which help students to gain solid
knowledge of the basics and assess their own level of understanding before moving on.
Students are highly encouraged to solve these programs to gain a solid hold on Python
programming language.
This book takes you through step by step process of learning the Python programming
language. Each line of the code is marked with numbers and is explained in detail. In this
book all the names of variables, strings, lists, dictionaries, tuples, functions, methods and
classes consist of several natural words and in the explanation part they are written in
italics to indicate the readers that they are part of programming code and to distinguish
them from normal words. This programming style of using readable natural names makes
the reading of code lot easier and prevents programming errors. Learning outcome component
is mentioned at the beginning of each chapter that calls the attention of the readers
to important items in the chapter. A summary of important concepts appears at the end
of each chapter.
We hope that students using the book will come away with an appreciation of the beauty,
power, and tremendous utility of Python programming language and that they will have
fun along the way.
Who Should Read This Book?
This book is for beginners to Python programming who are interested in learning the
basics of Python programming language.
Availability of Source Code
As you work through the programming examples given in the book, you can either type
the programs manually or download the programs from https://github.com/gowrishankarnath/Introduction_To_Python_Programming.
You are strongly encouraged to run
each of these programs and compare the results obtained in your system to those shown
in the text. By doing so, you will learn the language and its syntax.
Software Requirements
All the code in the book works with Python 3.6 version or above. Since f-strings are used
throughout the book which was introduced in Python 3.6 version, you need to have
Python interpreter 3.6 or above for the code to work as it is. Install the latest version of
64-bit Anaconda Distribution with support for Python 3.6 or above. If you want to make
the code work with Python 3.1 to 3.5 versions, use str.format() statements within the print()
function instead of f-strings. The rest of the statements within the code remains the same
and the code should work fine. It is highly recommended that you use the latest version of
PyCharm Community Edition to execute the programs. You will gain valuable insights in
to Python programming language by using this IDE, no matter whether you are a beginner
or an experienced professional.
Table of Contents
Preface ........................................................................................................................................... xiii
Acknowledgment ....................................................................................................................... xvii
Authors ......................................................................................................................................... xix
1. Introduction .............................................................................................................................1
1.1 What Is a Program? ....................................................................................................1
1.2 Programming Languages ..........................................................................................2
1.2.1 Machine Language ......................................................................................2
1.2.2 Assembly Language ....................................................................................3
1.2.3 High-Level Language .................................................................................3
1.3 Software Development ...............................................................................................5
1.4 History of Python Programming Language ...........................................................7
1.5 Thrust Areas of Python..............................................................................................8
1.5.1 Academia ......................................................................................................9
1.5.2 Scientific Tools ........................................................................................... 10
1.5.3 Machine Learning ..................................................................................... 10
1.5.4 Natural Language Processing ................................................................. 10
1.5.5 Data Analysis ............................................................................................. 10
1.5.6 Statistics ...................................................................................................... 11
1.5.7 Hypertext Transfer Protocol (HTTP) Library ........................................ 11
1.5.8 Database Connectors/ORM/NoSQL Connectors ................................ 11
1.5.9 Web Frameworks ....................................................................................... 11
1.5.10 Cloud Computing...................................................................................... 11
1.5.11 Python Distributions ................................................................................ 12
1.5.12 IDE Available ............................................................................................. 12
1.5.13 Community ................................................................................................ 12
1.5.14 Python Stack in Industry ......................................................................... 12
1.6 Installing Anaconda Python Distribution ............................................................ 13
1.7 Installing PyCharm IDE to Set Up a Python Development Environment ....... 16
1.8 Creating and Running Your First Python Project ................................................ 19
1.9 Installing and Using Jupyter Notebook.................................................................23
1.9.1 Starting Jupyter Notebook ....................................................................... 24
1.10 Open Source Software .............................................................................................27
1.10.1 Why Do People Prefer Using Open Source Software? ........................28
1.10.2 Doesn’t “Open Source” Just Mean Something Is Free of Charge? .....29
1.10.3 Open Source Licenses ...............................................................................29
1.11 Summary .................................................................................................................... 32
Multiple Choice Questions ................................................................................................... 32
Review Questions ..................................................................................................................34
2. Parts of Python Programming Language ........................................................................35
2.1 Identifiers ...................................................................................................................35
2.2 Keywords ...................................................................................................................36
2.3 Statements and Expressions ....................................................................................36
2.4 Variables ..................................................................................................................... 37
2.4.1 Legal Variable Names ............................................................................... 37
2.4.2 Assigning Values to Variables ................................................................. 37
2.5 Operators ....................................................................................................................38
2.5.1 Arithmetic Operators ................................................................................ 39
2.5.2 Assignment Operators ..............................................................................40
2.5.3 Comparison Operators .............................................................................42
2.5.4 Logical Operators ......................................................................................43
2.5.5 Bitwise Operators ......................................................................................44
2.6 Precedence and Associativity .................................................................................47
2.7 Data Types ..................................................................................................................48
2.7.1 Numbers .....................................................................................................48
2.7.2 Boolean .......................................................................................................48
2.7.3 Strings .........................................................................................................48
2.7.4 None ............................................................................................................49
2.8 Indentation .................................................................................................................49
2.9 Comments ..................................................................................................................50
2.9.1 Single Line Comment ...............................................................................50
2.9.2 Multiline Comments .................................................................................50
2.10 Reading Input ............................................................................................................50
2.11 Print Output ............................................................................................................... 51
2.11.1 str.format() Method .................................................................................... 51
2.11.2 f-strings .......................................................................................................53
2.12 Type Conversions ......................................................................................................54
2.12.1 The int() Function ......................................................................................54
2.12.2 The float() Function....................................................................................55
2.12.3 The str() Function ......................................................................................55
2.12.4 The chr() Function .....................................................................................56
2.12.5 The complex() Function .............................................................................56
2.12.6 The ord() Function ..................................................................................... 57
2.12.7 The hex() Function ..................................................................................... 57
2.12.8 The oct() Function ...................................................................................... 57
2.13 The type() Function and Is Operator ......................................................................58
2.14 Dynamic and Strongly Typed Language ..............................................................58
2.15 Summary .................................................................................................................... 59
Multiple Choice Questions ...................................................................................................60
Review Questions ..................................................................................................................65
3. Control Flow Statements ..................................................................................................... 67
3.1 The if Decision Control Flow Statement ................................................................68
3.2 The if…else Decision Control Flow Statement ....................................................... 69
3.3 The if…elif…else Decision Control Statement ........................................................71
3.4 Nested if Statement ...................................................................................................73
3.5 The while Loop ........................................................................................................... 74
3.6 The for Loop ...............................................................................................................79
3.7 The continue and break Statements .......................................................................... 81
3.8 Catching Exceptions Using try and except Statement ..........................................84
3.8.1 Syntax Errors ..............................................................................................84
3.8.2 Exceptions ..................................................................................................84
3.8.3 Exception Handling Using try…except…finally .....................................85
3.9 Summary ....................................................................................................................89
Multiple Choice Questions ...................................................................................................90
Review Questions ..................................................................................................................93
4. Functions ................................................................................................................................95
4.1 Built-In Functions .....................................................................................................95
4.2 Commonly Used Modules .......................................................................................97
4.3 Function Definition and Calling the Function .....................................................99
4.4 The return Statement and void Function .............................................................. 103
4.5 Scope and Lifetime of Variables ........................................................................... 106
4.6 Default Parameters ................................................................................................. 108
4.7 Keyword Arguments .............................................................................................. 109
4.8 *args and **kwargs .................................................................................................. 110
4.9 Command Line Arguments .................................................................................. 112
4.10 Summary .................................................................................................................. 113
Multiple Choice Questions ................................................................................................. 113
Review Questions ................................................................................................................ 117
5. Strings ................................................................................................................................... 119
5.1 Creating and Storing Strings ................................................................................. 119
5.1.1 The str() Function .................................................................................... 120
5.2 Basic String Operations .......................................................................................... 120
5.2.1 String Comparison ..................................................................................122
5.2.2 Built-In Functions Used on Strings ......................................................122
5.3 Accessing Characters in String by Index Number ............................................. 123
5.4 String Slicing and Joining ...................................................................................... 124
5.4.1 Specifying Steps in Slice Operation ...................................................... 126
5.4.2 Joining Strings Using join() Method ..................................................... 127
5.4.3 Split Strings Using split() Method ......................................................... 127
5.4.4 Strings Are Immutable ........................................................................... 128
5.4.5 String Traversing ..................................................................................... 128
5.5 String Methods ........................................................................................................ 131
5.6 Formatting Strings .................................................................................................. 138
5.6.1 Format Specifiers ..................................................................................... 140
5.6.2 Escape Sequences .................................................................................... 141
5.6.3 Raw Strings .............................................................................................. 142
5.6.4 Unicodes ................................................................................................... 142
5.7 Summary .................................................................................................................. 143
Multiple Choice Questions ................................................................................................. 143
Review Questions ................................................................................................................ 146
6. Lists........................................................................................................................................ 149
6.1 Creating Lists ........................................................................................................... 149
6.2 Basic List Operations .............................................................................................. 151
6.2.1 The list() Function .................................................................................... 151
6.3 Indexing and Slicing in Lists ................................................................................. 152
6.3.1 Modifying Items in Lists ........................................................................ 153
6.4 Built-In Functions Used on Lists .......................................................................... 155
6.5 List Methods ............................................................................................................ 156
6.5.1 Populating Lists with Items ................................................................... 158
6.5.2 Traversing of Lists ................................................................................... 159
6.5.3 Nested Lists .............................................................................................. 167
6.6 The del Statement .................................................................................................... 169
6.7 Summary .................................................................................................................. 170
Multiple-Choice Questions ................................................................................................. 170
Review Questions ................................................................................................................ 173
7. Dictionaries .......................................................................................................................... 175
7.1 Creating Dictionary ................................................................................................ 175
7.2 Accessing and Modifying key:value Pairs in Dictionaries ................................. 178
7.2.1 The dict() Function ................................................................................... 179
7.3 Built-In Functions Used on Dictionaries ............................................................. 179
7.4 Dictionary Methods ................................................................................................ 181
7.4.1 Populating Dictionaries with key:value Pairs ....................................... 183
7.4.2 Traversing of Dictionary ........................................................................ 185
7.5 The del Statement .................................................................................................... 193
7.6 Summary .................................................................................................................. 193
Multiple Choice Questions ................................................................................................. 193
Review Questions ................................................................................................................ 198
8. Tuples and Sets.................................................................................................................... 201
8.1 Creating Tuples ....................................................................................................... 201
8.2 Basic Tuple Operations ...........................................................................................203
8.2.1 The tuple() Function ................................................................................204
8.3 Indexing and Slicing in Tuples .............................................................................205
8.4 Built-In Functions Used on Tuples ....................................................................... 207
8.5 Relation between Tuples and Lists .......................................................................208
8.6 Relation between Tuples and Dictionaries ..........................................................209
8.7 Tuple Methods ......................................................................................................... 210
8.7.1 Tuple Packing and Unpacking .............................................................. 211
8.7.2 Traversing of Tuples ................................................................................ 211
8.7.3 Populating Tuples with Items ................................................................ 212
8.8 Using zip() Function ................................................................................................ 216
8.9 Sets ............................................................................................................................ 216
8.10 Set Methods ............................................................................................................. 218
8.10.1 Traversing of Sets .................................................................................... 219
8.11 Frozenset .................................................................................................................. 221
8.12 Summary ..................................................................................................................222
Multiple Choice Questions .................................................................................................222
Review Questions ................................................................................................................227
9. Files ........................................................................................................................................229
9.1 Types of Files ...........................................................................................................230
9.1.1 File Paths ................................................................................................... 231
9.1.2 Fully Qualified Path and Relative Path ................................................ 232
9.2 Creating and Reading Text Data ...........................................................................233
9.2.1 Creating and Opening Text Files ..........................................................233
9.2.2 File close() Method ...................................................................................235
9.2.3 Use of with Statements to Open and Close Files ................................. 237
9.2.4 File Object Attributes .............................................................................. 239
9.3 File Methods to Read and Write Data .................................................................. 239
9.4 Reading and Writing Binary Files ........................................................................ 247
9.5 The Pickle Module .................................................................................................. 249
9.6 Reading and Writing CSV Files ............................................................................ 251
9.7 Python os and os.path Modules ........................................................................... 257
9.8 Summary .................................................................................................................. 261
Multiple Choice Questions ................................................................................................. 262
Review Questions ................................................................................................................265
10. Regular Expression Operations ....................................................................................... 267
10.1 Using Special Characters ....................................................................................... 267
10.1.1 Using r Prefix for Regular Expressions ................................................ 272
10.1.2 Using Parentheses in Regular Expressions ......................................... 272
10.2 Regular Expression Methods ................................................................................ 273
10.2.1 Compiling Regular Expressions Using compile() Method of
re Module .................................................................................................. 273
10.2.2 Match Objects .......................................................................................... 274
10.3 Named Groups in Python Regular Expressions ................................................ 282
10.4 Regular Expression with glob Module ................................................................. 282
10.5 Summary ..................................................................................................................284
Multiple Choice Questions .................................................................................................284
Review Questions ................................................................................................................ 287
11. Object-Oriented Programming ....................................................................................... 289
11.1 Classes and Objects ................................................................................................ 289
11.2 Creating Classes in Python ................................................................................... 291
11.3 Creating Objects in Python ................................................................................... 293
11.4 The Constructor Method ....................................................................................... 294
11.5 Classes with Multiple Objects ............................................................................... 297
11.5.1 Using Objects as Arguments ................................................................. 301
11.5.2 Objects as Return Values ........................................................................303
11.6 Class Attributes versus Data Attributes ..............................................................306
11.7 Encapsulation ..........................................................................................................307
11.7.1 Using Private Instance Variables and Methods ..................................309
11.8 Inheritance ............................................................................................................... 311
11.8.1 Accessing the Inherited Variables and Methods ................................ 312
11.8.2 Using super() Function and Overriding Base Class Methods ........... 314
11.8.3 Multiple Inheritances .............................................................................. 317
11.8.4 Method Resolution Order (MRO) ......................................................... 320
11.9 The Polymorphism ................................................................................................. 328
11.9.1 Operator Overloading and Magic Methods ........................................ 331
11.10 Summary ..................................................................................................................335
Multiple Choice Questions .................................................................................................336
Review Questions ................................................................................................................338
12. Introduction to Data Science ............................................................................................ 341
12.1 Functional Programming ...................................................................................... 341
12.1.1 Lambda ..................................................................................................... 341
12.1.2 Iterators .....................................................................................................342
12.1.3 Generators ................................................................................................343
12.1.4 List Comprehensions ..............................................................................344
12.2 JSON and XML in Python .....................................................................................346
12.2.1 Using JSON with Python .......................................................................347
12.2.2 Using Requests Module ..........................................................................353
12.2.3 Using XML with Python ........................................................................355
12.2.4 JSON versus XML ................................................................................... 359
12.3 NumPy with Python .............................................................................................. 359
12.3.1 NumPy Arrays Creation Using array() Function ................................360
12.3.2 Array Attributes ...................................................................................... 361
12.3.3 NumPy Arrays Creation with Initial Placeholder Content ............... 362
12.3.4 Integer Indexing, Array Indexing, Boolean Array
Indexing, Slicing and Iterating in Arrays ............................................364
12.3.5 Basic Arithmetic Operations on NumPy Arrays ................................ 367
12.3.6 Mathematical Functions in NumPy .....................................................368
12.3.7 Changing the Shape of an Array .......................................................... 369
12.3.8 Stacking and Splitting of Arrays ........................................................... 370
12.3.9 Broadcasting in Arrays ........................................................................... 371
12.4 Pandas ....................................................................................................................... 374
12.4.1 Pandas Series ........................................................................................... 375
12.4.2 Pandas DataFrame ..................................................................................380
12.5 Altair ......................................................................................................................... 398
12.6 Summary ..................................................................................................................409
Multiple Choice Questions ................................................................................................. 410
Review Questions ................................................................................................................ 413
Appendix-A: Debugging Python Code ................................................................................. 415
Bibliography ................................................................................................................................425
Solutions ......................................................................................................................................427
Index ............................................................................................................................................. 437
Support for the Book
We will try to address your questions within an appropriate timeframe. Understand that
we have a day job just like you and we may not be able to respond immediately. Rest
assured we will respond to genuine questions and we do not like to keep our readers in
the dark whatsoever.
This Book Is Not for You
If you are an advanced Python Programmer or if you are a Professional who is well versed
in another programming language, then this book is not for you. The programming examples
and the explanations are kept simple. Please understand that with your advanced
knowledge you might feel that we are apparently dealing with basic stuff but what might
be a basic material to you is not the same to someone who is learning to write code for the first time.
Errata
We hope to improve this book continually. If you have any suggestions for improving this
text or if an error should be found, the authors would be grateful if notification were sent
by e-mail to gowrishankarnath@acm.org. To ensure your messages do not end up in our junk
mail folder, please include subject as “Introduction to Python Programming.”