Built-in Functions

Python has many built-in functions that you can use. These functions are available without importing any modules. In this lesson, we will learn about a few of them. You can see a full list of built-in functions here.

Numbers

There are several functions available for working with numbers and lists of numbers. These include:

  • abs() - returns the absolute value of a number
  • round() - rounds a number to a given number of decimals
  • divmod() - returns both // and % in a single operation

Functions that take a list of numbers as an argument include:

  • min() - returns the smallest number in a list
  • max() - returns the largest number in a list
  • sum() - returns the sum of all numbers in a list

Characters

The ord() and chr() functions allow you to convert a character to and from its Unicode code integer representation.

Booleans

The any() and all() functions are used to evaluate a list of booleans. Each item in the list is evaluated as a boolean, equivalent to bool() function.

Sequences

Several functions can be used to work with sequences. sorted() and reversed() return a new list with a changed order. The original list is not modified.

The enumerate() function is used to iterate over a sequence when you need both the index and the value.

The zip() function is used to iterate over multiple sequences at the same time.

The map() function is used to apply a function to each item in a sequence.

Exercise

Given two lists of numbers called secret and key, use the following steps to decode the message.

  1. Get the absolute value of each number in secret
  2. For each index add the number in key to the result of step 1
  3. Reverse the order of the numbers in the result of step 2
  4. Use integer division to divide each number in the result of step 3 by 5
  5. Add the index of each number in step 4 to its value
  6. Convert each value to a character
  7. use ''.join(list) to convert a list of characters to a string