Overview
Teaching: 15 min
Exercises: 10 minQuestions
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.
print and type)min, int, len, round, open, del, and helplen takes exactly one argument, and returns its lengthint, str, and float create a new value from an existing oneprint takes zero or more argumentsprint with no arguments prints a blank line
print('before')
print()
print('after')
before
after
len to find the length of a stringprint(len('helium'))
6
max, min, and roundmax to find the largest value of one or more values.min to find the smallest.print(max(1, 2, 3))
print(min('a', 'A', '0'))
3
0
len does not work when passed in a number because the result would
be ambiguous. For example, what is the ‘length’ of 10?print(len(10))
TypeError: object of type 'int' has no len()
max and min must be given at least one argument.
print(max(1, 'a'))
TypeError: unorderable types: str() > int()
round will round off a floating-point number.round(3.712)
4
round(3.712, 1)
3.7
help to get help for a functionhelp(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.
shift, and press tab.# Forgot to close the quotation marks around the string.
name = 'Feng
SyntaxError: EOL while scanning string literal
# An extra '=' in the assignment.
age = = 52
SyntaxError: invalid syntax
print("hello world"
File "<ipython-input-6-d1cc229bf815>", line 1
print ("hello world"
^
SyntaxError: unexpected EOF while parsing
-6- part of the filename indicates that
the error occurred in cell 6 of our Notebook.^ pointer.age = 53
remaining = 100 - aege # mis-spelled 'age'
NameError: name 'aege' is not defined
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?Solution
Consider that
Nonesignifies the successful execution of a function that doesn’t otherwise return a value. Such a response would be nonsensical, because no comparison is possible on an empty set of arguments.
Last Character of a String
If Python starts counting from zero, and
lenreturns the number of characters in a string, what index expression will get the last character in the stringname? (Note: we will see a simpler way to do this in a later episode.)Solution
name[len(name) - 1]
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.
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.