Buy Ebook Introduction To Computing Using Python: An Application Development Focus, 2nd Edition - Ebook PDF Version Cheap Price
Buy Ebook Introduction To Computing Using Python: An Application Development Focus, 2nd Edition - Ebook PDF Version Cheap Price
com
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookmass.com/product/society-in-focus-an-introduction-
to-sociology-9th-edition-ebook-pdf-version/
https://ebookmass.com/product/introduction-to-computing-and-
problem-solving-using-python-1st-edition-e-balaguruswamy/
https://ebookmass.com/product/introduction-to-8086-assembly-
language-and-computer-architecture-ebook-pdf-version/
https://ebookmass.com/product/women-music-culture-an-
introduction-2nd-edition-ebook-pdf-version/
Geosystems: An Introduction to Physical Geography –
Ebook PDF Version
https://ebookmass.com/product/geosystems-an-introduction-to-
physical-geography-ebook-pdf-version/
https://ebookmass.com/product/introduction-to-human-development-
and-family-studies-1st-edition-ebook-pdf-version/
https://ebookmass.com/product/the-cultural-landscape-an-
introduction-to-human-geography-11th-edition-ebook-pdf-version-
ebook-pdf-version/
https://ebookmass.com/product/humanity-an-introduction-to-
cultural-anthropology-11th-edition-ebook-pdf-version/
https://ebookmass.com/product/microbiology-an-introduction-12th-
edition-ebook-pdf-version/
Contents
Preface xix
Online Textbook Supplements . . . . . . . . . . . . . . xx
For Students: How to Read This Book . . . . . . . . . . . xx
Overview of the Book . . . . . . . . . . . . . . . . . . xxi
What Is New in This Edition? . . . . . . . . . . . . . . . xxiv
For Instructors: How to Use This Book . . . . . . . . . . . xxv
1
Introduction to Computer Science 1
1.1 Computer Science . . . . . . . . . . . . . . . . . . . 2
What Do Computing Professionals Do? . . . . . . . . . . 2
Models, Algorithms, and Programs. . . . . . . . . . . . 3
Tools of the Trade . . . . . . . . . . . . . . . . . 3
What Is Computer Science? . . . . . . . . . . . . . . 4
1.2 Computer Systems . . . . . . . . . . . . . . . . . . . 4
Computer Hardware . . . . . . . . . . . . . . . . 4
Operating Systems . . . . . . . . . . . . . . . . . 5
Networks and Network Protocols . . . . . . . . . . . . 6
Programming Languages . . . . . . . . . . . . . . . 7
Software Libraries . . . . . . . . . . . . . . . . . 7
1.3 Python Programming Language . . . . . . . . . . . . . 8
Short History of Python . . . . . . . . . . . . . . . 8
Setting Up the Python Development Environment . . . . . . . 8
1.4 Computational Thinking . . . . . . . . . . . . . . . . . 9
A Sample Problem . . . . . . . . . . . . . . . . . 9
Abstraction and Modeling . . . . . . . . . . . . . . . 10
Algorithm . . . . . . . . . . . . . . . . . . . . 10
Data Types . . . . . . . . . . . . . . . . . . . 11
Assignments and Execution Control Structures . . . . . . . 12
Chapter Summary . . . . . . . . . . . . . . . . . . . . . 13
vii
viii Contents
2
Python Data Types 15
2.1 Expressions, Variables, and Assignments . . . . . . . . . . 16
Algebraic Expressions and Functions . . . . . . . . . . . 16
Boolean Expressions and Operators . . . . . . . . . . . 18
Variables and Assignments . . . . . . . . . . . . . . 20
Variable Names . . . . . . . . . . . . . . . . . . 22
2.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . 23
String Operators . . . . . . . . . . . . . . . . . . 23
Indexing Operator . . . . . . . . . . . . . . . . . 25
2.3 Lists and Tuples . . . . . . . . . . . . . . . . . . . . 27
List Operators . . . . . . . . . . . . . . . . . . 27
Lists Are Mutable, Strings Are Not . . . . . . . . . . . . 29
Tuples, or “Immutable Lists” . . . . . . . . . . . . . . 29
List and Tuple Methods . . . . . . . . . . . . . . . 31
2.4 Objects and Classes . . . . . . . . . . . . . . . . . . 33
Object Type . . . . . . . . . . . . . . . . . . . 33
Valid Values for Number Types . . . . . . . . . . . . . 35
Operators for Number Types . . . . . . . . . . . . . . 36
Creating Objects . . . . . . . . . . . . . . . . . . 37
Implicit Type Conversions . . . . . . . . . . . . . . . 38
Explicit Type Conversions. . . . . . . . . . . . . . . 39
Class Methods and Object-Oriented Programming . . . . . . 40
2.5 Python Standard Library. . . . . . . . . . . . . . . . . 41
Module math . . . . . . . . . . . . . . . . . . . 41
Module fractions . . . . . . . . . . . . . . . . . 42
Case Study: Turtle Graphics . . . . . . . . . . . . . . . . . 43
Chapter Summary . . . . . . . . . . . . . . . . . . . . . 43
Solutions to Practice Problems . . . . . . . . . . . . . . . . 44
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 45
3
Imperative Programming 51
3.1 Python Programs . . . . . . . . . . . . . . . . . . . 52
Our First Python Program. . . . . . . . . . . . . . . 52
Python Modules . . . . . . . . . . . . . . . . . . 54
Built-In Function print() . . . . . . . . . . . . . . 54
Interactive Input with input() . . . . . . . . . . . . . 55
Function eval() . . . . . . . . . . . . . . . . . . 56
Contents ix
4
Text Data, Files, and Exceptions 91
4.1 Strings, Revisited . . . . . . . . . . . . . . . . . . . 92
String Representations. . . . . . . . . . . . . . . . 92
The Indexing Operator, Revisited . . . . . . . . . . . . 94
String Methods . . . . . . . . . . . . . . . . . . 95
4.2 Formatted Output . . . . . . . . . . . . . . . . . . . 98
Function print() . . . . . . . . . . . . . . . . . 98
String Method format() . . . . . . . . . . . . . . . 100
Lining Up Data in Columns . . . . . . . . . . . . . . 102
Getting and Formatting the Date and Time . . . . . . . . . 105
4.3 Files. . . . . . . . . . . . . . . . . . . . . . . . . 107
File System . . . . . . . . . . . . . . . . . . . 107
Opening and Closing a File . . . . . . . . . . . . . . 109
Patterns for Reading a Text File . . . . . . . . . . . . . 112
Writing to a Text File . . . . . . . . . . . . . . . . 115
x Contents
5
Execution Control Structures 127
6
Containers and Randomness 165
6.1 Dictionaries . . . . . . . . . . . . . . . . . . . . . . 166
User-Defined Indexes as Motivation for Dictionaries . . . . . . 166
Dictionary Class Properties . . . . . . . . . . . . . . 167
Dictionary Operators . . . . . . . . . . . . . . . . 169
Dictionary Methods . . . . . . . . . . . . . . . . . 170
A Dictionary as a Substitute for the Multiway if Statement. . . . 173
Dictionary as a Collection of Counters . . . . . . . . . . 173
tuple Objects Can Be Dictionary Keys . . . . . . . . . . 176
6.2 Sets . . . . . . . . . . . . . . . . . . . . . . . . . 177
Using the set Constructor to Remove Duplicates . . . . . . . 178
set Operators . . . . . . . . . . . . . . . . . . 179
set Methods . . . . . . . . . . . . . . . . . . . 180
6.3 Character Encodings and Strings . . . . . . . . . . . . . 181
Character Encodings . . . . . . . . . . . . . . . . 181
ASCII . . . . . . . . . . . . . . . . . . . . . 182
Unicode . . . . . . . . . . . . . . . . . . . . 183
UTF-8 Encoding for Unicode Characters . . . . . . . . . . 185
6.4 Module random . . . . . . . . . . . . . . . . . . . . 186
Choosing a Random Integer . . . . . . . . . . . . . . 187
Choosing a Random “Real” . . . . . . . . . . . . . . 188
Shuffling, Choosing, and Sampling at Random . . . . . . . . 189
Case Study: Games of Chance. . . . . . . . . . . . . . . . 190
Chapter Summary . . . . . . . . . . . . . . . . . . . . . 190
Solutions to Practice Problems . . . . . . . . . . . . . . . . 190
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 194
Problems . . . . . . . . . . . . . . . . . . . . . . . . 195
7
Namespaces 203
7.1 Encapsulation in Functions. . . . . . . . . . . . . . . . 204
Code Reuse . . . . . . . . . . . . . . . . . . . 204
Modularity (or Procedural Decomposition) . . . . . . . . . 205
Encapsulation (or Information Hiding) . . . . . . . . . . . 205
Local Variables . . . . . . . . . . . . . . . . . . 205
Namespaces Associated with Function Calls . . . . . . . . 206
Namespaces and the Program Stack . . . . . . . . . . . 207
xii Contents
8
Object-Oriented Programming 239
8.1 Defining a New Python Class. . . . . . . . . . . . . . . 240
Methods of Class Point . . . . . . . . . . . . . . . 240
A Class and Its Namespace . . . . . . . . . . . . . . 241
Every Object Has an Associated Namespace . . . . . . . . 242
Implementation of Class Point . . . . . . . . . . . . . 242
Instance Variables . . . . . . . . . . . . . . . . . 243
Instances Inherit Class Attributes . . . . . . . . . . . . 244
Class Definition, More Generally . . . . . . . . . . . . 245
Documenting a Class . . . . . . . . . . . . . . . . 246
Class Animal . . . . . . . . . . . . . . . . . . . 247
8.2 Examples of User-Defined Classes . . . . . . . . . . . . 248
Overloaded Constructor Operator . . . . . . . . . . . . 248
Default Constructor . . . . . . . . . . . . . . . . . 249
Playing Card Class . . . . . . . . . . . . . . . . . 250
Contents xiii
9
Graphical User Interfaces 291
9.1 Basics of tkinter GUI Development . . . . . . . . . . . 292
Widget Tk: The GUI Window. . . . . . . . . . . . . . 292
Widget Label for Displaying Text . . . . . . . . . . . . 292
Displaying Images . . . . . . . . . . . . . . . . . 294
Packing Widgets . . . . . . . . . . . . . . . . . . 295
Arranging Widgets in a Grid . . . . . . . . . . . . . . 297
9.2 Event-Based tkinter Widgets . . . . . . . . . . . . . . 299
Button Widget and Event Handlers . . . . . . . . . . . 299
Events, Event Handlers, and mainloop() . . . . . . . . . 301
The Entry Widget . . . . . . . . . . . . . . . . . 302
Text Widget and Binding Events . . . . . . . . . . . . 305
Event Patterns and the tkinter Class Event . . . . . . . . 306
9.3 Designing GUIs . . . . . . . . . . . . . . . . . . . . 308
Widget Canvas . . . . . . . . . . . . . . . . . . 308
Widget Frame as an Organizing Widget . . . . . . . . . . 311
xiv Contents
10
Recursion 329
10.1 Introduction to Recursion . . . . . . . . . . . . . . . . 330
Functions that Call Themselves . . . . . . . . . . . . . 330
Stopping Condition . . . . . . . . . . . . . . . . . 331
Properties of Recursive Functions . . . . . . . . . . . . 332
Recursive Thinking . . . . . . . . . . . . . . . . . 332
Recursive Function Calls and the Program Stack . . . . . . . 334
10.2 Examples of Recursion. . . . . . . . . . . . . . . . . 336
Recursive Number Sequence Pattern . . . . . . . . . . . 336
Fractals . . . . . . . . . . . . . . . . . . . . . 338
Virus Scanner . . . . . . . . . . . . . . . . . . 342
Linear recursion . . . . . . . . . . . . . . . . . . 345
10.3 Run Time Analysis . . . . . . . . . . . . . . . . . . 347
The Exponent Function . . . . . . . . . . . . . . . 347
Counting Operations . . . . . . . . . . . . . . . . 349
Fibonacci Sequence . . . . . . . . . . . . . . . . 349
Experimental Analysis of Run Time . . . . . . . . . . . 351
10.4 Searching . . . . . . . . . . . . . . . . . . . . . . 354
Linear Search. . . . . . . . . . . . . . . . . . . 354
Binary Search . . . . . . . . . . . . . . . . . . 354
Linear versus Binary Search . . . . . . . . . . . . . . 356
Uniqueness Testing . . . . . . . . . . . . . . . . . 357
Selecting the kth Largest (Smallest) Item. . . . . . . . . . 358
Computing the Most Frequently Occurring Item . . . . . . . 359
Case Study: Tower of Hanoi . . . . . . . . . . . . . . . . . 359
Chapter Summary . . . . . . . . . . . . . . . . . . . . . 360
Solutions to Practice Problems . . . . . . . . . . . . . . . . 360
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 362
Problems . . . . . . . . . . . . . . . . . . . . . . . . 363
Contents xv
11
The Web and Search 371
11.1 The World Wide Web . . . . . . . . . . . . . . . . . 372
Web Servers and Web Clients . . . . . . . . . . . . . 372
“Plumbing” of the WWW . . . . . . . . . . . . . . . 373
Naming Scheme: Uniform Resource Locator . . . . . . . . 373
Protocol: HyperText Transfer Protocol . . . . . . . . . . . 374
HyperText Markup Language . . . . . . . . . . . . . 375
HTML Elements . . . . . . . . . . . . . . . . . . 376
Tree Structure of an HTML Document. . . . . . . . . . . 377
Anchor HTML Element and Absolute Links . . . . . . . . . 377
Relative Links. . . . . . . . . . . . . . . . . . . 378
11.2 Python WWW API . . . . . . . . . . . . . . . . . . 379
Module urllib.request . . . . . . . . . . . . . . 379
Module html.parser . . . . . . . . . . . . . . . . 381
Overriding the HTMLParser Handlers . . . . . . . . . . . 383
Module urllib.parse . . . . . . . . . . . . . . . 384
Parser That Collects HTTP Hyperlinks . . . . . . . . . . 385
11.3 String Pattern Matching . . . . . . . . . . . . . . . . 387
Regular Expressions . . . . . . . . . . . . . . . . 387
Python Standard Library Module re . . . . . . . . . . . 390
Case Study: Web Crawler . . . . . . . . . . . . . . . . . . 391
Chapter Summary . . . . . . . . . . . . . . . . . . . . . 392
Solutions to Practice Problems . . . . . . . . . . . . . . . . 392
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 394
Problems . . . . . . . . . . . . . . . . . . . . . . . . 395
12
Databases and Data Processing 399
12.1 Databases and SQL . . . . . . . . . . . . . . . . . . 400
Database Tables. . . . . . . . . . . . . . . . . . 400
Structured Query Language . . . . . . . . . . . . . . 402
Statement SELECT . . . . . . . . . . . . . . . . . 402
Clause WHERE . . . . . . . . . . . . . . . . . . 404
Built-In SQL Functions . . . . . . . . . . . . . . . . 406
Clause GROUP BY . . . . . . . . . . . . . . . . . 406
Making SQL Queries Involving Multiple Tables . . . . . . . . 407
Statement CREATE TABLE . . . . . . . . . . . . . . 409
Statements INSERT and UPDATE . . . . . . . . . . . . 409
xvi Contents
Index 514
Preface
This textbook is an introduction to programming, computer application development, and
the science of computing. It is meant to be used in a college-level introductory programming
course. More than just an introduction to programming, the book is a broad introduction to
computer science concepts and to the tools used for modern computer application develop-
ment.
The computer programming language used in the book is Python, a language that has a
gentler learning curve than most. Python comes with powerful software libraries that make
complex tasks—such as developing a graphics application or finding all the links in a web
page—a breeze. In this textbook, we leverage the ease of learning Python and the ease of
using its libraries to do more computer science and to add a focus on modern application
development. The result is a textbook that is a broad introduction to the field of computing
and modern application development.
The book uses the imperative-first and procedural-first paradigm but does not shy away
from discussing objects early. User-defined classes and object-oriented programming are
covered later, when they can be motivated and students are ready. The last three chapters of
the textbook and the associated case studies use the context of web crawling, search engines,
and data mining to introduce a broad array of topics. These include foundational concepts
such as recursion, regular expressions, depth-first search, data compression, and Google’s
MapReduce framework, as well as practical tools such as GUI widgets, HTML parsers,
SQL, JSON, I/O streams, and multicore programming.
This textbook can be used in a course that introduces computer science and program-
ming to computer science majors. Its broad coverage of foundational computer science top-
ics as well as current technologies will give the student a broad understanding of the field
and a confidence to develop “real” modern applications that interact with the web and/or a
database. The textbook’s broad coverage also makes it ideal for students who need to master
programming and key computing concepts but will not take more than one or two computing
courses.
xix
xx Preface
tially related to the main discussion. You may skip those if you like. Or you may go further
and explore the topics in more depth if you get intrigued.
At some point while reading this text, you may get inspired to develop your own app,
whether a card game or an app that keeps track of a set of stock market indexes in real time.
If so, just go ahead and try it! You will learn a lot.
a discussion of exceptions and the different types of exceptions in Python. Case Study CS.4
discusses how image files (typically stored as binary files rather than text files) are read and
written and how images can be processed using Python.
Chapter 5 covers execution control structures and loop patterns in depth. Basic condi-
tional and iteration structures were introduced in Chapter 3 and then used in Chapter 4 (e.g.,
in the context of reading files). Chapter 5 starts with a discussion of multiway conditional
statements. The bulk of the chapter is spent on describing the different loop patterns: the
various ways for loops and while loops are used. Multidimensional lists are introduced as
well, in the context of the nested loop pattern. More than just covering Python loop struc-
tures, this core chapter describes the different ways that problems can be broken down.
Thus, the chapter fundamentally is about problem solving and algorithms. Case Study CS.5
looks underneath the hood of image processing and describes how classic image processing
algorithms can be implemented.
Chapter 6 completes the textbook’s coverage of Python’s built-in container data types
and their usage. The dictionary, set, and tuple data types are motivated and introduced. This
chapter also completes the coverage of strings with a discussion of character encodings and
Unicode. Finally, the concept of randomness is introduced in the context of selecting and
permuting items in containers. Case Study CS.6 makes use of concepts introduced in this
chapter to show how a blackjack application can be developed.
Chapters 4 through 6 represent the second layer in the breadth-first approach this text-
book takes. One of the main challenges students face in an introductory programming course
is mastering conditional and iteration structures and, more generally, the computational
problem-solving and algorithm development skills. The critical Chapter 5, on patterns of
applying execution control structures, appears after students have been using basic condi-
tional statements and iteration patterns for several weeks and have gotten somewhat com-
fortable with the Python language. Having gained some comfort with the language and basic
iteration, students can focus on the algorithmic issues rather than less fundamental issues,
such as properly reading input or formatting output.
Chapter 9 introduces graphical user interfaces (GUIs) and showcases the power of the
OOP approach for developing GUIs. It uses the Tk widget toolkit, which is part of the
Python Standard Library. The coverage of interactive widgets provides the opportunity to
discuss the event-driven programming paradigm. In addition to introducing GUI develop-
ment, the chapter also showcases the power of OOP to achieve modular and reusable pro-
grams. Case Study CS.9 illustrates this in the context of implementing a basic calculator
GUI.
The broad goal of Chapters 7 though 9 is to introduce students to the issues of program
complexity and code organization. They describe how namespaces are used to achieve func-
tional abstraction and data abstraction and, ultimately, encapsulated, modular, and reusable
code. Chapter 8 provides a comprehensive discussion of user-defined classes and OOP. The
full benefit of OOP, however, is best seen in context, which is what Chapter 9 is about.
Additional contexts and examples of OOP are shown in later chapters and specifically in
Sections 11.2, 12.3, and 12.4 as well as in Case Study CS.10. Chapters 7 though 9 provide
a foundation for the students’ future education in data structures and software engineering
methodologies.
be considered merely a basis for a later database course. This chapter also considers how
to leverage the multiple cores available on computers to process big data sets more quickly.
Google’s MapReduce problem-solving framework is described and used as a context for
introducing list comprehensions and the functional programming paradigm. This chapter
forms a foundation for further study of databases, programming languages, and data min-
ing. Case Study CS.12 uses this last context to discuss data interchange or how to format
and save data so that it is accessible, easily and efficiently, to any program that needs it.
Petetty talonpoikaispäällikkö.
Nyt oli kuitenkin niin, että heti retken alusta oli ollut riitaa siitä,
kuka saisi tahtonsa kuuluville ja kun hän valittuna johtajana oli
ajanut jyrkästi tahtonsa läpitse, alettiin häntä vihata ja kadehtia. Hän
kyllä olisi suonut päällikkyyden yhtä hyvin jollekulle toisellekin, jos
vain olisi löytynyt mies, joka tätä monimielistä joukkoa olisi pystynyt
hallitsemaan ja johtamaan. Sitä ei suurista eleistä ja sanoista
huolimatta näyttänyt löytyvän, ja hän kun oli ottanut retken
vastuulleen, tahtoi myöskin lujasti pitää johdon käsissään.
— Oh, onneton poikani Kustaa… sitä hän kyllä olisi, mutta eihän
hän voi… kateelliset eivät sallisi hänen kauan pysyvän vallassa…
*****
Ylitse vuotava viha leimahti Ilkan tuuheiden kulmien alta, kun hän
ojensi nyrkkinsä leiriä kohti ja sähähti.
*****
Nuijan jälkiä.
*****
Kun julistus oli saneltu loppuun, merkitsi sihteeri sen alle: Annettu
Suomen Turussa 3 pnä Lokakuuta Anno Domini 1597.
Kun julistus oli käännetty, piirsi herttua sen alle suurin kirjaimin
Carolus. Nimikirjoitus vahvistettiin vielä herttuallisella sinetillä ja siitä
toimitettiin riittävä määrä jäljennöksiä maan kaikissa kirkoissa julki
luettavaksi.
Aapelusta oppimassa.
*****
Muutamia kuukausia myöhemmin istui papin tuvassa muutamia
lähellä asuvia talonpoikia hartaasti tankkaamassa aapiskirjan
koukeroisia kirjaimia, jotka eivät tahtoneet mitenkään mahtua enää
ikämiesten harmaantuviin päihin. Kesken opetuksensa virkkoi
kirkkoherra naapurinsa miehille:
— Aa.
— See… setta.
Rauhan vieras.
Kuninkaan koulu.
— Eläköön Augustus!
— Vaimon?
— No olkoon sitten… ellet usko niin puske, mutta kyllä asia niin
vain on.
— Ja jos niin asiat ovat, niin silloinpa en astu enää jalallani koulun
kynnyksen ylitse. Lähden vaikka merille, eli jään tänne korpeen
eränkävijäksi, kuohahteli Eskil ylenmäärin suuttuneena Kaarinan
uskottomuudesta.
— Onhan se sinullakin…
— Minä hänestä vähät!
Kevätjuhla.
Vuoden 1640 talvi oli ollut kylmä ja luminen, mutta kevät tuli
aikaiseen ja Turusta suolaa ja rautaa hakevat sisämaalaiset saivat
toukokuun puolimaissa tervavenheitään maataipalien yli kiskoessaan
hikoilla niinkuin keskikesän lämpimillä. Kevät-touot olivat jo tehdyt,
nauriskasket vierretyt ja joutipa siis suolanhakumatkalla hieman
viivyttelemäänkin ja katselemaan sitä vireätä touhua, joka ilmeni
tällä kertaa niin erikoisen silmään pistävänä Turun kujilla ja kaduilla.
Kuulaassa kesäisessä ilmassa kajahteli rakennusmiesten kirveitten
iskut ja raskaita rakennustarvekuormia vedättävien hevosmiesten
huudot. Turku oli saanut rakennuskuumeen. Olihan maassa ollut
rauha taas pitemmän ajan ja kauppaporvarit olivat vaurastuneet.
Kun nyt maan ensimmäinen yliopisto avattaisiin pian Turussa, tietäisi
se kaupungille aivan uutta nousukautta ja asukaslisäystä ja niinpä
kohosikin talo toisensa perästä Aurajoen varrelle ja Anninkaisten
mäen rintaan.
*****