Python Programming, Third Edition

Python Programming, Third Edition

Now pay Easier and Secure using Paypal

Read more


John M. Zelle

e-books shop
e-books shop
Purchase Now !
Just with Paypal

Book Details
 554 p
 File Size 
 5,838 KB
 File Type
 PDF format
 2017 Franklin, Beedle
 & Associates Incorporated 

When the publisher first sent me a draft of this book, I was immediately excited.
Disguised as a Python textbook, it is really an introduction to the fine art of programming,
using Python merely as the preferred medium for beginners. This is
how I have always imagined Python would be most useful in education: not as
the only language, but as a first language, just as in art one might start learning
to draw using a pencil rather than trying to paint in oil right away.
The author mentions in his preface that Python is near-ideal as a first programming
language, without being a "toy language. " As the creator of Python I
don't want to take full credit for this: Python was derived from ABC, a language
designed to teach programming in the early 1980s by Lambert Meertens, Leo
Geurts, and others at CWI (National Research Institute for Mathematics and
Computer Science) in Amsterdam. If I added anything to their work, it was making
Python into a non-toy language, with a broad user base and an extensive
collection of standard and third-party application modules.
I have no formal teaching experience, so I may not be qualified to judge its
educational effectiveness. Still, as a programmer with nearly 30 years experience,
reading through the chapters I am continuously delighted by the book's
clear explanations of difficult concepts. I also like the many good excercises and
questions which both test understanding and encourage thinking about deeper
Reader of this book, congratulations! You will be well rewarded for studying
Python. I promise you'll have fun along the way, and I hope you won't forget
your first language once you have become a proficient software developer.
-Guido van Rossum

This book is designed to be used as a primary textbook in a college-level first
course in computing. It takes a fairly traditional approach, emphasizing problem
solving, design, and programming as the core skills of computer science. However,
these ideas are illustrated using a non-traditional language, namely Python. In my
teaching experience, I have found that many students have difficulty mastering
the basic concepts of computer science and programming. Part of this difficulty
can be blamed on the complexity of the languages and tools that are most often
used in introductory courses. Consequently, this textbook was written with a
single overarching goal: to introduce fundamental computer science concepts as
simply as possible without being simplistic. Using Python is central to this goal.

Traditional systems languages such as C++, Ada, and Java evolved to solve
problems in large-scale programming, where the primary emphasis is on structure
and discipline. They were not designed to make writing small- or mediumscale
programs easy. The recent rise in popularity of scripting (sometimes called
"agile") languages, such as Python, suggests an alternative approach. Python
is very flexible and makes experimentation easy. Solutions to simple problems
are simply and elegantly expressed. Python provides a great laboratory for the
neophyte programmer.

Python has a number of features that make it a near-perfect choice as a
first programming language. The basic structures are simple, clean, and well
designed, which allows students to focus on the primary skills of algorithmic
thinking and program design without getting bogged down in arcane language
details. Concepts learned in Python carry over directly to subsequent study of
systems languages such as C++ and Java. But Python is not a "toy language."
It is a real-world production language that is freely available for virtually every
programming platform and comes standard with its own easy-to-use integrated
programming environment. The best part is that Python makes learning to program fun again.

Although I use Python as the language, teaching Python is not the main
point of this book. Rather, Python is used to illustrate fundamental principles of
design and programming that apply in any language or computing environment.
In some places I have purposely avoided certain Python features and idioms that
are not generally found in other languages. There are many good books about
Python on the market; this book is intended as an introduction to computing.
Besides using Python, there are other features of this book designed to make it
a gentler introduction to computer science. Some of these features include:

Table of Contents
Foreword, by Guido van Rossum .................... ix
Preface . . . . . . . . . . . . . . . . x
Chapter 1 Computers and Programs
1.1 The Universal Machine 
1.2 Program Power 
1. 3 What Is Computer Science?
1.4 Hardware Basic
1.5 Programming Languages 
1.6 The Magic of Python
1. 7 Inside a Python Program
1.8 Chaos and Computers
1. 9 Chapter S u m mary
1.10 Exercise
Chapter 2 Writing Simple Programs
2.1 The Software Development Process
2. 2 Exam pie Program: T em perature Converter
2.3 Elements of Programs
2.3.1 Names
2.3.2 Expressions
2.4 0 utput Statements
2. 5 Assignment Statements
2. 5 .1 S i m pIe Assign men t
2.5.2 Assigning Input 
2.5.3 Simultaneous Assignment
2. 6 Definite Loops
2.8 Chapter Summary
2.9 Exercises
Chapter 3 Computing with Numbers
3.1 Numeric Data Types
3. 2 Type Conversions and Rounding
3.3 Using the Math Library
3.4 Accumulating Results: Factorials 
3.5 Limitations of Computer Arithmetic
3.6 Chapter Summary
3. 7 Exercises
Chapter 4 Objects and Graphics
4.1 Overview
4. 2 T h e 0 b j ect of 0 b j ects
4.3 Simple Graphics Programming
4.4 Using Graphical Objects
4.5 Graphing Future Value
4.6 Choosing Coordinates
4. 7 Interactive Graphics 
4.7.1 Getting Mouse Clicks
4. 7.2 Handling Textual Input
4.8 Graphics Module Reference
4.8.1 Graph Win Objects
4. 8. 2 G ra ph i cs 0 b j ects
4.8.3 Entry Objects
4.8.4 Displaying I mages
4.8.5 Generating Colors
4.8.6 Controlling Display Updates (Advanced)
4. 9 Chapter Sum mary 
4.10 Exercises
Chapter 5 Sequences: Strings, Lists, and Files
5.2 Si m pie String Processing
5.3 Lists as Sequences
5.4 String Representation and Message Encoding 
5.4.1 String Representation
5.4.2 Programming an Encoder
5.5 String Methods
5.5.1 Programming a Decoder
5.5.2 More String Methods
5.6 Lists Have Methods. Too 
5. 7 From Encoding to Encryption
5.8 Input/Output as String Manipulation 
5. 8.1 Exam pie Application: Date Conversion
5. 8. 2 String Formatting
5.8.3 Better Change Counter
5. 9 File Processing 
5.9.1 Multi-line Strings
5.9.2 File Processing
5.9.3 Example Program: Batch Usernames
5.9.4 File Dialogs (Optional)
5.10 Chapter Summary 
5.11 Exercises 
Chapter 6 Defining Functions
6.1 The Function of Functions
6.2 Functions, Informally 
6.3 Future Value with a Function
6.4 Functions and Para meters: The Exciting Deta i Is
6.5 Functions That Return Values
6.6 Functions that Modify Para meters 
6.7 Functions and Program Structure
6.8 Chapter Summary
6.9 Exercises
Chapter 7 Decision Structures
7.1 Sim pie Decisions
7.1.1 Example: Temperature Warnings 
7.1.2 Forming Simple Conditions
7 .1.3 Example: Condition a I Program Execution
7. 2 Two-Way Decisions
7.3 Multi-Way Decisions
7.4 Exception Handling 
7. 5 Study in Design: Max of Three 
7.5.1 Strategy 1: Compare Each to All
7.5. 2 Strategy 2: Decision Tree
7 .5.3 Strategy 3: Sequential Processing
7 .5.4 Strategy 4: Use Python
7 .5.5 Some Lessons
7.6 Chapter Summary
7. 7 Exercises
Chapter 8 Loop Structures and Booleans
8.1 For Loops: A Quick Review
8.2 Indefinite Loops
8. 3 Common Loop Patterns
8. 3.1 Interactive Loops
8.3.2 Sentinel Loops
8.3.3 File Loops
8.3.4 Nested Loops
8.4 Computing with Boo leans
8.4.1 Boolean Operators 
8.4.2 Boolean Algebra
8. 5 Other Common Structures
8.5.1 Post-test Loop 
8.5.2 Loop and a Half 
8.5.3 Boolean Expressions as Decisions
8.6 Example: A Simple Event Loop 
8. 7 Chapter Summary
8. 8 Exercises
Chapter 9 Simulation and Design
9 .1 S i m u I at i n g Ra cq u et ba II
9.1.1 A Simulation Problem
9 .1. 2 Ana lysis and Specification
9. 2 Pseudo-random Numbers 
9.3 Top-Down Design
9. 3.1 Top-Level Design 
9. 3. 2 Separation of Concerns
9.3.3 Second-Level Design 
9.3.4 Designing simNGames
9.3.5 Third-Level Design
9.3.6 Finishing Up
9.3. 7 Summary of the Design Process
9.4 Bottom-Up Implementation
9.4.1 Unit Testing
9.4.2 Simulation Results
9. 5 Other Design Techniques 
9.5.1 Prototyping and Spiral Development 
9.5.2 The Art of Design
9.6 Chapter Summary
9. 7 Exercises
Chapter 10 Defining Classes
10.1 Quick Review of Objects
10.2 Example Program: Cannonball
10.2.1 Program Specification
10.2.2 Designing the Program
10.2.3 Mod ularizing the Program
10.3 Defining New Classes
10.3.1 Example: Multi-sided Dice
10.3.2 Example: The Projectile Class
10.4 Data Processing with Class
10.5 0 bjects and Encapsulation
10.5.1 Encapsulating Useful Abstractions
10.5.2 Putting Classes in Modules
10.5.3 Module Documentation
10.5.4 Working with Multiple Modules
10.6 Widgets 
10.6.1 Example Program: Dice Roller
10.6.2 Building Buttons 
10.6.3 Building Dice
10.6.4 The Main Program
10. 7 Anima ted Can non ba II
10.7.1 Drawing the Animation Window
10.7 .2 Creating a Shot Tracker
10.7.3 Creating an Input Dialog
10.7.4 The Main Event Loop
10. 8 Chapter S u m mary
10.9 Exercises
Chapter 11 Data Collections
11.1 Exam pie Problem: S i m pie Statistics
11.2 Applying Lists
11.2.1 Lists and Arrays
11.2. 2 List 0 perations
11.2.3 Statistics with Lists
11.3 Lists of Records
11.4 Designing with Lists and Classes
11.5 Case Study: Python Ca leu Ia tor
11.5.1 A Calculator as an Object
11.5. 2 Constructing the Interface
11.5.3 Processing Buttons 
11.6 Case Study: Better Can non ba II Animation
11.6 .1 Creating a Launcher
11.6.2 Tracking Multiple Shots 
11.7 Non-seq uenti a I Collections
11.7 .1 Dictionary Basics
11.7. 2 Dictionary 0 perations
11.7.3 Example Program: Word Frequency
11. 8 Chapter S u m mary
11.9 Exercises
Chapter 12 Object-Oriented Design
12.1 The Process of OOD
12.2 Case Study: Racq uetba II Simulation
12.2.1 Candidate Objects and Methods
12.2.2 Implementing SimStat
12.2.3 Implementing RBaiiGame 
12.2.4 Implementing Player
12.2.5 The Complete Program
12.3 Case Study: Dice Poker
12.3.1 Program Specification
12.3.2 Identifying Candidate Objects
12.3.3Implementing the Model
12.3.4 A Text-Based U I
12.3.5 Developing a G U I
12.4 00 Concepts
12.4.1 Encapsulation
12.4.2 Polymorph ism
12.4.3 Inheritance
12. 5 Chapter S u m mary
12.6 Exercises
Chapter 13 Algorithm Design and Recursion
13.1 Searching
13.1.1 A Si m pie Searching Problem
13.1.2 Strategy 1: Linear Search
13.1.3 Strategy 2: Binary Search
13.1.4 Com paring Algorithms
13.2 Recursive Problem Solving
13.2.1 Recursive Definitions 
13.2.2 Recursive Functions
13.2.3 Example: String Reversal
13.2.4 Example: Anagrams
13.2.5 Example: Fast Exponentiation
13.2.6 Example: Binary Search 
13.2. 7 Recursion vs. Iteration
13.3 Sorting Algorithms
13.3.1 Naive Sorting: Selection Sort
13.3.2 Divide and Conquer: Merge Sort
13 . 3 . 3 Com pa r i n g Sorts
13.4 Hard Problems
13.4.1 Tower of Hanoi
13.4.2 The Halting Problem
13.4.3 Conclusion
13.5 Chapter Summary
13.6 Exercises 
Appendix A Python Quick Reference
Appendix C Glossary

e-books shop

• Extensive use of computer graphics. Students love working on
programs that include graphics. This book presents a simple-to-use graphics
package (provided as a Python module) that allows students both to
learn the principles of computer graphics and to practice object-oriented
concepts without the complexity inherent in a full-blown graphics library
and event-driven programming.
• Interesting examples. The book is packed with complete programming
examples to solve real problems.
• Readable prose. The narrative style of the book introduces key computer
science concepts in a natural way as an outgrowth of a developing discussion.
I have tried to avoid random facts or tangentially related sidebars.
• Flexible spiral coverage. Since the goal of the book is to present concepts
simply, each chapter is organized so that students are introduced to
new ideas in a gradual way, giving them time to assimilate an increasing
level of detail as they progress. Ideas that take more time to master are
introduced in early chapters and reinforced in later chapters.
• Just-in-time object coverage. The proper place for the introduction of
object-oriented techniques is an ongoing controversy in computer science
education. This book is neither strictly "objects early'' nor "objects late,"
but gradually introduces object concepts after a brief initial grounding
in the basics of imperative programming. Students learn multiple design
techniques, including top-down (functional decomposition) , spiral (prototyping)
, and object-oriented methods. Additionally, the textbook material
is flexible enough to accommodate other approaches.
• Extensive end-of-chapter problems. Exercises at the end of every
chapter provide ample opportunity for students to reinforce their mastery
of the chapter material and to practice new programming skills.