Built-in Functions, Help and Errors

Overview

Teaching: 15 min
Exercises: 15 min
Questions
  • How can I use built-in functions?

  • How can I find out what they do?

  • What kind of errors can occur in programs?

Objectives
  • Explain the purpose of functions.

  • Correctly call built-in Python functions.

  • Correctly nest calls to built-in functions.

  • Use help to display documentation for built-in functions.

  • Correctly describe situations in which SyntaxError and NameError occur.

A function may take zero or more arguments.

print('before')
print()
print('after')
before

after

Commonly-used built-in functions include max, min, and round.

print(max(1, 2, 3))
print(min('a', 'A', '0'))
3
0

Every function returns something.

result = print('example')
print('result of print is', result)
example
result of print is None

Functions may only work for certain (combinations of) arguments.

print(max(1, 'a'))
TypeError: unorderable types: str() > int()

Functions may have default values for some arguments.

round(3.712)
4
round(3.712, 1)
3.7

Use the built-in function help to get help for a function.

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.

(The Jupyter Notebook has an alternative way to access function descriptions)

round?

Python reports a syntax error when it can’t understand the source of a program.


# Forgot to close the quote marks around the string.
name = 'Feng
SyntaxError: EOL while scanning string literal
# An extra '=' in the assignment.
age = = 52
SyntaxError: invalid syntax
print("hello world"
  File "<ipython-input-6-d1cc229bf815>", line 1
    print ("hello world"
                        ^
SyntaxError: unexpected EOF while parsing

Python reports a runtime error when something goes wrong while a program is executing.

age = 53
remaining = 100 - aege # mis-spelled 'age'
NameError: name 'aege' is not defined

What Happens When

  1. Explain in simple terms the order of operations in the following program: when does the addition happen, when does the subtraction happen, when is each function called, etc.
  2. What is the final value of radiance?
radiance = 1.0
radiance = max(2.1, 2.0 + min(radiance, 1.1 * radiance - 0.5))

Solution

    1. 1.1 * radiance = 1.1
    2. 1.1 - 0.5 = 0.6
    3. min(randiance, 0.6) = 0.6
    4. 2.0 + 0.6 = 2.6
    5. max(2.1, 2.6) = 2.6
  1. At the end, radiance = 2.6

Why Not?

Why don’t max and min return None when they are given no arguments?

Solution

max and min return TypeErrors in this case because the correct number of parameters was not supplied. If it just returned None, the error would be much harder to trace as it would likely be stored into a variable and used later in the program, only to likely throw a runtime error.

Key Points

  • A function may take zero or more arguments.

  • Commonly-used built-in functions include max, min, and round.

  • Functions may only work for certain (combinations of) arguments.

  • Functions may have default values for some arguments.

  • Use the built-in function help to get help for a function.

  • The Jupyter Notebook has two ways to get help.

  • Every function returns something.

  • Python reports a syntax error when it can’t understand the source of a program.

  • Python reports a runtime error when something goes wrong while a program is executing.

  • Fix syntax errors by reading the source code, and runtime errors by tracing the program’s execution.