## Table of Contents

1. Introduction

1.1 Computer Programs

1.2 The Anatomy of a Computer

1.3 Computing and Society: Computers Are Everywhere

1.4 The Python Programming Language

1.5 Becoming Familiar with Your Programming Environment

1.6 Special Topic: The Python Interpreter

1.7 Analyzing Your First Program

1.8 Errors

1.9 Problem Solving: Algorithm Design

1.10 Computing and Society: Data Is Everywhere

1.11 How To: Describing an Algorithm with Pseudocode

1.12 Worked Example: Writing an Algorithm for Tiling a Floor

1.13 Chapter Summary

1.14 Interactive Practice: Computer Programs

1.15 Interactive Practice: The Anatomy of a Computer

1.16 Interactive Practice: The Python Programming Language

1.17 Interactive Practice: The Programming Environment

1.18 Interactive Practice: First Programs

1.19 Interactive Practice: Errors

1.20 Interactive Practice: Algorithm Design

1.21 Review Exercises

1.22 Programming Projects

2. Programming with Numbers and Strings

2.1 Variables

2.2 Arithmetic

2.3 Special Topic: Other Ways to Import Modules

2.4 Special Topic: Combining Assignment and Arithmetic

2.5 Special Topic: Line Joining

2.6 Problem Solving: First Do It By Hand

2.7 Worked Example: Computing Travel Time

2.8 Strings

2.9 Special Topic: Character Values

2.10 Special Topic: Escape Sequences

2.11 Computing and Society: International Alphabets and Unicode

2.12 Input and Output

2.13 How To: Writing Simple Programs

2.14 Worked Example: Computing the Cost of Stamps

2.15 Computing and Society: Bugs in Silicon

2.16 Graphics: Simple Drawings

2.17 How To: Graphics: Drawing Graphical Shapes

2.18 Toolbox: Symbolic Processing with SymPy

2.19 Chapter Summary

2.20 Interactive Practice: Variables

2.21 Interactive Practice: Arithmetic

2.22 Interactive Practice: First Do It By Hand

2.23 Interactive Practice: Strings

2.24 Interactive Practice: Input and Output

2.25 Interactive Practice: Simple Drawings

2.26 Review Exercises

2.27 Programming Projects

3. Decisions

3.1 The if Statement

3.2 Special Topic: Conditional Expressions

3.3 Relational Operators

3.4 Special Topic: Lexicographic Ordering of Strings

3.5 How To: Implementing an if Statement

3.6 Worked Example: Extracting the Middle

3.7 Nested Branches

3.8 Computing and Society: Dysfunctional Computerized Systems

3.9 Multiple Alternatives

3.10 Toolbox: Sending E-mail

3.11 Problem Solving: Flowcharts

3.12 Problem Solving: Test Cases

3.13 Boolean Variables and Operators

3.14 Special Topic: Chaining Relational Operators

3.15 Special Topic: Short-Circuit Evaluation of Boolean Operators

3.16 Special Topic: De Morgan’s Law

3.17 Analyzing Strings

3.18 Application: Input Validation

3.19 Special Topic: Terminating a Program

3.20 Special Topic: Interactive Graphical Programs

3.21 Computing and Society: Artificial Intelligence

3.22 Worked Example: Graphics: Intersecting Circles

3.23 Toolbox: Plotting Simple Graphs

3.24 Chapter Summary

3.25 Interactive Practice: The if Statement

3.26 Interactive Practice: Relational Operators

3.27 Interactive Practice: Nested Branches

3.28 Interactive Practice: Multiple Alternatives

3.29 Interactive Practice: Flowcharts

3.30 Interactive Practice: Test Cases

3.31 Interactive Practice: Boolean Variables and Operators

3.32 Interactive Practice: Analyzing Strings

3.33 Interactive Practice: Input Validation

3.34 Review Exercises

3.35 Programming Projects

4. Loops

4.1 The while Loop

4.2 Special Topic: Special Form of the print Function

4.3 Computing and Society: The First Bug

4.4 Problem Solving: Hand-Tracing

4.5 Application: Processing Sentinel Values

4.6 Special Topic: Processing Sentinel Values with a Boolean Variable

4.7 Special Topic: Redirection of Input and Output

4.8 Problem Solving: Storyboards

4.9 Common Loop Algorithms

4.10 The for Loop

4.11 How To: Writing a Loop

4.12 Nested Loops

4.13 Worked Example: Average Exam Grades

4.14 Worked Example: A Grade Distribution Histogram

4.15 Processing Strings

4.16 Application: Random Numbers and Simulations

4.17 Worked Example: Graphics: Bull’s Eye

4.18 Graphics: Digital Image Processing

4.19 Problem Solving: Solve a Simpler Problem First

4.20 Computing and Society: Digital Piracy

4.21 Chapter Summary

4.22 Interactive Practice: The while Loop

4.23 Interactive Practice: Hand Tracing

4.24 Interactive Practice: Processing Sentinel Values

4.25 Interactive Practice: Storyboards

4.26 Interactive Practice: Common Loop Algorithms

4.27 Interactive Practice: The for Loop

4.28 Interactive Practice: Nested Loops

4.29 Interactive Practice: Processing Strings

4.30 Interactive Practice: Random Numbers and Simulations

4.31 Interactive Practice: Digital Image Processing

4.32 Interactive Practice: Solve a Simpler Problem First

4.33 Review Exercises

4.34 Programming Projects

5. Functions

5.1 Functions as Black Boxes

5.2 Implementing and Testing Functions

5.3 Parameter Passing

5.4 Return Values

5.5 Special Topic: Using Single-Line Compound Statements

5.6 How To: Implementing a Function

5.7 Worked Example: Generating Random Passwords

5.8 Functions Without Return Values

5.9 Computing and Society: Personal Computing

5.10 Problem Solving: Reusable Functions

5.11 Problem Solving: Stepwise Refinement

5.12 Worked Example: Calculating a Course Grade

5.13 Worked Example: Using a Debugger

5.14 Variable Scope

5.15 Worked Example: Graphics: Rolling Dice

5.16 Graphics: Building an Image Processing Toolkit

5.17 Worked Example: Plotting Growth or Decay

5.18 Recursive Functions

5.19 How To: Thinking Recursively

5.20 Toolbox: Turtle Graphics

5.21 Chapter Summary

5.22 Interactive Practice: Functions as Black Boxes

5.23 Interactive Practice: Implementing and Testing Functions

5.24 Interactive Practice: Parameter Passing

5.25 Interactive Practice: Return Values

5.26 Interactive Practice: Functions Without Return Values

5.27 Interactive Practice: Reusable Functions

5.28 Interactive Practice: Stepwise Refinement

5.29 Interactive Practice: Variable Scope

5.30 Interactive Practice: Building an Image Processing Toolkit

5.31 Interactive Practice: Recursive Functions

5.32 Review Exercises

5.33 Programming Projects

6. Lists

6.1 Basic Properties of Lists

6.2 Special Topic: Negative Subscripts

6.3 Special Topic: Common Container Functions

6.4 Computing and Society: Computer Viruses

6.5 List Operations

6.6 Special Topic: Slices

6.7 Common List Algorithms

6.8 Worked Example: Plotting Trigonometric Functions

6.9 Using Lists with Functions

6.10 Special Topic: Call by Value and Call by Reference

6.11 Special Topic: Tuples

6.12 Special Topic: Functions with a Variable Number of Arguments

6.13 Special Topic: Tuple Assignment

6.14 Special Topic: Returning Multiple Values with Tuples

6.15 Toolbox: Editing Sound Files

6.16 Problem Solving: Adapting Algorithms

6.17 How To: Working with Lists

6.18 Worked Example: Rolling the Dice

6.19 Problem Solving: Discovering Algorithms by Manipulating Physical Objects

6.20 Tables

6.21 Worked Example: A World Population Table

6.22 Special Topic: Tables with Variable Row Lengths

6.23 Worked Example: Graphics: Drawing Regular Polygons

6.24 Chapter Summary

6.25 Interactive Practice: Basic Properties of Lists

6.26 Interactive Practice: List Operations

6.27 Interactive Practice: Common List Algorithms

6.28 Interactive Practice: Using Lists with Functions

6.29 Interactive Practice: Adapting Algorithms

6.30 Interactive Practice: Discovering Algorithms

6.31 Interactive Practice: Tables

6.32 Review Exercises

6.33 Programming Projects

7. Files and Exceptions

7.1 Reading and Writing Text Files

7.2 Text Input and Output

7.3 Special Topic: Reading the Entire File

7.4 Special Topic: Regular Expressions

7.5 Special Topic: Character Encodings

7.6 Toolbox: Working with CSV Files

7.7 Command Line Arguments

7.8 How To: Processing Text Files

7.9 Worked Example: Analyzing Baby Names

7.10 Toolbox: Working with Files and Directories

7.11 Computing and Society: Encryption Algorithms

7.12 Binary Files and Random Access

7.13 Worked Example: Graphics: Displaying a Scene File

7.14 Exception Handling

7.15 Special Topic: The with Statement

7.16 Toolbox: Reading Web Pages

7.17 Application: Handling Input Errors

7.18 Toolbox: Statistical Analysis

7.19 Worked Example: Creating a Bubble Chart

7.20 Computing and Society: The Ariane Rocket Incident

7.21 Chapter Summary

7.22 Interactive Practice: Text Files

7.23 Interactive Practice: Text Input and Output

7.24 Interactive Practice: Command Line Arguments

7.25 Interactive Practice: Binary Files

7.26 Interactive Practice: Exception Handling

7.27 Interactive Practice: Handling Input Errors

7.28 Review Exercises

7.29 Programming Projects

8. Sets and Dictionaries

8.1 Sets

8.2 Worked Example: Counting Unique Words

8.3 Special Topic: Hashing

8.4 Computing and Society: Standardization

8.5 Dictionaries

8.6 Special Topic: Iterating over Dictionary Items

8.7 Special Topic: Storing Data Records

8.8 Worked Example: Translating Text Messages

8.9 Complex Structures

8.10 Special Topic: User Modules

8.11 Worked Example: Graphics: Pie Charts

8.12 Toolbox: Harvesting JSON Data from the Web

8.13 Chapter Summary

8.14 Interactive Practice: Sets

8.15 Interactive Practice: Dictionaries

8.16 Interactive Practice: Complex Structures

8.17 Review Exercises

8.18 Programming Projects

9. Objects and Classes

9.1 Object-Oriented Programming

9.2 Implementing a Simple Class

9.3 Specifying the Public Interface of a Class

9.4 Designing the Data Representation

9.5 Constructors

9.6 Special Topic: Default and Named Arguments

9.7 Implementing Methods

9.8 Special Topic: Class Variables

9.9 Testing a Class

9.10 How To: Implementing a Class

9.11 Worked Example: Implementing a Bank Account Class

9.12 Problem Solving: Tracing Objects

9.13 Problem Solving: Patterns for Object Data

9.14 Object References

9.15 Computing and Society: Electronic Voting

9.16 Application: Writing a Fraction Class

9.17 Special Topic: Object Types and Instances

9.18 Worked Example: Graphics: A Die Class

9.19 Computing and Society: Open Source and Free Software

9.20 Chapter Summary

9.21 Interactive Practice: Object-Oriented Programming

9.22 Interactive Practice: Implementing a Simple Class

9.23 Interactive Practice: Specifying the Public Interface

9.24 Interactive Practice: Designing the Data Representation

9.25 Interactive Practice: Constructors

9.26 Interactive Practice: Implementing Methods

9.27 Interactive Practice: Testing a Class

9.28 Interactive Practice: Tracing Objects

9.29 Interactive Practice: Patterns for Object Data

9.30 Interactive Practice: Object References

9.31 Interactive Practice: Writing a Fraction Class

9.32 Review Exercises

9.33 Programming Projects

10. Inheritance

10.1 Inheritance Hierarchies

10.2 Special Topic: The Cosmic Superclass: object

10.3 Implementing Subclasses

10.4 Calling the Superclass Constructor

10.5 Overriding Methods

10.6 Polymorphism

10.7 Special Topic: Subclasses and Instances

10.8 Special Topic: Dynamic Method Lookup

10.9 Special Topic: Abstract Classes

10.10 How To: Developing an Inheritance Hierarchy

10.11 Worked Example: Implementing an Employee Hierarchy for Payroll Processing

10.12 Application: A Geometric Shape Class Hierarchy

10.13 Toolbox: Game Programming

10.14 Chapter Summary

10.15 Interactive Practice: Inheritance Hierarchies

10.16 Interactive Practice: Implementing Subclasses

10.17 Interactive Practice: Calling the Superclass Constructor

10.18 Interactive Practice: Overriding Methods

10.19 Interactive Practice: Polymorphism

10.20 Interactive Practice: A Geometric Shape Class Hierarchy

10.21 Review Exercises

10.22 Programming Projects

11. Recursion

11.1 Triangle Numbers Revisited

11.2 Special Topic: Recursion with Objects

11.3 Problem Solving: Thinking Recursively

11.4 Worked Example: Finding Files

11.5 Recursive Helper Functions

11.6 The Efficiency of Recursion

11.7 Permutations

11.8 Computing and Society: The Limits of Computation

11.9 Backtracking

11.10 Worked Example: Towers of Hanoi

11.11 Mutual Recursion

11.12 Toolbox: Analyzing Web Pages with Beautiful Soup

11.13 Chapter Summary

11.14 Interactive Practice: Triangle Numbers Revisited

11.15 Interactive Practice: Thinking Recursively

11.16 Interactive Practice: Recursive Helper Functions

11.17 Interactive Practice: The Efficiency of Recursion

11.18 Interactive Practice: Permutations

11.19 Interactive Practice: Backtracking

11.20 Interactive Practice: Mutual Recursion

11.21 Review Exercises

11.22 Programming Projects

12. Sorting and Searching

12.1 Selection Sort

12.2 Profiling the Selection Sort Algorithm

12.3 Analyzing the Performance of the Selection Sort Algorithm

12.4 Special Topic: Oh, Omega, and Theta

12.5 Special Topic: Insertion Sort

12.6 Merge Sort

12.7 Analyzing the Merge Sort Algorithm

12.8 Special Topic: The Quicksort Algorithm

12.9 Computing and Society: The First Programmer

12.10 Searching

12.11 Problem Solving: Estimating the Running Time of an Algorithm

12.12 Special Topic: Comparing Objects

12.13 Worked Example: Enhancing the Insertion Sort Algorithm

12.14 Chapter Summary

12.15 Interactive Practice: Selection Sort

12.16 Interactive Practice: Profiling Selection Sort

12.17 Interactive Practice: Analyzing Selection Sort

12.18 Interactive Practice: Merge Sort

12.19 Interactive Practice: Analyzing Merge Sort

12.20 Interactive Practice: SearchingNEC

12.21 Interactive Practice: Estimating Running Times

12.22 Review Exercises

12.23 Programming Projects

## The interactive zyBooks version of a classic introduction to programming

**Python for Everyone (3e)** zyVersion offers the complete contents of the 3rd edition of this popular textbook, plus new immersive activities and integrated live coding to help beginning programmers learn faster and more effectively.

- Goes beyond syntax and semantics to help students understand foundational programming principles, using Python as a vehicle for instruction
- Incorporates code trace exercises, Parsons problems and Jupyter Notebooks learning questions throughout the book
- Includes practical challenges, like statistical computations, drawing graphs and charts, and game programming

#### Designed to serve a wide range of student interests and abilities, the book requires no prior programming experience.

“In a CS1 course, you want to stress concepts that will prepare students for advanced learning, not just teach them a programming language; in our book we purposefully go deeper to give students that foundation.”

Co-author Cay Horstmann

### Dr. Cay Horstmann discusses the benefits of teaching with zyBooks

## What is a zyVersion?

zyVersions are leading print titles converted and adapted to zyBooks’ interactive learning platform, allowing for a quick and easy transition to an engaging digital experience for instructors and students.

zyBooks’ web-native content helps students visualize concepts to learn faster and more effectively than with a traditional textbook. (Check out our research.)

This zyVersion of **Python for Everyone** benefits both students and instructors:

- Instructor benefits
- Customize your course by reorganizing existing content or adding your own
- Continuous publication model updates your course with the latest content and technologies
- Robust reporting gives you insight into students’ progress, reading and participation
- Animations and other interactive content can be shown in presentation mode and used during a lecture

- Student benefits
- Learning questions and other content serve as an interactive form of reading
- Instant feedback on labs and homework
- Concepts come to life through extensive animations embedded into the interactive content
- Save chapters as PDFs to reference the material at any time

### The Python for Everyone zyVersion in action

In this video, coauthor Rance Necaise demonstrates how Jupyter Notebooks and other embedded tools advance learning:

## Authors

**Cay Horstmann, PhD**

Professor of Computer Science / San Jose State University

**Rance Necaise, PhD**

Associate Professor of Computer Science / Randolph-Macon College