Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Introduction to Python Programming (Computer program language)
Nội dung xem thử
Mô tả chi tiết
Introduction to
Python Programming
Introduction to
Python Programming
Gowrishankar S.
Veena A.
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2019 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Printed on acid-free paper
International Standard Book Number-13: 978-0-8153-9437-2 (Hardback)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made
to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all
materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all
material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been
obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future
reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized
in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying,
microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.
copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400.
CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been
granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.
Library of Congress Cataloging‑in‑Publication Data
Names: S, Gowrishankar, author. | A, Veena author.
Title: Introduction to Python programming / Gowrishankar S, Veena A.
Description: Boca Raton : Taylor & Francis, a CRC title, part of the Taylor &
Francis imprint, a member of the Taylor & Francis Group, the academic
division of T&F Informa, plc, 2018. | Includes bibliographical references
and index.
Identifiers: LCCN 2018046894 | ISBN 9780815394372 (hardback : alk. paper) |
ISBN 9781351013239 (ebook)
Subjects: LCSH: Python (Computer program language)
Classification: LCC QA76.73.P98 S2325 2018 | DDC 005.13/3--dc23
LC record available at https://lccn.loc.gov/2018046894
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Dedicated to my wife Roopa K. M. and to my sister Ashwini S. Nath.
—Dr. Gowrishankar S.
I would love to dedicate this book to my parents, and my family
for their love, support and encouragement.
—Veena A.
vii
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
viii Contents
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
Contents ix
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
x Contents
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
Contents xi
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
xii Contents
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
xiii
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.
xiv Preface
Organization of Chapters
Here’s a brief rundown of what you will find in each chapter:
In Chapter 1, the advantages of using Python programming language is discussed
and the scope of Python’s reach, and all the different areas of application development in which Python plays a part is identified. This chapter also covers the downloading and installation of Anaconda distribution and PyCharm IDE. You will be
guided towards setting up your own Python development environment. You will
understand the meaning of Open Source Software and its various licenses.
In Chapter 2, you will learn the basic building blocks of Python programming language like arithmetic operators, data types, operator precedence and associativity and construct complex expressions using operators. This chapter teaches you
the syntax of Python language which you need to know before writing Python
programs.
In Chapter 3, Python statements such as if, if…else, if…elif…else are taught which are
used to transfer the control from one part of the program to another. Loops to run
one or more statements repeatedly are examined. Controlling the flow of execution is carried out through the break and continue statements. To take advantage
of the iterative capabilities in a computer, you need to know how to include loops
and conditional logic within your program. How to deal with errors in the input
is also part of this chapter.
In Chapter 4, you will determine how to create functions, pass values to and return
values from the function. In this chapter, commonly used modules and built-in
functions are identified. This chapter also includes discussion on different function features like keyword arguments and variable number of arguments. You’ll
learn how to use functions to make your code more efficient and flexible.
In Chapter 5, some of the more commonly occurring string tasks, such as concatenating strings, trimming of white spaces, splitting string as well as finding substrings within strings is covered. Various string methods to manipulate strings are
described in detail. Indexing, slicing and joining of strings are illustrated through
figures. Formatting of the string using f-strings is also outlined.
In Chapter 6, one of the main pillars of Python programming language and the builtin data type, Lists, is discussed which acts as a container to hold items of different types. Creating, slicing and indexing of lists is elaborated with high-grade
examples. Use of list methods in manipulating Python programs is demonstrated.
In Chapter 7, another built-in data type in Python programming language, Dictionary,
is covered in depth. Accessing and modifying the key:value pairs of Dictionaries
is demonstrated.
In Chapter 8, role of tuples in returning multiple values from functions and storing
heterogeneous elements of fixed sized is discussed. Mathematical operations like
union and intersection are demonstrated using sets. Different methods supported
by tuples and sets are identified.
In Chapter 9, you will deal with file-based input and output functions, including
how to load files, perform basic operations, and save the results back to disk.