Skip to main content
\( \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)
How to Think Like a Computer Scientist:
Interactive Edition
Contents
Index
Prev
Up
Next
Contents
Prev
Up
Next
1
General Introduction
The Way of the Program
Algorithms
The Python Programming Language
Executing Python in this Book
More About Programs
What is Debugging?
Syntax errors
Runtime Errors
Semantic Errors
Experimental Debugging
Formal and Natural Languages
A Typical First Program
Comments
Glossary
Exercises
2
Simple Python Data
Variables, Expressions and Statements
Values and Data Types
Type conversion functions
Variables
Variable Names and Keywords
Statements and Expressions
Operators and Operands
Input
Order of Operations
Reassignment
Updating Variables
Glossary
Exercises
3
Debugging Interlude 1
How to be a Successful Programmer
How to Avoid Debugging
Beginning tips for Debugging
Know Your Error Messages
Summary
Exercises
4
Python Turtle Graphics
Hello Little Turtles!
Our First Turtle Program
Instances — A Herd of Turtles
The
for
Loop
Flow of Execution of the for Loop
Iteration Simplifies our Turtle Program
The range Function
A Few More turtle Methods and Observations
Summary of Turtle Methods
Glossary
Exercises
5
Python Modules
Modules and Getting Help
More About Using Modules
The math module
The random module
Creating Modules
Glossary
Exercises
6
Functions
Functions
Functions that Return Values
Unit Testing
Variables and Parameters are Local
The Accumulator Pattern
Functions can Call Other Functions
Flow of Execution Summary
Using a Main Function
Program Development
Composition
A Turtle Bar Chart
Glossary
Exercises
7
Selection
Boolean Values and Boolean Expressions
Logical operators
Precedence of Operators
Conditional Execution: Binary Selection
Omitting the else Clause: Unary Selection
Nested conditionals
Chained conditionals
Boolean Functions
Glossary
Exercises
8
More About Iteration
Iteration Revisited
The
for
loop revisited
The
while
Statement
Randomly Walking Turtles
The 3n + 1 Sequence
Newton’s Method
The Accumulator Pattern Revisited
Other uses of
while
Algorithms Revisited
Simple Tables
2-Dimensional Iteration: Image Processing
Image Processing on Your Own
Glossary
Exercises
9
Strings
Strings Revisited
A Collection Data Type
Operations on Strings
Index Operator: Working with the Characters of a String
String Methods
Length
The Slice Operator
String Comparison
Strings are Immutable
Traversal and the
for
Loop: By Item
Traversal and the
for
Loop: By Index
Traversal and the
while
Loop
The
in
and
not in
operators
The Accumulator Pattern with Strings
Turtles and Strings and L-Systems
Looping and Counting
A
find
function
Optional parameters
Character classification
Summary
Glossary
Exercises
10
Lists
Lists
List Values
List Length
Accessing Elements
List Membership
Concatenation and Repetition
List Slices
Lists are Mutable
List Deletion
Objects and References
Aliasing
Cloning Lists
Repetition and References
List Methods
The Return of L-Systems
Append versus Concatenate
Lists and
for
loops
The Accumulator Pattern with Lists
Using Lists as Parameters
Pure Functions
Which is Better?
Functions that Produce Lists
List Comprehensions
Nested Lists
Strings and Lists
list
Type Conversion Function
Tuples and Mutability
Tuple Assignment
Tuples as Return Values
Glossary
Exercises
11
Files
Working with Data Files
Finding a File on your Disk
Reading a File
Iterating over lines in a file
Alternative File Reading Methods
Writing Text Files
With Statements
Fetching Something From The Web
Glossary
Exercises
12
Dictionaries
Dictionaries
Dictionary Operations
Dictionary Methods
Aliasing and Copying
Sparse Matrices
Glossary
Exercises
13
Exceptions
What is an exception?
Runetime Stack and
raise
command
Standard Exceptions
Principles for using Exceptions
Exceptions Syntax
The
finally
clause of the
try
statement
Glossary
Exercises
14
Web Applications
Web Applications
How the Web Works
How Web Applications Work
Web Applications and HTML Forms
Writing Web Applications With Flask
More About Flask
Input For A Flask Web Application
Web Applications With a User Interface
Glossary
15
GUI and Event Driven Programming
Graphical User Interfaces
Tkinter Standard Dialog Boxes
GUI Widgets
Layout Mangers
Widget Groupings
Command Events
Low-Level Event Processing
The Design of GUI Programs
Common Widget Properties
Timer Events
A Programming Example
Managing GUI Program Complexity
Exercises
Glossary
16
Recursion
What Is Recursion?
Calculating the Sum of a List of Numbers
The Three Laws of Recursion
Converting an Integer to a String in Any Base
Visualizing Recursion
Sierpinski Triangle
Glossary
Programming Exercises
Exercises
17
Classes and Objects - the Basics
Object-oriented programming
A change of perspective
Objects Revisited
User Defined Classes
Improving our Constructor
Adding Other Methods to our Class
Objects as Arguments and Parameters
Converting an Object to a String
Instances as Return Values
Glossary
Exercises
18
Classes and Objects - Digging a Little Deeper
Fractions
Objects are Mutable
Sameness
Arithmetic Methods
Glossary
Exercises
19
Inheritance
Pillars of OOP
Introduction to Inheritance
Extending
Reuse Through Composition
Class Diagrams
Composition vs. Inheritance
Case Study: Structured Postal Addresses
20
Unit Testing
Introduction: Unit Testing
Checking Assumptions With
assert
Testing Functions
Designing Testable Functions
Writing Unit Tests
Test-First Development
Testing with pytest
Glossary
Exercises
Back Matter
1
Operator precedence table
Index
Authored in PreTeXt
Chapter
2
Simple Python Data
2.1
Variables, Expressions and Statements
2.2
Values and Data Types
2.3
Type conversion functions
2.4
Variables
2.5
Variable Names and Keywords
2.6
Statements and Expressions
2.7
Operators and Operands
2.8
Input
2.9
Order of Operations
2.10
Reassignment
2.11
Updating Variables
2.12
Glossary
2.13
Exercises