Built-in Functions and Help

Last updated on 2026-10-11 | Edit this page

Estimated time: 25 minutes

Overview

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.
  • Describe (and correct) situations in which SyntaxError and NameError occur.

Use comments to add documentation to programs.


Python ignores lines that begin with the # symbol. Use these to create comments to annotate your code.

PYTHON

# This sentence isn't executed by Python.
adjustment = 0.5   # Neither is this - anything after '#' is ignored.

Comments do not change how Python code behaves.

PYTHON

adjustment = 0.5
# adjustment = 1
print(adjustment)

OUTPUT

0.5

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. Functions take 0 or more arguments.
  • len takes exactly one.
  • int, str, and float also take one argument.
  • 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.

PYTHON

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

OUTPUT

before

after

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. None is a Python object that stands in anytime there is no value.

PYTHON

result = print('example')
print('the result of a print statement is', result)

OUTPUT

example
result of print is None

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 of one or more values.
  • These functions work on strings as well. Characters in strings are compared using ASCII values.

PYTHON

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

OUTPUT

3
0

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


  • max and min must be given at least one argument.
  • When given two or more arguments, they must be comparable.

PYTHON

print(max(1, 'a'))

ERROR

TypeError                                 Traceback (most recent call last)
<ipython-input-52-3f049acf3762> in <module>
----> 1 print(max(1, 'a'))

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

Functions may have default values for some arguments.


The round function rounds off a floating-point number. By default, it rounds to zero decimal places, ie. the nearest integer.

PYTHON

round(3.712)

OUTPUT

4

You can specify the number of decimal places to round to as a second argument.

PYTHON

round(3.712, 1)

OUTPUT

3.7

Use help to get help for a function.


How could you have known what the 1 in round(3.712, 1) does if you hadn’t been told? When learning new Python functions, you should read about them carefully.

You can use help() followed by the name of a function (without parentheses) to get information about what a function does and what arguments it expects.

PYTHON

help(round)

OUTPUT

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.

Functions attached to objects are called methods


Methods are special functions that come after an object, followed by a . symbol. Methods are said to be bound to objects of a particular type.

PYTHON

my_string = 'Hello world!'  # Create and assign my_string

print(len(my_string))       # the len function takes a string as an argument and returns the length of the string

print(my_string.swapcase()) # calling the swapcase method on the my_string object

print(my_string.__len__())  # calling the internal __len__ method on the my_string object, used by len(my_string)

OUTPUT

12
hELLO WORLD!
12

Methods (and functions) can be chained together. They always operate from left to right.

PYTHON

print(my_string.isupper())          # Not all the letters are uppercase
print(my_string.upper())            # This capitalizes all the letters

print(my_string.upper().isupper())  # Now all the letters are uppercase

OUTPUT

False
HELLO WORLD
True

The Jupyter Notebook has two ways to get help.


Option 1: Place the cursor near where the function is invoked in a cell (i.e., the function name or its parameters), - Hold down Shift, and press Tab. - Do this several times to expand the information returned. Option 2: Type the function name in a cell with a question mark after it. Then run the cell.

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


Grammatically incorrect Python produces syntax errors. The most common syntax errors involve failure to close quotes, parentheses, or improper indentation.

PYTHON

# Forgot to close the quote marks around the string.
name = 'Feng

ERROR

  File "<ipython-input-56-f42768451d55>", line 2
    name = 'Feng
                ^
SyntaxError: EOL while scanning string literal

PYTHON

# An extra '=' in the assignment.
age = = 52

ERROR

  File "<ipython-input-57-ccc3df3cf902>", line 2
    age = = 52
          ^
SyntaxError: invalid syntax

Look closely at error messages to figure out what to fix.

PYTHON

print("hello world"

ERROR

  File "<ipython-input-6-d1cc229bf815>", line 1
    print ("hello world"
                        ^
SyntaxError: unexpected EOF while parsing

The message indicates a problem on first line of the input (“line 1”). - 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.


PYTHON

age = 53
remaining = 100 - aege # mis-spelled 'age'

ERROR

NameError                                 Traceback (most recent call last)
<ipython-input-59-1214fb6c55fc> 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.

Other ways to get help


There are several other ways that people often get help when they are stuck with their Python code.

  • Search the internet: paste the last line of your error message or the word “python” and a short description of what you want to do into your favourite search engine and you will usually find several examples where other people have encountered the same problem and came looking for help.
    • StackOverflow can be particularly helpful for this: answers to questions are presented as a ranked thread ordered according to how useful other users found them to be.
    • Take care: copying and pasting code written by somebody else is risky unless you understand exactly what it is doing!
  • Ask somebody “in the real world”. If you have a colleague or friend with more expertise in Python than you have, show them the problem you are having and ask them for help.

What About Generative AI?

We recommend against using generative AI tools for the exercises in this workshop. This workshop is intended to teach you the grammar of Python and strategies for solving programming problems. You will not be able to understand or evaluate the results of Generate AI tools, should you choose to use them, if you do not master the fundamental skills presented here.

Challenge

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?

PYTHON

radiance = 1.0
radiance = max(2.1, 2.0 + min(radiance, 1.1 * radiance - 0.5))
  1. Order of operations:
  2. 1.1 * radiance = 1.1
  3. 1.1 - 0.5 = 0.6
  4. min(radiance, 0.6) = 0.6
  5. 2.0 + 0.6 = 2.6
  6. max(2.1, 2.6) = 2.6
  7. At the end, radiance = 2.6
Challenge

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 any sense?

PYTHON

easy_string = "abc"
print(max(easy_string))
rich = "gold"
poor = "tin"
print(max(rich, poor))
print(max(len(rich), len(poor)))

PYTHON

print(max(easy_string))

OUTPUT

c

PYTHON

print(max(rich, poor))

OUTPUT

tin

PYTHON

print(max(len(rich), len(poor)))

OUTPUT

4

max(len(rich), poor) throws a TypeError. This turns into max(4, 'tin') and as we discussed earlier a string and integer cannot meaningfully be compared.

ERROR

TypeError                                 Traceback (most recent call last)
<ipython-input-65-bc82ad05177a> in <module>
----> 1 max(len(rich), poor)

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

Explore the Python docs!

The official Python documentation is arguably the most complete source of information about the language. It is available in different languages and contains a lot of useful resources. The Built-in Functions page contains a catalogue of all of these functions, including the ones that we’ve covered in this lesson. Some of these are more advanced and unnecessary at the moment, but others are very simple and useful.

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