If you want to learn how to program, working with Python is an excellent way to start. This hands-on guide takes you through the language one step at a time, beginning with basic programming concepts before moving on to functions, recursion, data structures, and object-oriented design.
Through exercises in each chapter, you’ll try out programming concepts as you learn them. Think Python is ideal for students at the high school or college level, as well as self-learners, home-schooled students, and professionals who need to learn programming basics.
-
Start with the basics, including language syntax and semantics
-
Get a clear definition of each programming concept
-
Learn values, variables, statements, functions, and data structures in a logical progression
-
Discover how to work with files and databases
-
Understand objects, methods, and object-oriented programming
-
Use debugging techniques to fix syntax, runtime, and semantic errors
-
Explore interface design, data structures, and GUI-based programs through case studies
About The Author
Allen Downey is an Associate Professor of Computer Science at the Olin College of Engineering. He has taught computer science at Wellesley College, Colby College and U.C. Berkeley. He has a Ph.D. in Computer Science from U.C. Berkeley and Master’s and Bachelor’s degrees from MIT.
Table of Contents
Chapter 1 The Way of the Program
-
The Python Programming Language
-
What Is a Program?
-
What Is Debugging?
-
Syntax Errors
-
Runtime Errors
-
Semantic Errors
-
Experimental Debugging
-
Formal and Natural Languages
-
The First Program
-
Debugging
-
Glossary
-
Exercises
Chapter 2 Variables, Expressions, and Statements
-
Values and Types
-
Variables
-
Variable Names and Keywords
-
Operators and Operands
-
Expressions and Statements
-
Interactive Mode and Script Mode
-
Order of Operations
-
String Operations
-
Comments
-
Debugging
-
Glossary
-
Exercises
Chapter 3 Functions
-
Function Calls
-
Type Conversion Functions
-
Math Functions
-
Composition
-
Adding New Functions
-
Definitions and Uses
-
Flow of Execution
-
Parameters and Arguments
-
Variables and Parameters Are Local
-
Stack Diagrams
-
Fruitful Functions and Void Functions
-
Why Functions?
-
Importing with from
-
Debugging
-
Glossary
-
Exercises
Chapter 4 Case Study: Interface Design
-
TurtleWorld
-
Simple Repetition
-
Exercises
-
Encapsulation
-
Generalization
-
Interface Design
-
Refactoring
-
A Development Plan
-
Docstring
-
Debugging
-
Glossary
-
Exercises
Chapter 5 Conditionals and Recursion
-
Modulus Operator
-
Boolean Expressions
-
Logical Operators
-
Conditional Execution
-
Alternative Execution
-
Chained Conditionals
-
Nested Conditionals
-
Recursion
-
Stack Diagrams for Recursive Functions
-
Infinite Recursion
-
Keyboard Input
-
Debugging
-
Glossary
-
Exercises
Chapter 6 Fruitful Functions
-
Return Values
-
Incremental Development
-
Composition
-
Boolean Functions
-
More Recursion
-
Leap of Faith
-
One More Example
-
Checking Types
-
Debugging
-
Glossary
-
Exercises
Chapter 7 Iteration
-
Multiple Assignment
-
Updating Variables
-
The while Statement
-
break
-
Square Roots
-
Algorithms
-
Debugging
-
Glossary
-
Exercises
Chapter 8 Strings
-
A String Is a Sequence
-
len
-
Traversal with a for Loop
-
String Slices
-
Strings Are Immutable
-
Searching
-
Looping and Counting
-
String Methods
-
The in Operator
-
String Comparison
-
Debugging
-
Glossary
-
Exercises
Chapter 9 Case Study: Word Play
-
Reading Word Lists
-
Exercises
-
Search
-
Looping with Indices
-
Debugging
-
Glossary
-
Exercises
Chapter 10 Lists
-
A List Is a Sequence
-
Lists Are Mutable
-
Traversing a List
-
List Operations
-
List Slices
-
List Methods
-
Map, Filter, and Reduce
-
Deleting Elements
-
Lists and Strings
-
Objects and Values
-
Aliasing
-
List Arguments
-
Debugging
-
Glossary
-
Exercises
Chapter 11 Dictionaries
-
Dictionary as a Set of Counters
-
Looping and Dictionaries
-
Reverse Lookup
-
Dictionaries and Lists
-
Memos
-
Global Variables
-
Long Integers
-
Debugging
-
Glossary
-
Exercises
Chapter 12 Tuples
-
Tuples Are Immutable
-
Tuple Assignment
-
Tuples as Return Values
-
Variable-Length Argument Tuples
-
Lists and Tuples
-
Dictionaries and Tuples
-
Comparing Tuples
-
Sequences of Sequences
-
Debugging
-
Glossary
-
Exercises
Chapter 13 Case Study: Data Structure Selection
-
Word Frequency Analysis
-
Random Numbers
-
Word Histogram
-
Most Common Words
-
Optional Parameters
-
Dictionary Subtraction
-
Random Words
-
Markov Analysis
-
Data Structures
-
Debugging
-
Glossary
-
Exercises
Chapter 14 Files
-
Persistence
-
Reading and Writing
-
Format Operator
-
Filenames and Paths
-
Catching Exceptions
-
Databases
-
Pickling
-
Pipes
-
Writing Modules
-
Debugging
-
Glossary
-
Exercises
Chapter 15 Classes and Objects
-
User-Defined Types
-
Attributes
-
Rectangles
-
Instances as Return Values
-
Objects Are Mutable
-
Copying
-
Debugging
-
Glossary
-
Exercises
Chapter 16 Classes and Functions
-
Time
-
Pure Functions
-
Modifiers
-
Prototyping Versus Planning
-
Debugging
-
Glossary
-
Exercises
Chapter 17 Classes and Methods
-
Object-Oriented Features
-
Printing Objects
-
Another Example
-
A More Complicated Example
-
The init Method
-
The __str__ Method
-
Operator Overloading
-
Type-Based Dispatch
-
Polymorphism
-
Debugging
-
Interface and Implementation
-
Glossary
-
Exercises
Chapter 18 Inheritance
-
Card Objects
-
Class Attributes
-
Comparing Cards
-
Decks
-
Printing the Deck
-
Add, Remove, Shuffle, and Sort
-
Inheritance
-
Class Diagrams
-
Debugging
-
Data Encapsulation
-
Glossary
-
Exercises
Chapter 19 Case Study: Tkinter
-
GUI
-
Buttons and Callbacks
-
Canvas Widgets
-
Coordinate Sequences
-
More Widgets
-
Packing Widgets
-
Menus and Callables
-
Binding
-
Debugging
-
Glossary
-
Exercises
Appendix Debugging
-
Syntax Errors
-
Runtime Errors
-
Semantic Errors
Appendix Analysis of Algorithms
-
Order of Growth
-
Analysis of Basic Python Operations
-
Analysis of Search Algorithms
-
Hashtables
Appendix Lumpy
-
State Diagram
-
Stack Diagram
-
Object Diagrams
-
Function and Class Objects
-
Class Diagrams
Colophon