Foundations of Python Programming
This interactive book is a product of the Runestone Interactive Project.
This book is based on the Original work by: Jeffrey Elkner, Allen B. Downey, and Chris Meyers, the Interactive edition of How to Think Like a Computer Scientist by Brad Miller, David Ranum and many more and Programs Information and People by Paul Resnick
Activecode based on Skulpt
Codelens based on Online Python Tutor
Many contributions from the CSLearning4U research group at Georgia Tech.
ACM-SIGCSE for the special projects grant that funded our student Isaac Dontje Lindell for the summer of 2013.
NSF
The Runestone Interactive tools are open source and we encourage you to contact us, or grab a copy from GitHub <https://github.com/RunestoneInteractive> if you would like to use them to write your own resources.
We also welcome your support to help keep Runestone growing.
Table of ContentsΒΆ
- 1. General Introduction
- 1.1. Introduction: The Way of the Program
- 1.2. Algorithms
- 1.3. The Python Programming Language
- 1.4. Special Ways to Execute Python in this Book
- 1.5. More About Programs
- 1.6. Formal and Natural Languages
- 1.7. A Typical First Program
- 1.8. π©βπ» Predict Before You Run!
- 1.9. π©βπ» To Understand a Program, Change It!
- 1.10. Comments
- 1.11. Glossary
- 1.12. Chapter Assessment
- 2. Variables, Statements, and Expressions
- 2.1. Introduction
- 2.2. Values and Data Types
- 2.3. Operators and Operands
- 2.4. Function Calls
- 2.5. Data Types
- 2.6. Type conversion functions
- 2.7. Variables
- 2.8. Variable Names and Keywords
- 2.9. π©βπ» Choosing the Right Variable Name
- 2.10. Statements and Expressions
- 2.11. Order of Operations
- 2.12. Reassignment
- 2.13. Updating Variables
- 2.14. π©βπ» Hard-Coding
- 2.15. Input
- 2.16. Glossary
- 2.17. Exercises
- 2.18. Chapter Assessment
- 3. Debugging
- 4. Python Modules
- 5. Python Turtle
- 5.1. Hello Little Turtles!
- 5.2. Our First Turtle Program
- 5.3. Instances: A Herd of Turtles
- 5.4. Object Oriented Concepts
- 5.5. Repetition with a For Loop
- 5.6. A Few More
turtle
Methods and Observations - 5.7. Summary of Turtle Methods
- 5.8. π©βπ» Incremental Programming
- 5.9. π©βπ» Common
turtle
Errors - 5.10. Exercises
- 5.11. Chapter Assessment - Turtle and Object Mechanics
- 5.12. Chapter Assessment - Drawing with Turtle
- 6. Sequences
- 6.1. Introduction: Sequences
- 6.2. Strings and Lists
- 6.3. Index Operator: Working with the Characters of a String
- 6.4. Disambiguating []: creation vs indexing
- 6.5. Length
- 6.6. The Slice Operator
- 6.7. Concatenation and Repetition
- 6.8. Count and Index
- 6.9. Splitting and Joining Strings
- 6.10. Exercises
- 6.11. Chapter Assessment
- 7. Iteration
- 7.1. Introduction: Iteration
- 7.2. The for Loop
- 7.3. Flow of Execution of the for Loop
- 7.4. Strings and
for
loops - 7.5. Lists and
for
loops - 7.6. The Accumulator Pattern
- 7.7. Traversal and the
for
Loop: By Index - 7.8. Nested Iteration: Image Processing
- 7.9. π©βπ» Printing Intermediate Results
- 7.10. π©βπ» Naming Variables in For Loops
- 7.11. The Gory Details: Iterables
- 7.12. π©βπ» Keeping Track of Your Iterator Variable and Your Iterable
- 7.13. Glossary
- 7.14. Exercises
- 7.15. Chapter Assessment
- 8. Conditionals
- 8.1. Intro: What we can do with Turtles and Conditionals
- 8.2. Boolean Values and Boolean Expressions
- 8.3. Logical operators
- 8.4. The
in
andnot in
operators - 8.5. Precedence of Operators
- 8.6. Conditional Execution: Binary Selection
- 8.7. Omitting the
else
Clause: Unary Selection - 8.8. Nested conditionals
- 8.9. Chained conditionals
- 8.10. The Accumulator Pattern with Conditionals
- 8.11. π©βπ» Setting Up Conditionals
- 8.12. Glossary
- 8.13. Exercises
- 8.14. Chapter Assessment
- 9. Transforming Sequences
- 9.1. Introduction: Transforming Sequences
- 9.2. Mutability
- 9.3. List Element Deletion
- 9.4. Objects and References
- 9.5. Aliasing
- 9.6. Cloning Lists
- 9.7. Mutating Methods
- 9.8. Append versus Concatenate
- 9.9. Non-mutating Methods on Strings
- 9.10. F-Strings
- 9.11. The Accumulator Pattern with Lists
- 9.12. The Accumulator Pattern with Strings
- 9.13. π©βπ» Accumulator Pattern Strategies
- 9.14. π©βπ» Donβt Mutate A List That You Are Iterating Through
- 9.15. Summary
- 9.16. Exercises
- 9.17. Chapter Assessment - List Methods
- 10. Files
- 10.1. Introduction: Working with Data Files
- 10.2. Reading a File
- 10.3. Alternative File Reading Methods
- 10.4. Iterating over lines in a file
- 10.5. Finding a File in your Filesystem
- 10.6. Using
with
for Files - 10.7. Recipe for Reading and Processing a File
- 10.8. Writing Text Files
- 10.9. CSV Format
- 10.10. Reading in data from a CSV File
- 10.11. Writing data to a CSV File
- 10.12. π©βπ» Tips on Handling Files
- 10.13. Glossary
- 10.14. Exercises
- 10.15. Chapter Assessment
- 11. Dictionaries
- 11.1. Introduction: Dictionaries
- 11.2. Getting Started with Dictionaries
- 11.3. Dictionary operations
- 11.4. Dictionary methods
- 11.5. Aliasing and copying
- 11.6. Introduction: Accumulating Multiple Results In a Dictionary
- 11.7. Accumulating Results From a Dictionary
- 11.8. Accumulating the Best Key
- 11.9. π©βπ» When to use a dictionary
- 11.10. Glossary
- 11.11. Exercises
- 11.12. Chapter Assessment
- 12. Functions
- 12.1. Introduction to Functions
- 12.2. Function Definition
- 12.3. Function Invocation
- 12.4. Function Parameters
- 12.5. Returning a value from a function
- 12.6. π©βπ» Decoding a Function
- 12.7. Type Annotations
- 12.8. A function that accumulates
- 12.9. Variables and parameters are local
- 12.10. Global Variables
- 12.11. Functions can call other functions (Composition)
- 12.12. Flow of Execution Summary
- 12.13. π©βπ» Print vs. return
- 12.14. Passing Mutable Objects
- 12.15. Side Effects
- 12.16. Glossary
- 12.17. Exercises
- 12.18. Chapter Assessment
- 13. Tuple Packing and Unpacking
- 14. More About Iteration
- 15. Advanced Functions
- 16. Sorting
- 17. Nested Data and Nested Iteration
- 18. Test Cases
- 18.1. Introduction: Test Cases
- 18.2. Checking Assumptions About Data Types
- 18.3. Checking Other Assumptions
- 18.4. Testing Conditionals
- 18.5. Testing Loops
- 18.6. Writing Test Cases for Functions
- 18.7. Testing Optional Parameters
- 18.8. π©βπ» Program Development
- 18.9. Glossary
- 18.10. Chapter Assessment
- 19. Exceptions
- 20. Defining your own Classes
- 20.1. Introduction: Classes and Objects - the Basics
- 20.2. Objects Revisited
- 20.3. User Defined Classes
- 20.4. Adding Parameters to the Constructor
- 20.5. Adding Other Methods to a Class
- 20.6. Objects as Arguments and Parameters
- 20.7. Converting an Object to a String
- 20.8. Instances as Return Values
- 20.9. Sorting Lists of Instances
- 20.10. Class Variables and Instance Variables
- 20.11. Thinking About Classes and Instances
- 20.12. Testing classes
- 20.13. A Tamagotchi Game
- 20.14. Glossary
- 20.15. Exercises
- 20.16. Chapter Assessment
- 21. Building Programs
- 22. Inheritance
- 23. More on Accumulation: Map, Filter, List Comprehension, and Zip
- 24. Internet APIs
- 24.1. Requesting data from the Internet
- 24.2. The Internet: Behind the Scenes
- 24.3. Anatomy of URLs
- 24.4. The HTTP protocol
- 24.5. Using REST APIs
- 24.6. Fetching a page
- 24.7. Caching Response Content
- 24.8. Figuring Out How to Use a REST API
- 24.9. Debugging calls to requests.get()
- 24.10. Requests Cookbook
- 24.11. Searching for Media on iTunes
- 24.12. Searching for tags on flickr
- 24.13. Unicode for non-English characters
- 24.14. Project - OMDB and TasteDive