Skip to main content
Computer Science for STEM:
The PreTeXt Interactive Edition
Contents
Index
Search Book
close
Search Results:
No results.
dark_mode
Dark Mode
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
chevron_left
Preface to the First and Second Editions
Preface to the Interactive Edition
1
General Introduction
chevron_left
1.1
The Way of the Program
1.2
Algorithms
1.3
The Python Programming Language
1.4
Executing Python in this Book
1.5
More About Programs
1.6
What is Debugging?
1.7
Syntax errors
1.8
Runtime Errors
1.9
Semantic Errors
1.10
Experimental Debugging
1.11
A Typical First Program
1.12
Comments
1.13
Formal and Natural Languages
1.13
Glossary
1.13
Glossary
1.14
Glossary
1.14
Glossary
2
Simple Python Data
chevron_left
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.10.1
Developing your mental model of How Python Evaluates
2.11
Updating Variables
2.12
Glossary
2.12
Glossary
2.13
Practice Video Lecture
3
Strings
chevron_left
3.1
Strings Revisited
3.2
A Collection Data Type
3.3
Operations on Strings
3.4
Index Operator: Working with the Characters of a String
3.5
Length
3.6
The Slice Operator
3.7
Strings are Immutable
3.8
String Methods
3.9
F-Strings
3.10
Character classification
3.11
Glossary
3.11
Glossary
3.12
Some Extra String Methods (Optional)
4
Lists
chevron_left
4.1
Lists
4.2
List Values
4.3
List Length
4.4
Accessing Elements
4.5
Concatenation and Repetition
4.6
List Slices
4.7
Cloning Lists
4.8
Lists are Mutable
4.9
List Deletion
4.10
List Methods
4.11
Append versus Concatenate
4.12
Strings and Lists
4.13
list
Type Conversion Function
4.14
None
4.15
Glossary
4.15
Glossary
4.16
Practice Video Lecture
5
Branching
chevron_left
5.1
Boolean Values and Boolean Expressions
5.2
Conditional Execution: Binary Selection
5.3
Omitting the else Clause: Unary Selection
5.4
Nested conditionals
5.5
Chained conditionals
5.6
The
in
and
not in
operators
5.7
List Membership
5.8
String Comparison
5.9
Practice Video Lecture
5.10
Logical operators
5.10.1
Logical Opposites
5.11
Precedence of Operators
5.12
Pseudocode
5.13
Glossary
5.13
Glossary
5.14
Practice Video Lecture
6
Debugging Interlude
chevron_left
6.1
How to be a Successful Programmer
6.2
How to Avoid Debugging
6.3
Beginning tips for Debugging
6.4
Know Your Error Messages
6.4.1
ParseError
6.4.2
TypeError
6.4.3
NameError
6.4.4
ValueError
6.5
Summary
7
Iteration
chevron_left
7.1
Iteration Introduction
7.2
The
while
Statement
7.3
Flow of Execution of the while Loop
7.4
break
and
continue
7.5
Traversal and the
while
Loop
7.6
Other uses of
while
7.6.1
Sentinel Values
7.6.2
Validating Input
7.7
The 3n + 1 Sequence
7.8
Practice Video Lecture
7.9
The
for
loop
7.10
The range Function
7.11
Traversing lists with the
for
loop
7.12
Traversing strings with the
for
Loop: By Item
7.13
Traversing strings with the
for
Loop: By Index
7.14
Glossary
7.14
Glossary
7.15
Practice Video Lecture
8
Functions
chevron_left
8.1
Functions
8.2
Functions that Return Values
8.3
Variables and Parameters are Local
8.4
The Accumulator Pattern
8.4.1
The General Accumulator Pattern
8.4.2
A Variation on the Accumulator Pattern
8.5
Functions can Call Other Functions
8.6
Composition
8.7
Flow of Execution Summary
8.8
Using a Main Function
8.9
Unit Testing
8.9.1
assert
with
for
loops
8.9.2
Return Value Tests
8.10
Boolean Functions
8.10.1
More Unit Testing
8.11
Program Development
8.12
Glossary
8.12
Glossary
9
Python Modules
chevron_left
9.1
Modules and Getting Help
9.2
More About Using Modules
9.3
The random module
9.4
Creating Modules
9.5
Packages and Dependencies
9.6
Glossary
9.6
Glossary
9.7
Practice Video Lecture
10
Complex Logic
chevron_left
10.1
Complex Logic Introduction
10.2
Nested Lists
10.3
Nested List Traversal
10.4
A
find
function
10.5
The Accumulator Pattern with Lists
10.5.1
Accumulating the Max Value
10.5.2
Accumulating a String Result
10.6
The Accumulator Pattern with Strings
10.7
Objects and References
10.8
Using Lists as Parameters
10.9
Practice Video Lecture
11
Files
chevron_left
11.1
Working with Data Files
11.2
Finding a File on your Disk
11.3
With Statements
11.4
Iterating over lines in a file
11.5
Alternative File Reading Methods
11.6
Writing Text Files
11.7
Glossary
11.7
Glossary
11.8
Practice Video Lecture
12
Recursion
chevron_left
12.1
What Is Recursion?
12.2
Calculating the Sum of a List of Numbers
12.3
The Three Laws of Recursion
12.4
Converting an Integer to a String in Any Base
12.5
Glossary
12.5
Glossary
12.6
Practice Video Lecture
13
Classes and Objects - the Basics
chevron_left
13.1
Object-oriented programming
13.2
A change of perspective
13.3
Objects Revisited
13.4
User Defined Classes
13.5
Improving our Constructor
13.6
Adding Other Methods to our Class
13.7
Objects as Arguments and Parameters
13.8
Converting an Object to a String
13.9
Instances as Return Values
13.10
Glossary
13.10
Glossary
14
Classes and Objects - Digging a Little Deeper
chevron_left
14.1
Fractions
14.2
Objects are Mutable
14.3
Sameness
14.4
Arithmetic Methods
14.5
Glossary
14.5
Glossary
14.6
Practice Video Lecture
15
Additional Topics
chevron_left
15.1
Binary Representations
15.2
Big O Analysis
15.3
Big O Simplification and Practice
15.4
Data Science
15.5
Search and Sort Algorithms
15.6
Dictionaries
15.7
Dictionary Operations
15.8
Dictionary Methods
15.9
Aliasing and Copying
15.10
List Comprehensions
15.11
Dictionary Comprehensions
15.12
Glossary
15.12
Glossary
15.13
Practice Video Lecture
Back Matter
chevron_left
1
Operator precedence table
Index
Chapter
11
Files
11.1
Working with Data Files
11.2
Finding a File on your Disk
11.3
With Statements
11.4
Iterating over lines in a file
11.5
Alternative File Reading Methods
11.6
Writing Text Files
11.7
Glossary
11.8
Practice Video Lecture
🔗