This lesson is in the early stages of development (Alpha version)

Built-in Functions and Help

Overview

Teaching: 25 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.

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

  • Correctly describe situations in which SyntaxError and NameError occur.

Use comments to add documentation to programs

When coding, it is always a good idea to write comments explaining what the code does - and why.
Comments will help other people understand what the program does.
Just like we spoke about in regards to meaningful variable names, the most important “other person” is your future self!

# This sentence isn't executed by Python.
name = 'Python for Absolute Beginners'   # Neither is this comment - anything after '#' is ignored.

A function may take zero or more arguments

We have seen some functions already (like print() and type()) — now let’s take a closer look.

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', 'b', 'c'))
print(min('a', 'A'))
3
a
A


To see more built-in functions, visit the Python Documentation

Functions may only work for certain (combinations of) arguments

print(max(1, 'a'))
TypeError: '>' not supported between instances of 'str' and 'int'


The error message tells us, that we cannot compare strings and integers.

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.

Python reports a syntax error when grammar rules (that’s Python grammar, not English grammar) have been violated

You’ve seen errors when you try to use a function incorrectly, but you can also have errors when you use punctuation incorrectly.

# Forgot to close the quotation marks around the string.
name = 'Feng
    name = 'Feng
           ^
SyntaxError: unterminated string literal
# An extra '=' in the assignment.
age = = 52
    age = = 52
          ^
SyntaxError: invalid syntax
print("hello world"
  Cell In[1], line 1
    print("hello world"
                       ^
SyntaxError: incomplete input

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

In Python, a runtime error is an error that occurs while a Python program is running. These errors are also known as exceptions. Runtime errors in Python are typically caused by unexpected conditions or events that disrupt the normal flow of the program.
They can happen for various reasons, for example:

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

Every function returns something

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

Contrary to what we might expect, print does not return any value as such. I carries out the execution (i.e., it prints), but after that nothing is returned. Thus, it has a return value of None.

Exercises

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 word?
word = 'blah '
word = max(min(word * 2 + 'blur ', 'aaah '), 'ping')
print(word)

Solution

ping
  1. Initialization: word is assigned the initial value ‘blah ‘.
  2. Inside max() function:
    • word * 2 results in ‘blah blah ‘. (Repeats the string ‘blah ‘ twice using *.)
    • ‘blah blah ‘ + ‘blur ‘ results in ‘blah blah blur ‘. (Concatenates the two strings using +.)
    • ‘aaah ‘ is compared with ‘blah blah blur ‘. The min() function returns the smaller of the two strings, which is ‘aaah ‘.
  3. Outer max() function:
    • The result of the min() function (‘aaah ‘) is passed as an argument.
    • The max() function then compares ‘aaah ‘ with ‘ping’ and returns the larger of the two strings, which is ‘ping’.
  4. Assignment: Finally, the value ‘ping’ is assigned to the variable word.
  5. Output: The program prints the value of word, which is ‘ping’.

Spot the Difference

  1. Predict what each of the print statements in the program below will print.
  2. Does max(len(rich), poor) run or produce an error message? If it runs, does its result make sense to you? (Remember that we are comparing the value of the variable and not the name of the variable.)
rich = "gold"
poor = "tin"
print(max(rich, poor))
print(max(len(rich), len(poor)))

Solution

tin
4

Why Not?

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

Solution

Both functions require at least one argument to execute.

print(max())
TypeError: max expected at least 1 argument, got 0

Last Character of a String

If Python starts counting from zero, and len returns the number of characters in a string, what index expression will get the last character in the string name? (Note: we will see a simpler way to do this in a later episode.)

Solution

name[len(name) - 1]

Key Points

  • 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.

  • 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.