Introduction

Published

2023-08-17

The goal of this book is to provide an introduction to computer programming with Python. This includes

When you get to know it, Python is a peculiar programming language.1 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!

Organization of this book

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.

Programming and the Python shell

This chapter 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.

This chapter also includes a brief introduction to binary numbers and binary arithmetic.

Types and literals

The concept of type is one of the most important in all computer science (indeed there’s an entire field called “type theory”).

This chapter introduces the most commonly used Python types, though in some cases, complete presentation of a given type will come later in the text—lists and dictionaries, for example. Other types are introduced later in the text (for example, function, range, enumerate, etc.). As types are introduced, examples of literals of each type are given.

Since representation error is a common cause of bewilderment among beginners, there is some discussion of why this occurs with float objects.

Variables, statements, and expressions

This chapter introduces much of the machinery that will be used throughout the remainder of the text: variables, assignment, expressions, operators, and evaluation of expressions.

On account of its broad applicability, a substantial account of modular arithmetic is presented as well.

Functions

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).

Because the math module is so widely used and includes many useful functions, we introduce the math module in this chapter. In this way, we also reinforce the idea of information hiding and good functional design. Do we need to know how the math module implements its sqrt() function? Of course not. Should we have to know how a function is implemented in order to use it? Apart from knowing what constitutes a valid input and what it returns as an output, no, we do not!

Style

Our goal here is to encourage the writing of idiomatic Python. Accordingly, we address the high points of PEP 8—the de facto style guide for Python—and provide examples of good and bad style.

Students don’t always understand how important style is for the readability of one’s code. By following style guidelines we can reduce the cognitive load that’s required to read code, thereby making it easier to reason about and understand our code.

Console I/O (input/output)

This chapter demonstrates how to get input from the user (in command line programs) and how to format output using f-strings. Because f-strings have been around so long now, and because they allow for more readable code, we’ve avoided presentation of older, and now seldom used, C-style string formatting.

Branching

Branching is a programming language’s way of handling conditional execution of code. In this chapter, we cover conditions (Boolean expressions) which evaluate to a true or false (or a value which is “truthy” or “falsey”—like true or like false). Python uses these conditions to determine whether a block of code should be executed. In many cases we have multiple branches—multiple paths of execution that might be taken. These are implemented with if, elif (a portmanteau of “else if”), and often else.

One common confusion that beginners face is understanding which branch is executed in an if/elif/else structure, and hopefully the chapter makes this clear.

Also covered are nested if statements, and two ways of visually representing branching (each appropriate to different use cases)—decision trees and flow charts.

Structure, development, and testing

Beginners often struggle with how to structure their code—both for proper flow of execution and for readability. This chapter gives clear guidelines for structuring code based on common idioms in Python. It also addresses how we can incrementally build and test our code.

Unlike many introductory texts, we present assertions in this chapter. Assertions are easy to understand and their use has great pedagogical value. In order to write an assertion, a programmer must understand clearly what behavior or output is expected for a given input. Using assertions helps you reason about what should be happening when your code is executed.

Sequences

Sequences—lists, tuples, and strings—are presented in this chapter. It makes sense to present these before presenting loops for two reasons. First, sequences are iterable, and as such are used in for loops, and without a clear understanding of what constitutes an iterable, understanding such loops may present challenges. Second, we often do work within a loop which might involve constructing or filtering a list of objects.

Common features of sequences—for example, they are all indexed, support indexed reads, and are iterable—are highlighted throughout the chapter.

As this chapter introduces our first mutable type, the Python list, we present the concepts of mutability and immutability in this chapter.

Loops

Loops allow for repetitive work or calculation. In this chapter we present the two kinds of loop supported by Python—while loops and for loops. At this point, students have seen iterables (in the form of sequences) and Boolean expressions, which are a necessary foundation for a proper presentation of loops.

Also, this chapter introduces two new types—range and enumerate—and their corresponding constructors. Presentation of range entails discussion of arithmetic sequences, and presentation of enumerate works nicely with tuple unpacking (or more generally, sequence unpacking), and so these are presented first in this chapter.

This chapter also provides a brief introduction to stacks and queues, which are trivially implemented in Python using list as an underlying data structure.

I’ve intentionally excluded treatment of comprehensions since beginners have difficulty reading and writing comprehensions without a prior, solid foundation in for loops.

Randomness, games, and simulations

There are many uses for randomness. Students love to write programs which implement games, and many games involve some chance element or elements—rolling dice, spinning a wheel, tossing a coin, shuffling a deck, and so on. Another application is in simulations, which may also include some chance elements. All manner of physical and other phenomena can be simulated with some randomness built in.

This chapter presents Python’s random module, and some of the more commonly used methods within this module—random.random(), random.randint(), random.choice(), and random.shuffle(). Much of this is done within the context of games of chance, but we also include some simulations (for example, random walk and Gambler’s Ruin). There is also some discussion of pseudo-random numbers and how Python’s pseudo-random number generator is seeded.

File I/O (input/output)

This chapter shows you how to read data from and write data to a file. File I/O is best accomplished using a context manager. Context managers were introduced with Python 2.5 in 2006, and are a much preferred idiom (as compared to using try/finally). Accordingly, all file I/O demonstrations make use of context managers created with the Python keyword with.

Because so much data is in CSV format (or can be exported to this format easily), we introduce the csv module in this chapter. Using the csv module reduces some of the complexity we face when reading data from a file, since we don’t have to parse it ourselves.

Exception handling

In this chapter, we present simple exception handling (using try/except, but not finally), and explain that some exceptions should not be handled since in doing so, we can hide programming defects which should be corrected. We also demonstrate the use of exception handling in input validation. When you reach this chapter, you’ll already have seen while loops for input validation, so the addition of exception handling represents only an incremental increase in complexity in this context.

Data analysis and presentation

This chapter is motivated in large part by the University of Vermont’s QD (quantitative and data literacy) designation for the course for which this textbook was written. Accordingly, we present some very basic descriptive statistics and introduce Python’s statistics module including statistics.mean(), statistics.pstdev(), and statistics.quantiles(). The presentation component of this chapter is done using Matplotlib, which is the de facto standard for plotting and visualization with Python. This covers only the rudiments of Matplotlib’s Pyplot interface (line plot, bar plot, etc.), and is not intended as a complete introduction.

Dictionaries

Dictionaries are the last new type we present in the text. Dictionaries store information using a key/value model—we look up values in a dictionary by their keys. Like sequences, dictionaries are iterable, but since they have keys rather than indices, this works a little differently. We’ll see three different ways to iterate over a dictionary.

We’ll also learn about hashability in the context of dictionary keys.

Graphs

Since graphs are so commonplace in computer science, it seems appropriate to include a basic introduction to graphs in this text. Plus, graphs are really fun!

A graph is a collection of vertices (also called nodes) and edges, which connect the vertices of the graph. The concrete example of a highway map is used, and an algorithm for breadth-first search (BFS) is demonstrated. Since queues were introduced in chapter 11, the conceptual leap here—using a queue in the BFS algorithm—shouldn’t be too great.

Assumptions regarding prior knowledge of mathematics

This text assumes a reasonable background in high-school algebra and a little geometry (for example, the Pythagorean theorem and right triangles). Prior exposure to summations and subscripts would help the reader but is not essential, as these are introduced in the text. The same goes for mean, standard deviation, and quantiles. You might find it helpful if you’ve seen these before, but these, too, are introduced in the text.

The minimum expectation is that you can add, subtract, multiply and divide; that you understand exponents and square roots; and that you understand the precedence of operations, grouping of expressions with parentheses, and evaluating expressions with multiple terms and operations.

Assumptions regarding prior knowledge of computer use

While this book assumes no prior knowledge whatsoever when it comes to programming, it does assume that you have some familiarity with using a computer and have a basic understanding of your computer’s file system (a hierarchical system consisting of files and directories). If you don’t know what a file is, or what a directory is, see Appendix D, or consult documentation for your operating system. Writing and running programs requires that you understand the basics of a computer file system.

Typographic conventions used in this book

Names of functions, variables, and modules are rendered in fixed-pitch typeface, as are Python keywords, code snippets, and sample output.

print("Hello, World!")

When referring to structures which make use of multiple keywords we render these keywords separated by slashes but do not use fixed-pitch typeface. Examples: if/else, if/elif, if/elif/else, try/except, try/finally, etc.

File names, for example, hello_world.py, and module names, for example, math, are also rendered in fixed-pitch typeface.

Where it is understood that code is entered into the Python shell, the interactive Python prompt >>> is shown. Wherever you see this, you should understand we’re working in Python shell. >>> should never appear in your code.2 Return values and evaluation of expressions are indicated just as they are in the Python shell, without the leading >>>.

>>> 1 + 2
3
>>> import math
>>> math.sqrt(36)
6

In a few places, items which are placeholders for actual values or variable names are given in angle brackets, thus <foo>. For example, when describing the three-argument syntax for the range() function, we might write range(<start>, <stop>, <stride>) to indicate that three arguments must be supplied—the first for the start value, the second for the stop value, and the last for the stride. It’s important to understand that the angle brackets are not part of the syntax, but are merely a typographic convention to indicate where an appropriate substitution must be made.

All of these conventions are in accord with the typographical conventions used in the official Python documentation at python.org. Hopefully, this will make it easier for students when they consult the official documentation.

Note that this use of angle brackets is a little different when it comes to traceback messages printed when exceptions occur. There you may see things like <stdin> and <module>, and in this context, they are not placeholders requiring substitution by the user.

Other conventions

When referring to functions, whether built-in, from some imported module, or otherwise, without any other context or specific problem instance, we write function identifiers along with parentheses (as a visual indicator that we’re speaking of a function) but without formal parameters. Example: “The range() function accepts one, two, or three arguments.” This should not be read as suggesting that range() takes no arguments.

Entry point / top-level code environment

As noted in the text, unlike many other languages such as C, C++, Java, etc., a function named main() has no special meaning in Python whatsoever. The correct way to specify the entry point of your code in Python is with

if __name__ == '__main__':
    # the rest of your code here

This is explained fully in Chapter 9.

In code samples in the book, we do, however, avoid using this if there are no function definitions included in the code. We do this for space and conciseness of the examples. The same could reasonably apply to your code. In most cases, if there are no function definitions in your module, there’s no need for this if statement (though it’s fine to include it). However, if there are any function definitions in your module, then if __name__ == '__main__': is the correct, Pythonic way to segregate your driver code from your function definitions.

Origin of Python

Python has been around a long time, with the first release appearing in 1991 (four years before Java). It was invented by Guido van Rossum, who is now officially Python’s benevolent dictator for life (BDFL).

Python gets its name from the British comedy troupe Monty Python’s Flying Circus (Guido is a fan).

Nowadays, Python is one of the most widely used programming languages on the planet and is supported by an immense ecosystem and thriving community. See: https://python.org/ for more.

Python version

As this book is written, the current version of Python is 3.11.4. However, no new language features introduced since version 3.6 are presented in this book (as most are not appropriate or even useful for beginners). This book does cover f-strings, which were introduced in version 3.6. Accordingly, if you have Python version 3.6–3.11, you should be able to follow along with all code samples and exercises.

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).

Footnotes

  1. It’s not quite sui generis—Python is firmly rooted in the tradition of ALGOL-influenced programming languages.↩︎

  2. Except in the case of doctests, which are not presented in this text.↩︎