Top Banner
7/29/2019 662647939 http://slidepdf.com/reader/full/662647939 1/11 Introduction to Computing Using Python  An  Application Development Focus Ljubomir Perkovic DePaul University
11

662647939

Apr 14, 2018

Download

Documents

ohmega
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 1/11

Introduction to Computing

Using Python

 An  Application Development Focus

LjubomirPerkovic

DePaul University

Page 2: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 2/11

Contents

Preface xvh

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 Hardware4

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

 Algori thm 10

Data Types 11

 Assignments and Execution Control Structures 12

Chapter Summary 13

Page 3: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 3/11

viii Contents

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 27

List Operators 27

Lists Are Mutable, Strings Are Not 29

List Methods 29

2.4 Objects and Classes 31

Object Type 32

Valid Values for Number Types 33

Operators for Number Types 34

Creating Objects 35

Implicit Type Conversions 36

Explicit Type Conversions 37

Class Methods and Object-Oriented Programming 38

2.5 Python Standard

Library39

Module math 39

Module fractions 40

2.6 Case Study: Turtle Graphics Objects 41

Chapter Summary 45

Solutions to Practice Problems 46

Exercises 48

3

Imperative Programming 53

3.1 Python Programs 54

Our First Python Program 54

Python Modules 56

Built-in Function print () 56

Interactive Input with input () 57

Function eval() 58

Page 4: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 4/11

Contents

3.2 Execution Control Structures 59

One-Way Decisions 59

Two-Way Decisions 62

Iteration Structures 64

Nesting Control Flow Structures 67

Function range ()68

3.3 User-Defined Functions 69

Our First Function 69

print () versus return 71

Function Definitions Are "Assignment" Statements 72

Comments 73

Docstrings 73

3.4 Python Variables and  Assignments 75

Mutable and Immutable Types 76

 Assignments and Mutability 77

Swapping 78

3.5 Parameter Passing 79

Immutable Parameter Passing 80

Mutable Parameter Passing 81

3.6 Case Study: Automating Turtle Graphics 82

Chapter Summary 84

Solutions to Practice Problems 85

Exercises 88

Problems 88

Text Data, Files, and Exceptions 95

4.1 Strings, Revisited 96

String Representations 96

The Indexing Operator, Revisited 98

String Methods 99

4.2 Formatted Output 102

Function print 0 102

String Method format 0 104

Lining Up Data in Columns 106

4.3 Files 109

File System 109

Opening and Closing a File 111

Patterns for Reading a Text File 114

Writing to a Text File 117

Page 5: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 5/11

x Contents

4.4 Errors and Exceptions 118

Syntax Errors 118

Built-in Exceptions 119

4.5 Case Study: Logging File Access 121

 A  Thin Wrapper Function 122

Logging File Names 122

Getting and Formatting the Date and Time 123

Final Implementation of openLogO 125

Chapter Summary 125

Solutions to Practice Problems 126

Exercises 128

Problems 130

Execution Control Structures 133

5.1 Decision Control and the if Statement 134

Three-Way (and More!) Decisions 134

Ordering of Conditions 136

5.2 for Loop and Iteration Patterns 137

Loop Pattern: Iteration Loop 137

Loop Pattern: Counter Loop 138

Loop Pattern: Accumulator Loop 140

 Accumulating Different Types 141

Loop Patterns: Nested Loop 143

5.3 More on Lists: Two-Dimensional Lists 145

Two-Dimensional Lists 146

Two-Dimensional Lists and the Nested Loop Pattern 147

5.4 while Loop 149

5.5 More Loop Patterns 151

Iteration Patterns: Sequence Loop 151

Loop Pattern: Infinite Loop 153

Loop Pattern: Loop and a Half 153

5.6 Additional Iteration Control Statements 155

break Statement 155

continue Statement 156

pass Statement 157

Chapter Summary 157

Solutions to Practice Problems 158

Exercises 161

Problems 163

Page 6: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 6/11

Contents

6

Containers and Randomness 171

6.1 Dictionaries 172

User-Defined Indexes as Motivation for Dictionaries 172

Dictionary Class Properties 173

Dictionary Operators 175

Dictionary Methods 176

 A  Dictionary as a Substitute for Multiway Condition 178

Dictionary as a Collection of Counters 179

6.2 Other Built-in Container Types 182

Class tuple 182

tuple Objects Can Be Dictionary Keys 183

Dictionary Method items (), Revisited 184

Class set 185

Using the set Constructor to Remove Duplicates 186

set Operators 187

set Methods 188

6.3 Character Encodings and Strings 189

Character Encodings 189

 ASCII 190

Unicode 191

UTF-8 Encoding for Unicode Characters 193

6.4 Module random 194

Choosing a Random Integer 195

Choosing a Random "Real" 196

Shuffling, Choosing, and Sampling at Random 197

6.5 Case Study: Games of Chance 198

Blackjack 198

Creating and Shuffling the Deck of Cards 199

Dealing a Card 200

Computing the Value of a Hand 200

Comparing the Player's and the House's Hands 201

MainBlackjack

Function 202

Chapter Summary 203

Solutions to Practice Problems 203

Exercises 206

Problems 208

Page 7: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 7/11

xii Contents

7

Namespaces 215

7.1 Encapsulation in Functions 216

Code Reuse 216

Modularity (or Procedural Decomposition) 217

Encapsulation (or Information Hiding) 217

Local Variables 217

Namespaces Associated with Function Calls 218

Namespaces and the Program Stack 219

7.2 Global versus Local Namespaces 223

Global Variables 223

Variables with Local Scope 224

Variables with Global Scope224

Changing Global Variables Inside a Function 226

7.3 Exceptional Control Flow 227

Exceptions and Exceptional Control Flow 227

Catching and Handling Exceptions 228

The Default Exception Handler 230

Catching Exceptions of a Given Type 230

Multiple Exception Handlers 231

Controlling the Exceptional Control Flow 232

7.4 Modules as Namespaces 235

Module Attributes 235

What Happens When Importing a Module 236

Module Search Path 236

Top-Level Module 238

Different Ways to Import Module Attributes 240

7.5 Classes as Namespaces 242

 A  Class Is a Namespace 242

Class Methods Are Functions Defined in the Class Namespace . .

243

Chapter Summary 244

Solutions to Practice Problems 244

Exercises 245

Problems 248

Page 8: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 8/11

Contents

8

Object-Oriented Programming 251

8.1 Defining a New Python Class 252

Methods of Class Point 252

 A  Class and Its Namespace 253

Every Object Has an Associated Namespace 254

Implementation of Class Point 254

Instance Variables 255

Instances Inherit Class Attributes 256

Class Definition, More Generally 257

Documenting a Class 258

Class Animal 259

8.2 Examples of User-Defined Classes 260

Overloaded Constructor Operator 260

Default Constructor 261

Playing Card Class 262

8.3 Designing New Container Classes 263

Designing a Class Representing a Deck of Playing Cards....

263

Implementing the Deck (of Cards) Class 264

Container Class Queue 266

Implementing a Queue Class 267

8.4 Overloaded Operators 268

Operators Are Class Methods 269

Making the Class Point User Friendly 270

Contract between the Constructor and the repr() Operator. .

.

272

Making the Queue Class User Friendly 274

8.5 Inheritance 276

Inheriting Attributes of a Class 276

Class Definition, in General 279

Overriding Superclass Methods 279

Extending Superclass Methods 282

Implementing a Queue Class by Inheriting from list 283

8.6 User-Defined Exceptions 284

Raising an Exception 285

User-Defined Exception Classes 286

Improving the Encapsulation of Class Queue 286

8.7 Case Study: Indexing and Iterators 287

Overloading the Indexing Operators 287

Iterators and OOP Design Patterns 289

Chapter Summary 292

Solutions to Practice Problems 293

Exercises 296

Problems 299

Page 9: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 9/11

xiv Contents

9

Graphical User Interfaces 309

9.1 Basics of tkinter GUI Development 310

Widget Tk: The GUI Window 310

Widget Label for Displaying Text 310

Displaying Images 312

Packing Widgets 313

 Arran ging Widgets in a Grid 315

9.2 Event-Based tkinter Widgets 317

Button Widget and Event Handlers 317

Events, Event Handlers, and mainloopO 319

The Entry Widget 320

Text Widget and Binding Events 323

Event Patternsand the tkinter Class Event 324

9.3 Designing GUIs 326

Widget Canvas 326

Widget Frame as an Organizing Widget 329

9.4 OOP for GUIs 331

GUI OOP Basics 331

Shared Widgets Are Assigned to Instance Variables 333

Shared Data  Are  Assigned to Instance Variables 335

9.5 Case Study: Developing a Calculator 336

The Calculator Buttons and Passing Arguments to Handlers. .

.337

Implementing the "Unofficial" Event Handler click() 338

Chapter Summary 341

Solutions to Practice Problems 341

Exercises 346

Problems 346

10

Recursion 351

10.1 Introduction to Recursion 352

Recursive Functions 352

Recursive Thinking 354

Recursive Function Calls and the Program Stack 356

10.2 Examples of Recursion 358

Recursive Number Sequence Pattern 358

Fractals 360

Virus Scanner 364

Page 10: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 10/11

Contents

10.3 Run Time Analysis 367

The Exponent Function 367

Counting Operations 368

Fibonacci Sequence 369

Experimental Analysis of Run Time 370

10.4 Searching 374

Linear Search 374

Binary Search 374

Other Search Problems 377

10.5 Case Study: Tower of Hanoi 379

Classes Peg and Disk 383

Chapter Summary 385

Solutions to Practice Problems 385

Exercises 387

Problems 388

11

The Web and Search 395

11.1 The World Wide Web 396

Web Servers and Web Clients 396

"Plumbing" of the WWW 397

Naming Scheme: Uniform Resource Locator 397

Protocol:

HyperText Transfer Protocol 398HyperText Markup Language 399

HTML Elements 400

Tree Structure of an HTML Document 401

 Anchor HTML Element and Absolute Links 401

Relative Links 402

11.2 Python WWW  API 403

Module urllib.request 403

Module html.parser 405

Overriding the HTMLParser Handlers 407

Module urllib.parse 408

Parser That Collects HTTP Hyperlinks 409

11.3 String Pattern Matching ,411

Regular Expressions 411

Python Standard Library Module re 414

11.4 Case Study: Web Crawler 415

Recursive Crawler, Version 0.1 416

Recursive Crawler, Version 0.2 418

The Web Page Content  Analysis 420

Page 11: 662647939

7/29/2019 662647939

http://slidepdf.com/reader/full/662647939 11/11

xvi Contents

Chapter Summary 422

Solutions to Practice Problems 423

Exercises 425

Problems 426

12

Databases and Data Processing 429

12.1 Databases and SQL 430

Database Tables 430

Structured Query Language 432

Statement SELECT 432

Clause WHERE 434

Built-in SQL Functions 436

Clause GROUP BY 436

Making SQL Queries Involving Multiple Tables 437

Statement CREATE TABLE 439

Statements INSERT and UPDATE 439

12.2 Database Programming in Python 440

Database Engines and SQLite 440

Creating a Database with sqlite3 441

Committing to Database Changes and Closing the Database...

442

Querying a Database Using sqlite3 443

12.3 Functional Language Approach 445

List Comprehension 445

MapReduce Problem Solving Framework 447

MapReduce, in the Abstract 450

Inverted Index 451

12.4 Parallel Computing 453

Parallel Computing 453

Class Pool of Module multiprocessing 454

Parallel Speedup 457

MapReduce, in Parallel 458

Parallel versus Sequential MapReduce 459

Chapter Summary 461

Solutions to Practice Problems 462

Exercises 465

Problems 466

Index 471