The University of Vermont, 2023. — 402 p. — ISBN: 9798988709206, 8988709209.
The goal of this book is to provide an introduction to computer programming with Python. This includes functional decomposition and a structured approach to programming, writing idiomatic Python, understanding the importance of abstraction, practical problem-solving exercises, and a brief introduction to plotting with Matplotlib.
When you get to know it, Python is a peculiar programming language. Much of what’s peculiar about Python is concealed by its seemingly simple syntax. This is part of what makes Python a great first language—and it’s fun!
The book is organized into chapters which roughly correspond to a week’s worth of material (with some deviations). Some chapters, particularly the first few, should be consumed at a rate of two a week. We present below a brief description of each chapter, followed by mention of some conventions used in the book.
The Chapter 2 provides some motivation for why programming languages are useful, and gives a general outline of how a program is executed by the Python interpreter. This chapter also introduces the two modes of using Python. The interactive mode allows the user to interact with the Python interpreter using the Python shell. Python statements and expressions are entered one at a time, and the interpreter evaluates or executes the code entered by the user. This is an essential tool for experimentation and learning the details of various language features. Script mode allows the user to write, save, and execute Python programs. This is convenient since in this mode we can save our work, and run it multiple times without having to type it again and again at the Python shell.
Functions are the single most important concept a beginning programmer can acquire. Functional decomposition is a crucial requirement of writing reliable, robust, correct code. This chapter explains why we use functions, how functions are defined, how functions are called, and how values are returned. We’ve tried to keep this “non-technical” and so there’s no discussion of a call stack, though there is discussion of scope. Because beginning programmers often introduce side effects into functions where they are undesirable or unnecessary, this chapter makes clear the distinction between pure functions (those without side effects) and impure functions (those with side effects, including mutating mutable objects).
This book has been written for use in University of Vermont’s CS1210 Introduction to Programming (formerly CS021). This is a semester long course which covers much of the basics of programming, and an introduction to some fundamental concepts in computer science. Not being happy with any of the available textbooks, s, I endeavored to write my own.
Introduction.
Programming and the Python Shell.Why learn a programming language?
Compilation and interpretation.
The Python shell.
Hello, Python!
Syntax and semantics.
Introduction to binary numbers.
Exercises.
Types and literals.What are types?
Dynamic typing.
Types and memory.
More on string literals.
Representation error of numeric types.
Exercises.
Variables, statements, and expressions.Variables and assignment.
Expressions.
Augmented assignment operators.
Euclidean or «floor» division.
Modular arithmetic.
Exponentiation.
Exceptions.
Exercises.
Functions.Introduction to functions.
A deeper dive into functions.
Passing arguments to a function.
Scope.
Pure and impure functions.
The math module.
Exceptions.
Exercises.
Style.The importance of style.
PEP 8.
Whitespace.
Names (identifiers).
Line length.
Constants.
Comments in code.
Exercises.
Console I/O.Motivation.
Command line interface.
The input() function.
Converting strings to numeric types.
Some ways to format output.
Python f-strings and string interpolation.
Format specifiers.
Scientific notation.
Formatting tables.
Example: currency converter.
Format specifiers: a quick reference.
Exceptions.
Exercises.
Branching and Boolean expressions.Boolean logic and Boolean expressions.
Comparison operators.
Branching.
if, elif, and else.
Truthy and falsey.
Input validation.
Some string methods.
Flow charts.
Decision trees.
Exercises.
Structure, development, and testing.main the Python way.
Program structure.
Iterative and incremental development.
Testing your code.
The origin of the term «bug».
Using assertions to test your code.
Rubberducking.
Exceptions.
Exercises.
Sequences.Lists.
Tuples.
Mutability and immutability.
Subscripts are indices.
Concatenating lists and tuples.
Copying lists.
Finding an element within a sequence.
Sequence unpacking.
Strings are sequences.
Sequences: a quick reference guide.
Slicing.
Passing mutables to functions.
Exceptions.
Exercises.
Loops and iteration.Loops: an introduction.
while loops.
Input validation with while loops.
An ancient algorithm with a while loop.
for loops.
Iterables.
Iterating over strings.
Calculating a sum in a loop.
Loops and summations.
Products.
enumerate().
Tracing a loop.
Nested loops.
Stacks and queues.
A deeper dive into iteration in Python.
Exercises.
Randomness, games, and simulations.The random module.
Pseudo-randomness in more detail.
Using the seed.
Exercises.
File I/O.Context managers.
Reading from a file.
Writing to a file.
Keyword arguments.
More on printing strings.
The CSV module.
Exceptions.
Exercises.
Data analysis and presentation.Some elementary statistics.
Python's statistics module.
A brief introduction to plotting with Matplotlib.
The basics of Matplotlib.
Exceptions.
Exercises.
Exception handling.Exceptions.
Handling exceptions.
Exceptions and flow of control.
Exercises.
Dictionaries.
Introduction to dictionaries.
Iterating over dictionaries.
Deleting dictionary keys.
Hashables.
Counting letters in a string.
Exceptions.
Exercises.
Graphs.Introduction to graphs.
Searching a graph: breadth-first search.
Exercises.
Appendices.
Glossary.
Mathematical notation.
pip and venv.
File systems.
Code for cover artwork.
Index.