Skip to main content
Let’s Think Python:
COMSC020 PreTeXt Interactive Edition
x
Search Results:
No results.
☰
Contents
Index
You!
Choose avatar
▻
✔️
You!
😺
👤
👽
🐶
🐼
🌈
Font family
▻
✔️
Open Sans
AaBbCc 123 PreTeXt
Roboto Serif
AaBbCc 123 PreTeXt
Adjust font
▻
Size
12
Smaller
Larger
Width
100
narrower
wider
Weight
400
thinner
heavier
Letter spacing
0
/200
closer
f a r t h e r
Word spacing
0
/50
smaller gap
larger gap
Line Spacing
135
/100
closer
together
further
apart
Light/dark mode
▻
✔️
default
pastel
twilight
dark
midnight
Reading ruler
▻
✔️
none
underline
L-underline
grey bar
light box
sunrise
sunrise underline
Motion by:
✔️
follow the mouse
up/down arrows - not yet
eye tracking - not yet
<
Prev
^
Up
Next
>
🔍
\( \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \definecolor{fillinmathshade}{gray}{0.9} \newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}} \)
Front Matter
Change Log
Preface to the Computer Science 020 Edition
Preface to the First and Second Editions
Preface to the Interactive Edition
1
General Introduction
1.1
The Way of the Program
1.2
Algorithms
1.3
Computer Hardware Architecture
1.4
The Python Programming Language
1.5
Executing Python in this Book
1.6
Writing Assignments—The Thonny IDE
1.7
A Typical First Program
1.8
More About Programs
1.9
What is Debugging?
1.10
Syntax errors
1.11
Let’s Make Mistakes!
1.12
Runtime Errors
1.13
Semantic Errors
1.14
Experimental Debugging
1.15
Comments
1.16
Formal and Natural Languages
1.17
Glossary
1.17
Glossary
1.18
Exercises
2
Simple Python Data
2.1
Variables, Expressions and Statements
2.2
Values and Data Types
2.3
Variables
2.4
Variable Names, Keywords, and Constants
2.4.1
Variable Names and Keywords
2.4.2
Constants
2.5
Choosing Variable Names
2.6
Statements and Expressions
2.7
Operators and Operands
2.8
Planning a Program
2.9
Input
2.10
Type Conversion Functions
2.11
Using Input in a Program
2.12
Order of Operations
2.13
More About Strings
2.14
Reassignment
2.14.1
Developing your Mental Model of How Python Evaluates
2.15
Updating Variables
2.16
Updating Variables: Compound Assignment
2.17
Glossary
2.17
Glossary
2.18
Exercises
3
Debugging Interlude 1
3.1
How to be a Successful Programmer
3.2
How to Avoid Debugging
3.3
Beginning tips for Debugging
3.4
Know Your Error Messages
3.4.1
Introduction
3.4.2
ParseError
3.4.3
TypeError
3.4.4
NameError
3.4.5
ValueError
3.5
Summary
3.6
Exercises
4
Conditionals
4.1
Making Decisions
4.2
Conditional Execution: Binary Selection
4.3
Omitting the else Clause: Unary Selection
4.4
Logical operators
4.5
Logical operators—Advanced Topics
4.5.1
Logical Opposites
4.5.2
The De Morgan Laws
4.6
Precedence of Operators
4.7
Chained conditionals
4.8
Nested conditionals
4.9
“Short-circuit” Evaluation of Logical Expressions
4.10
Glossary
4.10
Glossary
4.11
Exercises
4.12
Mixed-up Code Questions
4.13
Write Code Questions
5
Loops and Iterations
5.1
What is Iteration?
5.2
The
while
statement
5.3
Using
while
5.4
“Do Nothing” and Infinite Loops
5.4.1
while
Loops That Do Nothing
5.4.2
Infinite Loops
5.4.3
The
break
Statement
5.5
Definite loops using
for
5.6
Loop patterns
5.6.1
Counting and summing loops
5.6.2
Maximum and Minimum Loops
5.7
The
range
Function
5.8
Debugging
5.9
Glossary
5.9
Glossary
5.10
Multiple Choice Questions
5.11
Write Code Questions
5.12
Group Work - Loops (For, Range, While)
5.12.1
for Statements
5.12.2
The range Function
5.12.3
while Statements
6
Functions
6.1
Function calls
6.2
Built-in functions
6.3
Type conversion functions
6.4
Math functions
6.4.1
Trigonometric Functions
6.4.2
Exercises
6.5
Random numbers
6.6
Writing new functions
6.7
Why Functions?
6.8
Repetitive Code
6.9
Eliminating Repetitive Code
6.10
Flow of execution
6.11
Value-Returning Functions and Void Functions
6.12
Docstrings
6.13
Debugging
6.14
Glossary
6.14
Glossary
6.15
Multiple Choice Questions
6.16
Mixed-up Code Questions
6.17
Write Code Questions
6.18
Group Work: Functions
6.18.1
Print and Function Basics
6.18.2
Parts of a Function and Function Calls
6.18.3
Writing Function Calls
6.18.4
Function Order
6.18.5
Special Characters and Keywords
7
Strings
7.1
Strings Revisited
7.2
A Collection Data Type
7.3
Operations on Strings
7.4
Index Operator: Working with the Characters of a String
7.5
Length
7.6
The Slice Operator
7.7
Objects and Methods
7.8
String Methods
7.9
Program: Changing the Form of a Name
7.10
Formatting Strings
7.10.1
f-strings
7.10.2
Controlling Decimal Digits
7.10.3
Advanced Topic: Aligning Output
7.10.4
Advanced Topic: Additional Formatting Codes
7.10.5
Exercises
7.11
String Comparison
7.12
Strings are Immutable
7.13
Traversal and the
for
Loop: By Item
7.14
Traversal and the
for
Loop: By Index
7.15
Traversal and the
while
Loop
7.16
The
in
and
not in
operators
7.17
The Accumulator Pattern with Strings
7.18
Looping and Counting
7.19
A
find
function
7.20
Optional parameters
7.21
Character Classification
7.22
Summary
7.22
Glossary
7.23
Glossary
7.23
Glossary
7.24
Exercises
8
Interlude: The Software Development Life Cycle
8.1
Developing Small Projects
8.2
Developing Large Projects
9
Lists
9.1
Lists
9.2
The Accumulator Pattern with Lists
9.2.1
Accumulating the Maximum Value
9.2.2
Accumulating a String Result
9.3
List Values
9.4
Accessing Elements
9.5
List Membership
9.6
Concatenation and Repetition
9.7
List Slices
9.8
Lists are Mutable
9.9
List Deletion
9.10
Programming Phase 1: Planning
9.11
Programming Phase 2: Debugging
9.12
Programming Phase 3: Fixing
9.12.1
The Problem
9.12.2
The Fix
9.12.3
The Moral of the Story
9.13
Objects, References, and Aliasing
9.14
Cloning Lists
9.15
List Methods
9.16
Interlude: The Scarcity Model
9.17
Deleting Negative Elements: The Rewrite
9.17.1
Advanced Topic: When to Update a List
9.18
Pure Functions
9.19
Which is Better?
9.20
Advanced Topic: List Comprehensions
9.20
Exercises
9.21
Nested Lists
9.21.1
Accessing Sublists and Sublist Elements
9.21.2
Check Your Understanding
9.21.3
Cloning Nested Lists
9.22
Working with Nested Lists
9.22
Exercises
9.23
Strings and Lists
9.24
Glossary
9.24
Glossary
9.25
Exercises
10
Files
10.1
Working with Data Files
10.2
Finding a File on your Disk
10.3
Opening a File
10.4
Iterating over lines in a file
10.5
Alternative File Reading Methods
10.6
Writing Text Files
10.7
Advanced Topic:
with
Statements
10.8
Glossary
10.8
Glossary
10.9
Exercises
11
Dictionaries
11.1
Dictionaries
11.2
Dictionary Operations
11.3
Dictionary Methods
11.4
Aliasing and Copying
11.5
Glossary
11.5
Glossary
11.6
Exercises
12
Exceptions
12.1
What is an Exception?
12.2
try
and
except
12.3
Multiple Exceptions
12.3.1
Advanced Topic: Unanticipated Exceptions
12.4
Principles for using Exceptions
12.4.1
Principle 1:
12.4.2
Principle 2:
12.4.3
Principle 3:
12.5
The
finally
clause of the
try
statement
12.6
Glossary
12.6
Glossary
13
Classes and Objects - the Basics
13.1
Object-oriented Programming
13.2
A change of perspective
13.3
Objects Revisited
13.4
The Turtle Object
13.5
User Defined Classes
13.6
Accessing Properties
13.7
Improving our Constructor
13.8
Adding Other Methods to our Class
13.9
Objects as Arguments and Parameters
13.10
Converting an Object to a String
13.11
Instances as Return Values
13.12
Glossary
13.12
Glossary
13.13
Exercises
Back Matter
1
Operator precedence table
Index
Exercises
3.6
Exercises
This page is intentionally blank (for now)