Книга: Practical Programming, Fourth Edition
Назад: Chapter 3: Designing and Using Functions
Дальше: Identities: How Python Keeps Track of Values

Functions That Python Provides

Python comes with many built-in functions that perform routine operations. One example is the abs function, which produces (returns) the absolute value of a number:

 >>>​​ ​​abs
 <built-in function abs>
 >>>​​ ​​f​​ ​​=​​ ​​abs
 >>>​​ ​​f
 <built-in function abs>
 >>>​​ ​​abs(-9)
 9
 >>>​​ ​​abs(3.3)
 3.3

The first three statements demonstrate that abs (directly and via the variable f) is a function. The remaining two statements are a function calls.

Keep Your Shell Open

icon indicating an aside

We recommend that you have IDLE (or any other Python IDE) open and try all the code under discussion; this is a good way to solidify your learning.

The general form of a function call is as follows:

 function_name(arguments)

An argument is an expression that appears between the parentheses of a function call

An argument is an expression that appears between the parentheses of a function call. In abs(-9), the argument is -9.

Let’s calculate the difference between the day and night temperatures, as might be seen on a weather report (a warm weather system moved in overnight):

 >>>​​ ​​day_temperature​​ ​​=​​ ​​3
 >>>​​ ​​night_temperature​​ ​​=​​ ​​10
 >>>​​ ​​abs(day_temperature​​ ​​-​​ ​​night_temperature)
 7

In this call of the function abs, the argument is day_temperature - night_temperature. Because day_temperature refers to 3 and night_temperature refers to 10, Python evaluates this expression to -7. This value is then passed to the abs function, which returns the value 7.

A function call is a command to execute a function

Here are the rules for executing a function call:

  1. Evaluate each argument one at a time, working from left to right.
  2. Pass the resulting values into the function.
  3. Execute the function. When the function call finishes, it produces a value.

Because function calls produce values, they can be used in expressions:

 >>>​​ ​​abs(-7)​​ ​​+​​ ​​abs(3.3)
 10.3

You can also use function calls as arguments to other functions:

 >>>​​ ​​pow(abs(-2),​​ ​​round(4.3))
 16

Python sees the call on pow and starts by evaluating the arguments from left to right. The first argument is a call to the function abs, so Python executes it. The call abs(-2) produces 2, so that’s the first value for the call on pow. Then Python executes round(4.3), which produces 4.

Now that the arguments to the pow function have been evaluated, Python finishes calling pow, passing in 2 and 4 as the argument values. That means that pow(abs(-2), round(4.3)) is equivalent to pow(2,4), and 24 is 16.

The following diagram shows the order in which Python evaluates the various pieces of this expression.

Subexpression

Each subexpression has been underlined and given a number to indicate when Python executes or evaluates that subexpression.

Some of the most useful built-in functions are used to convert between data types. Type names int and float can be used as functions:

 >>>​​ ​​int(34.6)
 34
 >>>​​ ​​int(-4.3)
 -4
 >>>​​ ​​float(21)
 21.0

In this example, you observe that when a floating-point number is converted to an integer, it is truncated, rather than rounded.

If you’re not sure what a function does, try calling the built-in function help, which shows documentation for any function:

 >>>​​ ​​help(abs)
 Help on built-in function abs in module builtins:
 
 abs(x, /)
  Return the absolute value of the argument.

The first line states which function is being described and to which module it belongs. Here, the module name is builtins. Modules are an organizational tool in Python and are discussed in Chapter 6, .

The rest of the output describes the function’s functionality. The form of the function appears first: function abs expects one argument. (The / indicates that there are no more arguments.) After the form is an English description of what the function does when it is called.

Another built-in function is round, which rounds a floating-point number to the nearest integer:

 >>>​​ ​​round(3.8)
 4
 >>>​​ ​​round(3.3)
 3
 >>>​​ ​​round(3.5)
 4
 >>>​​ ​​round(-3.3)
 -3
 >>>​​ ​​round(-3.5)
 -4

The function round can be called with one or two arguments. If called with one, as you’ve been doing, it rounds to the nearest integer. If called with two, it rounds to a floating-point number, where the second argument indicates the precision:

 >>>​​ ​​round(3.141592653,​​ ​​2)
 3.14

The documentation for round suggests that the second argument is optional, as indicated by its default value, None:

 >>>​​ ​​help(round)
 Help on built-in function round in module builtins:
 
 round(number, ndigits=None)
  Round a number to a given precision in decimal digits.
 
  The return value is an integer if ndigits is omitted or None. Otherwise
  the return value has the same type as the number. ndigits may be negative.

Let’s explore the built-in function pow by starting with its help documentation:

 >>>​​ ​​help(pow)
 Help on built-in function pow in module builtins:
 
 pow(base, exp, mod=None)
  Equivalent to base**exp with 2 arguments or base**exp % ​​mod​​ ​​with​​ ​​3​​ ​​arguments
 
  Some types, such as ints, are able to use a more efficient algorithm when
  invoked using the three argument form.

The description demonstrates that the pow function can be called with either two or three arguments. The English description mentions that when called with two arguments, it is equivalent to x ** y, or xy. Let’s try it:

 >>>​​ ​​pow(2,​​ ​​4)
 16

This call calculates 24. So far, so good. How about with three arguments?

 >>>​​ ​​pow(2,​​ ​​4,​​ ​​3)
 1

24 is 16, and evaluation of 16 % 3 produces 1.

Назад: Chapter 3: Designing and Using Functions
Дальше: Identities: How Python Keeps Track of Values