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:
Numbers, which evaluate to themselves:
3.14evaluates to the number3.14Names, which evaluate to their values in the current environment
Call expressions, which call a function on some arguments
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)1024pow(1 + 1, min(3, abs(-4)))8A 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))):
the operator is the name
pow, and its value is the built-inpowfunction.the operands are
1 + 1andmin(3, abs(-4)), and their values are 2 and 3.calling the
powfunction on 2 and 3 returns 8, which is the value of the whole expression.
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:
powalways takes two numbers.pow(x, y)returnsxraised to the power ofy.pow(2, 3)returns 8.abstakes a single number and returns its absolute value.abs(-5)returns 5.booltakes a single argument and returnsTrueif the argument is a true value (sometimes called a truthy value), otherwise it returnsFalse. Some false values:0,False,'',None. All other numbers and strings are true values, as are all functions.strreturns a string for any value.str(123)returns'123'.roundtakes a number and optionally the number of decimal places to round to, and returns the rounded number.round(3.14159, 2)returns 3.14,round(3.5)returns 4.maxtakes two or more arguments and returns the largest one.max(1, 5, 3)returns 5.mintakes two or more arguments and returns the smallest one.min(1, 5, 3)returns 1.lentakes one argument that has a length, such as a string. It returns the length of the string. It cannot be called on a number.
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:
addalways takes two arguments, which could both be numbers or both be strings.add(2, 3)returns 5 andadd('two', 'three')returnstwothree. For any two expressionsxandy,add(x, y)has the same value and behavior asx + ysub(x, y)is the same asx - ymul(x, y)is the same asx * yfloordiv(x, y)is the same asx // ytruediv(x, y)is the same asx / y
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:
For any
xand any positivea,f(x + a) > f(x).f(max(x, y)) = max(f(x), f(y))
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:
For any
xand any positivea,f(x + a) >= f(x).f(max(x, y)) = max(f(x), f(y))
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:
kis a factor ofnifnis a multiple ofk.For any
n, there are at least two factors: 1 andn. If these are the only two factors, thennis prime.
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...
by 10 is the last digit of
nby 100 is the last two digits of
nby
pow(10, p)is the lastpdigits ofnfor positive integerp.
The operator expression has different names in different documentation. For example, in the Python Docs it is called the primary.