Skip to main content

Section 6.2 Built-in functions

As we mentioned previously, the creators of Python wrote a large number of functions to solve common problems and included them in Python for us to use. Here are more of them.
The max and min functions give us the largest and smallest values of their arguments, respectively:
The max function gives us the value 4 because it is the largest value among its arguments. The min function, conversely, give us the value -2 because it is the smallest value of its arguments.
You can put as many arguments as you want in the parentheses when you call max or min. All the arguments must be “compatible” data types. You can have integers and floating point numbers in the arguments, but you can’t mix integers and booleans, for example.
See if you can predict what will happen when you run this program.
If you need to find the absolute value of a number, you can use the built-in abs function. The abs function takes exactly one argument, and it must be numeric.
Another very useful built-in function is the len function, which tells us how many items are in its argument. If the argument to len is a list, it returns the number of items in the list. If the argument to len is a string, it returns the number of characters in the string.
You should treat the names of built-in functions as reserved words (i.e., avoid using max as a variable name).

Checkpoint 6.2.1.

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

Checkpoint 6.2.2.

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