Built-in Functions and Help
Overview
Teaching: 10 min
Exercises: 10 minQuestions
How can I use built-in functions?
How can I find out what they do?
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.
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.
lentakes exactly one.int,str, andfloatcreate a new value from an existing one.printtakes zero or more.printwith 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
maxto find the largest value of one or more values. - Use
minto 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.
maxandminmust 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: unorderable types: str() > int()
Functions may have default values for some arguments.
roundwill 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(...)
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 two ways to get help.
- Place the cursor inside the parenthesis of the function,
hold down
shift, and presstab. - Or type a function name with a question mark after it.
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)
example
result of print is None
What Happens When
- 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.
- What is the final value of
radiance?radiance = 1.0 radiance = max(2.1, 2.0 + min(radiance, 1.1 * radiance - 0.5))
Spot the Difference
- Predict what each of the
- Does
max(len(rich), poor)run or produce an error message? If it runs, does its result make any sense?rich = "gold" poor = "tin" print(max(rich, poor)) print(max(len(rich), len(poor)))
Why Not?
Why don’t
maxandminreturnNonewhen they are given no arguments?
Key Points
A function may take zero or more arguments.
Commonly-used built-in functions include
max,min, andround.Functions may only work for certain (combinations of) arguments.
Functions may have default values for some arguments.
Use the built-in function
helpto get help for a function.The Jupyter Notebook has two ways to get help.
Every function returns something.