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 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
- 1.13. π€ A Preview of the End Goal
- 1.14. π€ Driving the Turtle
- 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
- 2.19. π€ Input Compute Output
- 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
- 5.13. π€ Drawing a Circle with the 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. Disabmiguating []: 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
- 6.12. π€ Generating a Password XKCD Style
- 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
- 7.16. π€ Computing Statistics with Kiva Data
- 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
- 8.15. π€ Graphing Kiva Data with the Turtle
- 8.16. π€ Graphing Kiva Data with Altair
- 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. The Accumulator Pattern with Lists
- 9.11. The Accumulator Pattern with Strings
- 9.12. π©βπ» Accumulator Pattern Strategies
- 9.13. π©βπ» Donβt Mutate A List That You Are Iterating Through
- 9.14. Summary
- 9.15. Exercises
- 9.16. Chapter Assessment - List Methods
- 9.17. π€ Substitution Cipher
- 9.18. π€ Image Processing
- 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
- 10.16. π€ Exploring Common Words and SETI
- 10.17. π€ Monte Carlo Simulations
- 11. Dictionaries
- 11.1. Introduction: Dictionaries
- 11.2. Introduction: 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
- 11.13. π€ Converting Roman Numerals
- 11.14. π€ Stack Overflow Developer Survey
- 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. A function that accumulates
- 12.8. Variables and parameters are local
- 12.9. Global Variables
- 12.10. Functions can call other functions (Composition)
- 12.11. Flow of Execution Summary
- 12.12. π©βπ» Print vs. return
- 12.13. Passing Mutable Objects
- 12.14. Side Effects
- 12.15. Glossary
- 12.16. Exercises
- 12.17. Chapter Assessment
- 12.18. π€ Turtles and Strings and L-Systems
- 12.19. π€ Image Processing Revisited
- 12.20. π€ Predicting Pizza Prices - Linear Regression
- 12.21. π€ Build your own Google
- 13. Tuple Packing and Unpacking
- 14. More About Iteration
- 15. Advanced Functions
- 16. Sorting
- 17. Nested Data and Nested Iteration
- 18. Test Cases
- 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