Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The first chapter discusses expressions, names, assignment, control, and functions. This reference section contains the chapter’s most important definitions, along with a list of built-in functions and some mathematical background material that appears in examples.

Expressions

An expression is code that can be evaluated and therefore has a value.

Learning Python involves learning its different forms of expressions and how they are evaluated. Important kinds of expressions include:

Names

Programs can have different behavior (such as displaying messages, sending email, etc.), but mostly exist to manipulate values. A value is a general term for some piece of data, which could be a single number or a whole document. This course starts by focusing on only a few types of values: numbers, boolean values (True and False), the None value, strings, and functions.

Code includes names, which refer to values. All expressions are evaluated in some environment, which is the part of the Python interpreter that tracks which names refer to which values. When a name and value are bound together in the environment, the name will evaluate to the value in that environment.

It means the same thing to say that a name is bound to a value or to say that a value is assigned to a name.

Call Expressions

Here are two example call expressions and their values:

pow(2, 10)
1024
pow(1 + 1, min(3, abs(-4)))
8

A call expression has an expression before its parentheses, which in this course we call the operator expression [1]. The operator is almost always a name, and it evaluates to the function being called.

A call expression also has expressions within its parentheses separated by commas, which in this course we call operand expressions. These evaluate to the arguments of the function.

Python evaluates a call expression by evaluating the operator, then evaluating the operands, then calling the function (the value of the operator) on its arguments (the values of the operands). Calling a function on some arguments returns a value, and the value of the call expression is the value returned by the function called.

For example, in pow(1 + 1, min(3, abs(-4))):

Built-in Functions

Python starts running all programs in the global environment. Initially, a lot of names are bound to built-in functions. For example, the name max is initially bound to the built-in max function. The built-in functions used in the first unit of the course are:

In Chapter 2 we’ll learn about other ways to call max and min. We’ll also learn about many other built-in functions, such as sum. A common pitfall: in Python, sum(2, 3) does not return 5. Use add(2, 3) or 2 + 3.

Some functions need to be imported. The ones used in the first unit include:

Numbers

Many examples in this course involve concepts related to numbers, such as multiples and factors.

The definitions that appear on assignments and exams aim to be short, general, and precise rather than intuitive and conversational. For example, when defining the term multiple, you won’t see: “The multiples of 10 are 20, 30, 40, 50, and so on. They are what you get by repeatedly adding the same number.” That definition is problematic because “and so on” isn’t precise, and it leaves open a lot of questions, such as whether 0, 10, or -20 are multiples of 10. Instead, you’ll see: “n is a multiple of integer m if n equals m * b for some integer b.” This definition is shorter and clarifies that 0, 10, and -20 are all multiples of 10, just like 20, 30, 40, and 50.

Here are some useful concepts related to numbers:

A function f is increasing if f(y) > f(x) whenever y > x. Some properties of an increasing function f include:

A closely related concept is non-decreasing: A function f is non-decreasing if f(y) >= f(x) whenever y > x. The properties are similar for a non-decreasing function f:

A positive integer k is a factor of a positive integer n if n/k is an integer. When n/k is an integer, we say that k evenly divides n. Some properties:

The remainder when dividing integer n by integer k is the smallest r such that k*d + r for some integer d. The remainder when dividing n...

Footnotes
  1. The operator expression has different names in different documentation. For example, in the Python Docs it is called the primary.