Built-in Functions | Functions
Introduction to Python

# Built-in Functions

What if you were asked to find the largest number in a list? With the knowledge you've gained, you could write a loop to check if the current element is larger/smaller than the previous biggest one and update it; if not, you'd continue. But for long lists, this method can be quite time-consuming. Thankfully, there are built-in functions that can make this task more efficient. Here are a few:

• `min(x, y, ...)` - Returns the smallest value among `x, y, ...`;
• `max(x, y, ...)` - Returns the largest value among `x, y, ...`;
• `abs(x)` - Gives the absolute value of `x`;
• `round(x, n)` - Rounds the number `x` to `n` decimal places;
• `pow(x, n)` - Raises `x` to the power of `n`.

For instance, suppose we want to calculate the population density for a set of countries in the `countries` list. To do this, we'd divide the population by the area. Here's how it's done:

In the example above, our list had 5 nested sub-lists. We looped through the main list and checked if each item was a list. If it was, we divided the third item (population) by the second item (area).

However, the results were not very reader-friendly since they had more than 10 decimal places. To make them more readable, we can use the `round()` function to reduce them to just 2 decimal places. Remember, this function takes two arguments: the first is the number you want to round, and the second specifies how many decimal places you want to keep.

As you can see, the revised result is much clearer and easier to understand.

Everything was clear?

Section 6. Chapter 1

Course Content

Introduction to Python

# Built-in Functions

What if you were asked to find the largest number in a list? With the knowledge you've gained, you could write a loop to check if the current element is larger/smaller than the previous biggest one and update it; if not, you'd continue. But for long lists, this method can be quite time-consuming. Thankfully, there are built-in functions that can make this task more efficient. Here are a few:

• `min(x, y, ...)` - Returns the smallest value among `x, y, ...`;
• `max(x, y, ...)` - Returns the largest value among `x, y, ...`;
• `abs(x)` - Gives the absolute value of `x`;
• `round(x, n)` - Rounds the number `x` to `n` decimal places;
• `pow(x, n)` - Raises `x` to the power of `n`.

For instance, suppose we want to calculate the population density for a set of countries in the `countries` list. To do this, we'd divide the population by the area. Here's how it's done:

In the example above, our list had 5 nested sub-lists. We looped through the main list and checked if each item was a list. If it was, we divided the third item (population) by the second item (area).

However, the results were not very reader-friendly since they had more than 10 decimal places. To make them more readable, we can use the `round()` function to reduce them to just 2 decimal places. Remember, this function takes two arguments: the first is the number you want to round, and the second specifies how many decimal places you want to keep.

As you can see, the revised result is much clearer and easier to understand.

Everything was clear?

Section 6. Chapter 1