Skip to main content
COMP 1000: Think Like a Computer:
The PreTeXt Interactive Edition
Contents
Index
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
1
General Introduction
1.1
Forewords
1.1.1
Foreword to PIE Edition
1.1.2
Original Foreword
1.1.3
Preface
1.2
Introduction: The Way of the Program
1.2.1
Learning Goals
1.2.2
Objectives
1.2.3
Get in the Learning Zone
1.3
Problem Solving, Interactivity & Ethics
1.3.1
Problem Solving with Algorithms
1.3.2
Interactivity - Programming for People
1.3.3
Ethics - Programming Responsibly
1.4
The Python Programming Language
1.4.1
1.4.2
ActiveCode in Runestone
1.4.3
CodeLens in Runestone
1.5
More About Programs
1.5.1
Program Sequencing
1.5.2
Preview of Control Structures
1.6
Formal and Natural Languages
1.6
Glossary
1.6
Glossary
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
🐢 Hello Little Turtles!
1.11.1
Interactivity & Turtles
1.12
🐢 Our First Turtle Program
1.13
Glossary
1.13
Glossary
1.14
Chapter Assessment
2
Variables, Statements, and Expressions
2.1
Introduction
2.1.1
Python Building Blocks
2.1.2
Learning Goals
2.1.3
Objectives
2.2
Values and Data Types
2.3
Operators and Operands
2.4
Function Calls
2.4.1
Calling Functions in Python
2.4.2
Function calls as part of complex expressions
2.4.3
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
Updating Variable References
2.12.2
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
Exercises
2.18
Chapter 2 Assessment
3
Debugging and Modules
3.1
Introduction to Debugging
3.1.1
Learning Goals
3.1.2
Objectives
3.2
👩💻 Debugging
3.2.1
How to Avoid Debugging
3.2.2
👩💻 Beginning tips for Debugging
3.2.3
Types of Errors
3.3
Syntax errors
3.4
Runtime Errors
3.5
Semantic Errors
3.5.1
3.5.2
Test Cases
3.6
👩💻 Know Your Error Messages
3.6.1
3.6.2
SyntaxError
3.6.3
TypeError
3.6.4
NameError
3.6.5
ValueError
3.7
Introduction to Python Modules
3.7.1
Learning Goals
3.7.2
Objectives
3.8
Modules
3.8.1
Importing Modules
3.8.2
Syntax for Importing Modules and Functionality
3.9
Ethics & Code Ownership
3.10
The
random module
3.10.1
Motivation for Randomness
3.10.2
Randomness and Ethics
3.11
🐢 Exploring the
turtle module
3.12
Instances: A Herd of Turtles
3.13
Object Oriented Concepts
3.13.1
User-defined Classes
3.13.2
Instances
3.13.3
Attributes
3.13.4
Methods
3.14
A Few More
turtle
Methods and Observations
3.15
👩💻 Common
turtle
Errors
3.16
Ethics: Variable Defaults Embed Values
3.17
Glossary
3.17
Glossary
3.18
Chapter Assessment
3.18.1
Drawing with Turtles
3.18.2
Drawing with Turtles
3.18.3
Debugging & Modules
4
Iteration
4.1
Introduction: Iteration
4.2
The
for
Loop
4.3
Turtle Repetition
4.4
Flow of Execution of the for Loop
4.5
Index Operator
4.5.1
Working with the Characters of a String
4.5.2
Index Operator: Accessing Elements of a List
4.6
Strings and
for
loops
4.7
Lists and
for
loops
4.7.1
A simple for loop example
4.7.2
Using the range Function to Generate a Sequence to Iterate Over
4.7.3
Iteration Simplifies our Turtle Program
4.8
👩💻 Naming Variables in For Loops
4.9
The Accumulator Pattern
4.10
👩💻 Printing Intermediate Results
4.11
Traversal and the
for
Loop: By Index
4.12
👩💻 Keeping Track of Your Iterator Variable and Your Iterable
4.13
The Power of Iteration
4.13.1
Large Sets of Data
4.14
Nested Iteration: Image Processing
4.14.1
The RGB Color Model
4.14.2
Image Objects
4.14.3
Image Processing and Nested Iteration
4.15
Glossary
4.15
Glossary
4.16
Exercises
4.17
Chapter Assessment
5
Functions and Files
5.1
Introduction to Functions and Files
5.1.1
Introduction to Functions
5.1.2
Introduction to Files
5.1.3
Learning Goals
5.1.4
Objectives
5.2
Function Definition
5.3
Function Invocation
5.4
Ethics: Documenting Function Assumptions
5.4.1
Motivating function documentation
5.4.2
Function Pre-Conditions
5.4.3
Function Post-Conditions
5.5
Listener Functions
5.6
Working with Data Files
5.7
Reading a File
5.8
Alternative File Reading Methods
5.9
Iterating over lines in a file
5.10
Finding a File in your Filesystem
5.11
Writing Text Files
5.12
👩💻 Tips on Handling Files
5.13
Glossary
5.13
Glossary
5.14
Exercises
5.15
Chapter Assessment
6
Conditionals
6.1
Intro: What we can do with Turtles and Conditionals
6.1.1
6.1.2
Learning Goals
6.1.3
Objectives
6.2
Boolean Values and Boolean Expressions
6.3
Logical operators for conditional evaluation
6.3.1
And, Or, Not
6.3.2
Smart Evaluation
6.4
The
in
and
not in
operators
6.5
Precedence of Operators
6.6
Conditional Execution: Binary Selection
6.7
Omitting the
else
Clause: Unary Selection
6.8
Nested conditionals
6.9
Chained conditionals
6.10
The Accumulator Pattern with Conditionals
6.10.1
Accumulating Specific Items
6.10.2
Accumulating the Max Value
6.11
👩💻 Setting Up Conditionals
6.11.1
Choosing your type of Conditional
6.12
Glossary
6.12
Glossary
6.13
Exercises
6.14
Chapter Assessment
7
Integration Pause
7.1
Integrating Concepts
7.1.1
7.1.2
Topics
7.2
Combining Iterations and Functions
7.2.1
Code Re-Use
7.2.2
Invoking Functions Inside Loops
7.2.3
Iterating Inside Functions
7.3
Combining Iterations and Conditionals
7.3.1
Iteration inside Conditional Blocks
7.3.2
Conditional Execution inside Iteration Blocks
7.4
Combining Conditionals and Functions
7.4.1
Conditional Branching inside Functions
7.4.2
Calling Functions from Conditional Blocks
7.5
Combining Iterations, Conditionals and Functions
7.6
Integration Practice
8
Function Parameters, Return Values and Scope
8.1
Introduction: Functions Part II
8.1.1
8.1.2
Topics
8.1.3
Learning Objectives
8.2
Function Parameters
8.2.1
A Black Box Diagram
8.2.2
Parameter Order and Type
8.3
Returning Value and Calling Functions in Expressions
8.3.1
Return Values
8.3.2
Functions as Part of Expressions
8.4
👩💻 Decoding a Function
8.5
Type Annotations
8.6
A function that accumulates
8.7
Scope - Local vs Global Variables
8.7.1
Variable and parameters are local
8.7.2
Global Variables
8.8
Function Composition (functions calling functions)
8.9
Flow of Execution Summary
8.10
👩💻 Print vs. return
8.11
Side Effects
8.11.1
What is a side effect?
8.11.2
Documenting Side Effects
8.12
Listener Functions with Parameters
8.13
Glossary
8.13
Glossary
8.14
Exercises
8.15
Chapter Assessment
9
Sequences
9.1
Introduction: Sequences
9.1.1
Learning Goals
9.1.2
Objectives
9.2
Strings, Lists and Tuples
9.2.1
Strings
9.2.2
Lists
9.2.3
Tuples
9.2.4
Index Operator: Accessing Elements of a String, List or Tuple
9.2.5
Adding and Removing Items from Lists
9.3
Immutable Strings
9.3.1
Demonstration of immutability of Strings
9.3.2
String methods return a new string
9.4
Disambiguating []: creation vs indexing
9.5
Length
9.6
The Slice Operator
9.6.1
Slicing Strings
9.6.2
List Slices
9.6.3
Tuple Slices
9.7
Concatenation and Repetition
9.7.1
Concatenation
9.7.2
Repetition
9.8
Count and Index Methods
9.8.1
Count
9.8.2
Index
9.9
Splitting and Joining Strings
9.10
Passing Mutable vs Immuatable Objects
9.10.1
Editing local values
9.10.2
Passing Lists
9.10.3
Passing Tuples
9.10.4
Ethics & Data Protection
9.11
Mutable Object Side Effects
9.11.1
Mutable objects can change when passed to functions
9.11.2
Side Effects in Interactive Programs
9.12
Exercises
9.13
Chapter Assessment
10
Event Handling
10.1
Event-Based Programming
10.1.1
Topics
10.1.2
Learning Objectives
10.2
Program Architecture
10.2.1
Batch Programming
10.2.2
Event-Based Programming
10.3
Building a Graphical User Interface (GUI)
10.3.1
What’s in a GUI?
10.3.2
GUI Toolkits
10.4
The SimpleGUI Module
10.5
Using Toolkit Documentation
10.6
Adding GUI Controls
10.6.1
Adding Labels
10.6.2
Adding Buttons
10.6.3
Adding Text Input Boxes
10.7
Event Handlers
10.7.1
UI Widget Event Handlers
10.7.2
Window-Based Input Event Handlers
10.7.3
Registering Event Handlers
10.8
Events - Mouse and Keyboard
10.8.1
Mouse Events
10.8.2
Keyboard Events
10.9
A Complete Event-Based GUI Example
10.9.1
Walking through the complete example
10.9.2
Practice Exercises
10.9.3
Global Variables in Event-Based Programs
10.10
Event-Based Programming Made Elegant
10.10.1
A Simple Class Example
10.10.2
The Event-Based GUI Example - OO Style
10.11
Default Bias
10.11.1
When Default Becomes Standard
10.11.2
Checkboxes, Radio Buttons, Dropdowns…oh my!
10.12
Glossary
10.12
Glossary
11
Indefinite Iteration
11.1
Introduction
11.1.1
Learning Goals
11.1.2
Objectives
11.2
The
while
Statement
11.3
The Input Listener Loop
11.3.1
Indefinite iteration built-in to the turtle module
11.3.2
Other uses of
while
11.3.2.1
Sentinel Values
11.3.2.2
Validating Input
11.4
Indeterminate Turtles
11.5
Break and Continue
11.6
👩💻 Infinite Loops
11.7
Glossary
11.7
Glossary
11.8
Exercises
11.9
Chapter Assessment
12
Labs
12.1
General Lab Instructions
12.1.1
Venue
12.1.2
Getting Help From Labmates
12.1.3
Grading
12.1.4
Formatting/Commenting
12.2
Week 1 Lab
12.2.1
Level 1 - Show IDLE is working on your computer
12.2.2
Level 2 - Write a turtle program
12.2.3
Level 3 - Type turtle program into IDLE
12.3
Week 2 Lab
12.3.1
Level 1
12.3.2
Level 2
12.3.3
Level 3
12.4
Week 3 Lab
12.4.1
Level 1
12.4.2
Level 2
12.4.3
Level 3
12.5
Week 4 Lab
12.5.1
Level 1
12.5.2
Level 2
12.5.3
Level 3
12.6
Week 5 Lab
12.6.1
Level 1
12.6.2
Level 2
12.6.3
Level 3
12.7
Week 6 Lab
12.7.1
Level 1
12.7.2
Level 2
12.7.3
Level 3
12.8
Week 7 Lab
12.8.1
Level 1
12.8.2
Level 2
12.8.3
Level 3
12.9
Week 8 Lab
12.9.1
Level 1
12.9.2
Level 2
12.9.3
Level 3
12.10
Week 9 Lab
12.10.1
Level 1
12.10.2
Level 2
12.10.3
Level 3
12.11
Week 10 Lab
12.11.1
Level 1 - Create a stopwatch
12.11.2
Level 2 - Part one of target shooting game
12.11.3
Level 3 - Complete target shooting game
12.12
Week 11 Lab
12.12.1
Level 1
12.12.2
Level 2
12.12.3
Level 3
1
Contributor List
1.1
PIE Edition
1.2
Second Edition
1.3
First Edition
2
Copyright Notice
Back Matter
Index
🔗
Section
1
Contributor List
1.1
PIE Edition
1.2
Second Edition
1.3
First Edition