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

  • max and min must 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'))

{: .language-python}

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'

{: .error}

Functions may have default values for some arguments.¶

  • round will round off a floating-point number.

  • By default, rounds to zero decimal places.

round(3.712)

{: .language-python}

4

{: .output}

  • We can specify the number of decimal places we want.

round(3.712, 1)

{: .language-python}

3.7

{: .output}

Functions attached to objects are called methods¶

  • Functions take another form that will be common in the pandas episodes.

  • Methods have parentheses like functions, but come after the variable.

  • Some methods aren’t used often, and are marked with double underlines.

my_string = 'Hello world!'  # creation of a string object 

print(len(my_string))       # function with the string as an argument

print(my_string.__len__())  # method acting upon the string object

{: .language-python}

12
12

{: .output}

  • You might even see them chained together. They operate left to right.

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

{: .language-python}

False
HELLO WORLD
True

{: .output}

Use the built-in function help to get help for a function.¶

  • Every built-in function has online documentation.

help(round)

{: .language-python}

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.

{: .output}

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

  • Won’t even try to run the program if it can’t be parsed.

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

{: .language-python}

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

{: .error}

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

{: .language-python}

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

{: .error}

  • Look more closely at the error message:

print("hello world"

{: .language-python}

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

{: .error}

  • The message indicates a problem on first line of the input (“line 1”).

    • In this case the “ipython-input” section of the file name tells us that we are working with input into IPython, the Python interpreter used by the Jupyter Notebook.

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

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

{: .language-python}

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

{: .error}

  • Fix syntax errors by reading the source and runtime errors by tracing execution.

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?

radiance = 1.0
radiance = max(2.1, 2.0 + min(radiance, 1.1 * radiance - 0.5))

{: .language-python}

Solution¶

  1. Order of operations:

    1. 1.1 * radiance = 1.1

    2. 1.1 - 0.5 = 0.6

    3. min(radiance, 0.6) = 0.6

    4. 2.0 + 0.6 = 2.6

    5. max(2.1, 2.6) = 2.6

  2. At the end, radiance = 2.6 {: .solution} {: .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?

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

{: .language-python}

Solution¶

print(max(easy_string))

{: .language-python}

c

{: .output}

print(max(rich, poor))

{: .language-python}

tin

{: .output}

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

{: .language-python}

4

{: .output} 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.

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'

{: .error } {: .solution} {: .challenge}

Why Not?¶

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

Solution¶

max and min return TypeErrors in this case because the correct number of parameters was not supplied. If it just returned None, the error would be much harder to trace as it would likely be stored into a variable and used later in the program, only to likely throw a runtime error. {: .solution} {: .challenge}

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] {: .solution} {: .challenge}