Functions That Python Provides

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

 >>>​​ ​​abs(-9)
 9
 >>>​​ ​​abs(3.3)
 3.3

Each of these statements is a function call.

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. In abs(-9), the argument is -9.

Here, we calculate the difference between a day temperature and a night temperature, 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 on 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 function abs, which then returns, or produces, the value 7.

Here are the rules to 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

We 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 on function abs, so Python executes it. 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 call on function pow have been evaluated, Python finishes calling pow, sending 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.

Here is a diagram indicating the order in which the various pieces of this expression are evaluated by Python:

images/functions/subexpression.png

We have underlined each subexpression and given it a number to indicate when Python executes or evaluates that subexpression.

Some of the most useful built-in functions are ones that convert from one type to another. 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, we see that when a floating-point number is converted to an integer, it is truncated, not rounded.

If you’re not sure what a function does, try calling 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 which module it belongs to. Here, the module name is builtins. Modules are an organizational tool in Python and are discussed in Chapter 6, A Modular Approach to Program Organization.

The next part describes what the function does. 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 we’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 indicates that the second argument is optional by surrounding it with brackets:

 >>>​​ ​​help(round)
 Help on built-in function round in module builtins:
 
 round(...)
  round(number[, ndigits]) -> number
 
  Round a number to a given precision in decimal digits (default 0 digits).
  This returns an int when called with one argument, otherwise the
  same type as the number. ndigits may be negative.

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

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

This shows that the function pow 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. 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

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.86.211