Built-in Functions

NoteQuestions
  • What built-in functions does Python provide?
  • How do I call a function and use its result?
  • What kinds of errors can occur in programs?
NoteObjectives
  • Call built-in Python functions with the correct number and type of arguments.
  • Nest function calls and predict the order of evaluation.
  • Distinguish between syntax errors and runtime errors.

Comments document your code

A # character tells Python to ignore the rest of the line. Use comments to explain why your code does something when the reason is not obvious from reading it:

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

Functions take arguments and return results

A function is a named piece of reusable code that you invoke by writing its name followed by parentheses. Values passed inside the parentheses are called arguments. You must always include the parentheses — even when calling a function with no arguments — so Python knows you are making a call rather than referencing the function object itself.

print takes zero or more arguments and displays them on a single line, separated by spaces. With no arguments it prints a blank line:

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

after

len takes exactly one argument and returns the number of items in it. int, str, and float each take one argument and return a converted value.

Commonly-used built-in functions

max and min return the largest and smallest values from a set of arguments. They work on both numbers and strings (strings are compared character by character using Unicode order):

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

round rounds a floating-point number. By default it rounds to zero decimal places; pass a second argument to specify the precision:

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

Functions enforce their requirements. max and min need at least one argument, and the arguments must be mutually comparable — mixing incompatible types raises a TypeError:

print(max(1, 'a'))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 print(max(1, 'a'))

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

Every function returns something

Every function call produces a result. If a function has no meaningful value to return it returns the special value None. print is a common example — its job is the side effect of writing to the screen, not producing a value:

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

When you call functions inside other expressions, nested calls are evaluated from the inside out, just as in mathematics: max(len('tin'), len('copper')) first computes the two lengths, then finds the maximum.

Syntax errors versus runtime errors

Python checks your code for syntax errors — structural problems that make the program impossible to parse — before running a single line. A forgotten closing quote, a misplaced =, or an unclosed parenthesis will produce a SyntaxError with a ^ pointing at approximately where the problem is:

name = 'Feng
  Cell In[7], line 1
    name = 'Feng
           ^
SyntaxError: unterminated string literal (detected at line 1)
print("hello world"
  Cell In[8], line 1
    print("hello world"
                       ^
SyntaxError: incomplete input

Runtime errors only appear when the problematic line actually executes. A misspelled variable name raises a NameError:

age = 53
remaining = 100 - aege  # mis-spelled 'age'
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[9], line 2
      1 age = 53
----> 2 remaining = 100 - aege  # mis-spelled 'age'

NameError: name 'aege' is not defined

Fix syntax errors by reading the source code carefully around the ^ marker; fix runtime errors by tracing what value each variable holds when the error line is reached.

For detailed guidance on finding answers when you are stuck, see the Getting Help lesson.

CautionChallenge

What Happens When

  1. Explain the order of operations in the program below.
  2. What is the final value of radiance?
radiance = 1.0
radiance = max(2.1, 2.0 + min(radiance, 1.1 * radiance - 0.5))
  1. Inside out:
    1. 1.1 * radiance1.1
    2. 1.1 - 0.50.6
    3. min(1.0, 0.6)0.6
    4. 2.0 + 0.62.6
    5. max(2.1, 2.6)2.6
  2. radiance = 2.6
CautionChallenge

Spot the Difference

  1. Predict what each print statement below will output.
  2. Does max(len(rich), poor) run or raise an error? If it runs, does the result make sense?
easy_string = "abc"
print(max(easy_string))
rich = "gold"
poor = "tin"
print(max(rich, poor))
print(max(len(rich), len(poor)))
c
tin
4
c
tin
4
  1. max(len(rich), poor) raises a TypeError — it tries to compare the integer 4 with the string 'tin', which Python cannot order.
CautionChallenge

Why Not?

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

Returning None silently would hide a programming mistake — the caller would store None, use it later, and get a confusing error far from the actual problem. Raising a TypeError immediately points you directly to the bug.

CautionChallenge

Last Character of a String

If Python indexes from zero and len returns the number of characters, what index expression retrieves the last character of name?

name[len(name) - 1]

(A simpler form using negative indexing — name[-1] — is covered in the Variables and Assignment lesson.)

TipKey Points
  • Use comments (#) to document the why behind non-obvious code.
  • Call a function by writing its name followed by arguments in parentheses; parentheses are always required.
  • max, min, and round are commonly-used built-in functions.
  • Nested function calls are evaluated from the inside out.
  • Every function call returns a value; functions with no useful result return None.
  • Python reports a SyntaxError before running the program; a runtime error only appears when the bad line executes.