Skip to main content
Foundations of Python Programming:
Functions First
Matthew Hrehirchuk, Eric Chalmers, Charlotte Curtis, Patrick Perri
Contents
Search Book
close
Search Results:
No results.
Prev
Up
Next
Scratch ActiveCode
Profile
Course Home
Assignments
Practice
Peer Instruction (Instructor)
Peer Instruction (Student)
Change Course
Instructor's Page
Progress Page
Edit Profile
Change Password
Log Out
Front Matter
1
Foreword to the Functions First Edition
2
Original Foreword
Preface
Acknowledgements
1
General Introduction
1.1
The Way of the Program
1.1.1
Learning Goals
1.1.2
Objectives
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.11
Glossary
2
Variables, Statements, and Expressions
2.1
Introduction
2.1.1
Learning Goals
2.1.2
Objectives
2.2
Values and Data Types
2.3
Operators and Operands
2.4
Function Calls
2.4.1
Function calls as part of complex expressions
2.4.2
Functions are objects; parentheses invoke functions
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.12.1
Developing your mental model of How Python Evaluates
2.13
Updating Variables
2.14
Hard-Coding
2.15
Input
2.16
Glossary
2.16
Glossary
2.17
Chapter Assessment
3
Debugging
3.1
Introduction to Debugging
3.1.1
Learning Goals
3.1.2
Objectives
3.2
Three kinds of Errors
3.3
Syntax errors
3.4
Runtime Errors
3.5
Semantic Errors
3.5.1
Test Cases
3.6
Know Your Error Messages
3.6.1
SyntaxError
3.6.2
TypeError
3.6.3
NameError
3.6.4
ValueError
3.7
Tips for Debugging
4
Functions
4.1
Introduction to Functions
4.1.1
Topics
4.1.2
Learning Objectives
4.2
Function Definition
4.3
Function Invocation
4.4
Function Parameters
4.5
Returning a value from a function
4.6
Decoding a Function
4.7
Type Annotations
4.8
Variables and parameters are local
4.9
Global Variables
4.10
Functions can call other functions (Composition)
4.11
Flow of Execution Summary
4.12
Print vs. return
4.13
Glossary
4.13
Glossary
4.14
Chapter Assessment
5
Conditionals
5.1
Intro: What we can do with Conditionals
5.1.1
Learning Goals
5.1.2
Objectives
5.2
Boolean Values and Boolean Expressions
5.3
Logical operators
5.4
The
in
and
not in
operators
5.5
Precedence of Operators
5.6
Conditional Execution: Binary Selection
5.7
Omitting the
else
Clause: Unary Selection
5.8
Nested conditionals
5.9
Chained conditionals
5.10
Setting Up Conditionals
5.10.1
Choosing your type of Conditional
5.11
Short-Circuit Evaluation
5.11.1
Smart Evaluation
5.12
Glossary
5.12
Glossary
6
Iteration
6.1
Introduction
6.1.1
Learning Goals
6.1.2
Objectives
6.2
The
while
Statement
6.3
The Listener Loop
6.3.1
Other uses of
while
6.3.1.1
Sentinel Values
6.3.1.2
Validating Input
6.4
Break and Continue
6.5
Infinite Loops
6.6
Chapter Assessment
7
More About 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
The Accumulator Pattern
7.6
Printing Intermediate Results
7.7
Naming Variables in For Loops
7.8
Glossary
7.8
Glossary
8
Sequences
8.1
Introduction: Sequences
8.1.1
Learning Goals
8.1.2
Objectives
8.2
Strings and Lists
8.2.1
Strings
8.2.2
Lists
8.2.3
Tuples
8.3
Index Operator: Working with the Characters of a String
8.3.1
Index Operator: Accessing Elements of a List or Tuple
8.4
Disambiguating []: creation vs indexing
8.5
Length
8.6
The Slice Operator
8.6.1
List Slices
8.6.2
Tuple Slices
8.7
Concatenation and Repetition
8.8
Count and Index
8.8.1
Count
8.8.2
Index
8.9
Splitting and Joining Strings
8.10
Chapter Assessment
9
Transforming Sequences
9.1
Introduction: Transforming Sequences
9.1.1
Learning Goals
9.1.2
Objectives
9.2
Mutability
9.2.1
Lists are Mutable
9.2.2
Strings are Immutable
9.2.3
Tuples are Immutable
9.3
List Element Deletion
9.4
Objects and References
9.5
Aliasing
9.6
Cloning Lists
9.7
Mutating Methods
9.7.1
List Methods
9.8
Append versus Concatenate
9.9
Non-mutating Methods on Strings
9.9.1
FStrings
9.10
The Accumulator Pattern with Lists
9.11
The Accumulator Pattern with Strings
9.12
Accumulator Pattern Strategies
9.12.1
When to Use it
9.12.2
Before Writing it
9.12.3
Choosing Good Accumulator and Iterator Variable Names
9.13
Don’t Mutate A List That You Are Iterating Through
9.14
Summary
9.14
Glossary
10
Nested Data and Nested Iteration
10.1
Introduction: Nested Data and Nested Iteration
10.1.1
Lists with Complex Items
10.2
Nested Dictionaries
10.3
Processing JSON results
10.4
Nested Iteration
10.5
Structuring Nested Data
10.6
Deep and Shallow Copies
10.7
Chapter Assessment
11
Files
11.1
Introduction: Working with Data Files
11.1.1
Learning Goals
11.1.2
Objectives
11.2
Reading a File
11.3
Alternative File Reading Methods
11.4
Iterating over lines in a file
11.5
Finding a File in your Filesystem
11.6
Using
with
for Files
11.7
Recipe for Reading and Processing a File
11.8
Writing Text Files
11.9
CSV Format
11.10
Reading in data from a CSV File
11.11
Writing data to a CSV File
11.12
Tips on Handling Files
11.13
Glossary
11.13
Glossary
11.14
Chapter Assessment
12
Exceptions
12.1
Handling Exceptions
12.1.1
When to use try/except
12.2
Standard Exceptions
12.3
Chapter Assessment
13
Defining your own Classes
13.1
Introduction: Classes and Objects - the Basics
13.1.1
Object-oriented programming
13.2
Objects Revisited
13.3
User Defined Classes
13.4
Adding Parameters to the Constructor
13.5
Adding Other Methods to a Class
13.6
Objects as Arguments and Parameters
13.7
Converting an Object to a String
13.8
Instances as Return Values
13.9
Class Variables and Instance Variables
13.10
Thinking About Classes and Instances
13.11
A Tamagotchi Game
13.12
Glossary
13.12
Glossary
13.13
Chapter Assessment
14
Additional Resources
14.1
The Command Line
14.1.1
GitBash
14.1.2
Commands
14.1.2.1
Navigation Commands
14.1.2.2
File and Directory Commands
14.2
Intro to Git
14.2.1
The History of Git
14.2.2
Repositories
14.2.3
Cloning a Repository
14.2.4
Staging and Committing
14.2.5
Pushing and Pulling
14.3
Advanced Git
14.3.1
Conflicts
14.3.2
Branching
14.3.3
Merging Branches
14.3.4
Rebasing
14.3.5
Stashing
14.4
Comprehensive Practice Program: Books and Bookshelves
14.4.1
Adding Some Books
14.4.2
Confirming Book Data
14.4.3
Searching for Books
14.4.4
Filling Up Bookshelves
14.4.5
Final Touches
Back Matter
A
GNU Free Documentation License
A.1
0. PREAMBLE
A.2
1. APPLICABILITY AND DEFINITIONS
A.3
2. VERBATIM COPYING
A.4
3. COPYING IN QUANTITY
A.5
4. MODIFICATIONS
A.6
5. COMBINING DOCUMENTS
A.7
6. COLLECTIONS OF DOCUMENTS
A.8
7. AGGREGATION WITH INDEPENDENT WORKS
A.9
8. TRANSLATION
A.10
9. TERMINATION
A.11
10. FUTURE REVISIONS OF THIS LICENSE
A.12
11. RELICENSING
B
Copyright Notice
🔗
Chapter
2
Variables, Statements, and Expressions
🔗
Last updated January 4, 2025.
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
Chapter Assessment