COMP 1000: Think Like a Computer¶
Welcome to COMP 1000!
This course is designed to introduce people to programming, which is a fundamental skill within Computer Science. This text is called ‘Think Like a Computer’ because the goal is for you to think through and understand how a computer will run the lines of code in a program, whether it’s a program you wrote or a program someone else wrote. At the same time as learning about programming and how to think like a computer, we will also begin right away by thinking about people: the end users who interact with the programs you write. And when we think about people as end users, we must think about ethics, because when people interact with computers both good and bad things can result, and it’s our responsibility to consider these possibilities. Thus, this course combines Programming, Interactivity, and Ethics: PIE!
Table of Contents¶
- 1. General Introduction
- 1.1. Foreword to PIE Edition
- 1.2. Introduction: The Way of the Program
- 1.3. Problem Solving, Interactivity & Ethics
- 1.4. The Python Programming Language
- 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. 🐢 Hello Little Turtles!
- 1.12. 🐢 Our First Turtle Program
- 1.13. Glossary
- 1.14. 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 2 Assessment
- 3. Debugging and Modules
- 3.1. Introduction to Debugging
- 3.2. 👩💻 Debugging
- 3.3. Syntax errors
- 3.4. Runtime Errors
- 3.5. Semantic Errors
- 3.6. 👩💻 Know Your Error Messages
- 3.7. Introduction to Python Modules
- 3.8. Modules
- 3.9. Ethics & Code Ownership
- 3.10. The
random
module - 3.11. 🐢 Exploring the Turtle Module
- 3.12. Instances: A Herd of Turtles
- 3.13. Object Oriented Concepts
- 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.18. Chapter Assessment
- 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: Working with the Characters of a String
- 4.6. Strings and
for
loops - 4.7. Lists and
for
loops - 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.14. Nested Iteration: Image Processing
- 4.15. Glossary
- 4.16. Exercises
- 4.17. Chapter Assessment
- 5. Functions and Files
- 5.1. Introduction to Functions
- 5.2. Function Definition
- 5.3. Function Invocation
- 5.4. Ethics: Documenting Function Assumptions
- 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.14. Exercises
- 5.15. Chapter Assessment
- 6. Conditionals
- 6.1. Intro: What we can do with Turtles and Conditionals
- 6.2. Boolean Values and Boolean Expressions
- 6.3. Logical operators
- 6.4. The
in
andnot 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.11. 👩💻 Setting Up Conditionals
- 6.12. Glossary
- 6.13. Exercises
- 6.14. Chapter Assessment
- 7. Integration Pause
- 8. Function Parameters, Return Values and Scope
- 8.1. Introduction: Functions Part II
- 8.2. Function Parameters
- 8.3. Return Values
- 8.4. 👩💻 Decoding a Function
- 8.5. Type Annotations
- 8.6. A function that accumulates
- 8.7. Scope - Local vs 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.12. Listener Functions with Parameters
- 8.13. Glossary
- 8.14. Exercises
- 8.15. Chapter Assessment
- 9. Sequences
- 9.1. Introduction: Sequences
- 9.2. Strings, Lists and Tuples
- 9.3. Immutable Strings
- 9.4. Disambiguating []: creation vs indexing
- 9.5. Length
- 9.6. The Slice Operator
- 9.7. Concatenation and Repetition
- 9.8. Count and Index Methods
- 9.9. Splitting and Joining Strings
- 9.10. Passing Mutable vs Immuatable Objects
- 9.11. Mutable Object Side Effects
- 9.12. Exercises
- 9.13. Chapter Assessment
- 10. Event Handling
- 10.1. Event-Based Programming
- 10.2. Program Architecture
- 10.3. Building a Graphical User Interface (GUI)
- 10.4. The SimpleGUI Module
- 10.5. Adding GUI Controls
- 10.6. Event Handlers
- 10.7. SimpleGUI Events
- 10.8. A Complete Event-Based GUI Example
- 10.9. Event-Based Programming Made Elegant
- 10.10. Default Bias
- 11. Indefinite Iteration
- 12. Labs
- 13. Contributor List
- 14. Copyright Notice