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.

keypoints:

  • Use comments to add documentation to programs.
  • 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.

Use comments to add documentation to programs.

adjustment = 0.5   # Neither is this - anything after '#' is ignored.

A function may take zero or more arguments.

  • We have seen some functions already --- now let's take a closer look.
  • An argument is a value passed into a function.
  • len takes exactly one.
  • int, str, and float create a new value from an existing one.
  • print takes zero or more.
  • print with no arguments prints a blank line.
    • Must always use parentheses, even if they're empty, so that Python knows a function is being called.
print('before')
print()
print('after')
before

after

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

  • Use max to find the largest value of one or more values.
  • Use min to find the smallest.
  • Both work on character strings as well as numbers.
    • "Larger" and "smaller" use (0-9, A-Z, a-z) to compare letters.
print(max(1, 2, 3))
print(min('a', 'A', '0'))
3
0

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

  • max and min must be given at least one argument.
    • "Largest of the empty set" is a meaningless question.
  • And they must be given things that can meaningfully be compared.
print(max(1, 'a'))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/5q/mny3pg2n7h5g21h3v32rfj9wpykqrf/T/ipykernel_11743/2220240766.py in <module>
----> 1 print(max(1, 'a'))

TypeError: '>' not supported between instances of 'str' and 'int'

Functions may have default values for some arguments.

  • round will round off a floating-point number.
  • By default, rounds to zero decimal places.
round(3.712)
4
  • We can specify the number of decimal places we want.
round(3.712, 1)
3.7

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

  • Every built-in function has online documentation.
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.

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

  • Won't even try to run the program if it can't be parsed.
name = 'Feng
  File "/var/folders/5q/mny3pg2n7h5g21h3v32rfj9wpykqrf/T/ipykernel_11743/3103437601.py", line 2
    name = 'Feng
                ^
SyntaxError: EOL while scanning string literal
age = = 52
  File "/var/folders/5q/mny3pg2n7h5g21h3v32rfj9wpykqrf/T/ipykernel_11743/3760570524.py", line 2
    age = = 52
          ^
SyntaxError: invalid syntax
  • We can Look more closely at the error message:
print("hello world"
  File "/var/folders/5q/mny3pg2n7h5g21h3v32rfj9wpykqrf/T/ipykernel_11743/1028029194.py", line 1
    print("hello world"
                       ^
SyntaxError: unexpected EOF while parsing
  • The message indicates a problem on first line of the input ("line 1").
    • In this case the "ipython-input" section of the file name tells us that we are working with input into IPython, the Python interpreter used by the Jupyter Notebook.
  • The -6- part of the filename indicates that the error occurred in cell 6 of our Notebook.
  • Next is the problematic line of code, indicating the problem with a ^ pointer.

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

age = 53
remaining = 100 - aege # mis-spelled 'age'
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/var/folders/5q/mny3pg2n7h5g21h3v32rfj9wpykqrf/T/ipykernel_11743/4124232921.py in <module>
      1 age = 53
----> 2 remaining = 100 - aege # mis-spelled 'age'

NameError: name 'aege' is not defined
  • Fix syntax errors by reading the source and runtime errors by tracing execution.

The Jupyter Notebook has two additional ways to get help.

  • Place the cursor inside the parenthesis of the function, hold down shift, and press tab.
  • Or type a function name with a question mark after it: e.g. math.sqrt?
  • These are the same as doing e.g. help(math.sqrt)
    Tip: How did we know what functions a module has and how to use them?
    If you are working in the IPython/Jupyter Notebook, there is an easy way to find out. If you type the name of something followed by a dot, then you can use tab completion (e.g. type math. and then press tab) to see a list of all functions and attributes that you can use.

Every function returns something.

  • Every function call produces some result.
  • If the function doesn't have a useful result to return, it usually returns the special value None.
result = print('example')
print('result of print is', result)