Skip to main content

Section 5.2 Built-in functions

Python provides a number of important built-in functions that we can use without needing to provide the function definition. The creators of Python wrote a set of functions to solve common problems and included them in Python for us to use.
The max and min functions give us the largest and smallest values in a list, respectively:
The max function gives us the value 4 because it is the largest value in the list. The min function, inversely, give us the value -2 because it is the smallest value in the list.

Checkpoint 5.2.1.

    Q-2: What will be printed as the output of this code?
    maximum = max(5, 'fish', True)
    print(maximum)
    
  • 5
  • Incorrect! You cannot use the max function to compare different data types. Try again.
  • fish
  • Incorrect! You cannot use the max function to compare different data types. Try again.
  • True
  • Incorrect! You cannot use the max function to compare different data types. Try again.
  • There is an error
  • Correct! This code causes a TypeError because the max function cannot be used to compare different data types.
Another very common built-in function is the len function, which tells us how many items are in its argument. If the argument to len is a string, it returns the number of characters in the string.
These functions can operate on any set of values, as we will see in later chapters.
You should treat the names of built-in functions as reserved words (i.e., avoid using “max” as a variable name).

Checkpoint 5.2.2.

    Q-5: Consider the code block below. What prints?
    sentence_a = "Hello, world!"
    length_sentence_a = len(sentence_a)
    print(length_sentence_a)
    
  • 10
  • Incorrect! Spaces and punctuation characters count in the length. Try again.
  • 11
  • Incorrect! Punctuation characters count in the length. Try again.
  • 12
  • Incorrect! Spaces count in the length. Try again.
  • 13
  • Correct! 13 is the length of all characters in the string, including spaces and punctuation.

Checkpoint 5.2.3.

    Q-6: Which of the following would work as a variable name?
  • max
  • Incorrect! This is a reserved keyword because it is a built-in function in Python. Try again.
  • min
  • Incorrect! This is a reserved keyword because it is a built-in function in Python. Try again.
  • built_in
  • Correct! built_in is a valid variable name because it is not a built-in Python function.
  • len
  • Incorrect! This is a reserved keyword because it is a built-in function in Python. Try again.