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:

Code Description
• `countries = ...`: This line creates a list named `countries`. Each item in this list is another list containing three elements: the name of a country, its area in square kilometers, and its population.

• `for i in range(len(countries)):`: This starts a loop. `len(countries)` counts how many items are in the `countries` list, and `range(len(countries))` creates a sequence of numbers from `0` up to (but not including) the number of items in the `countries` list. The variable `i` takes each of these numbers in turn as the loop runs.

• `if type(countries[i]) is list:`: This line checks if each item (indexed by `i`) in the `countries` list is a list. In this code, every item will indeed be a list (containing the country details), so this condition will always be true.

• `pop_dens = countries[i][2]/countries[i][1]`: Inside the loop, this line calculates the population density of each country. `countries[i]` accesses the `i`-th item in the `countries` list (which is one of the inner lists representing a country). `countries[i][2]` gets the population of the country (the third element in its list), and `countries[i][1]` gets the area of the country (the second element in its list). Dividing the population by the area gives the population density.

• `print(countries[i][0], pop_dens, 'people per km²')`: This line prints three things:
• 1. `countries[i][0]`: The first element in the `i`-th list of `countries`, which is the name of the country.
2. `pop_dens`: The population density calculated in the previous step.
3. `'people per km²'`: A string that just adds some context to the printed population density value.

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.

Code Description
• `countries = ...`: This line initializes a list named `countries`. Each element within this list is another list that contains information about a country: the country's name, its land area in square kilometers, and its population.

• The `for` loop, `for i in range(len(countries)):`: This line sets up a loop that will execute once for each element in the `countries` list. `len(countries)` counts how many elements are in the `countries` list, and `range(len(countries))` generates a sequence of numbers from `0` to one less than the number of elements in the `countries` list. The variable `i` sequentially takes on each value in this range.

• Inside the loop, `if type(countries[i]) is list:` checks whether each element accessed by `countries[i]` is a list. Given the structure of our `countries` list, this condition will always be true.

• `pop_dens = round(countries[i][2]/countries[i][1], 2)`: This line calculates the population density for the country at the current index `i` by dividing the population (third element, index `2`) by the land area (second element, index `1`). The `round` function is used to round the resulting population density to two decimal places.

• `print(countries[i][0], pop_dens, 'people per km²')`: This command prints the name of the country (first element, index `0` of `countries[i]`), followed by the calculated population density (`pop_dens`), and the string `'people per km²'`. This output gives a clear and formatted display of the population density for each country listed in the `countries` array.
• As you can see, the revised result is much clearer and easier to understand.

Tudo estava claro?

Seção 6. Capítulo 1

Conteúdo do Curso

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:

Code Description
• `countries = ...`: This line creates a list named `countries`. Each item in this list is another list containing three elements: the name of a country, its area in square kilometers, and its population.

• `for i in range(len(countries)):`: This starts a loop. `len(countries)` counts how many items are in the `countries` list, and `range(len(countries))` creates a sequence of numbers from `0` up to (but not including) the number of items in the `countries` list. The variable `i` takes each of these numbers in turn as the loop runs.

• `if type(countries[i]) is list:`: This line checks if each item (indexed by `i`) in the `countries` list is a list. In this code, every item will indeed be a list (containing the country details), so this condition will always be true.

• `pop_dens = countries[i][2]/countries[i][1]`: Inside the loop, this line calculates the population density of each country. `countries[i]` accesses the `i`-th item in the `countries` list (which is one of the inner lists representing a country). `countries[i][2]` gets the population of the country (the third element in its list), and `countries[i][1]` gets the area of the country (the second element in its list). Dividing the population by the area gives the population density.

• `print(countries[i][0], pop_dens, 'people per km²')`: This line prints three things:
• 1. `countries[i][0]`: The first element in the `i`-th list of `countries`, which is the name of the country.
2. `pop_dens`: The population density calculated in the previous step.
3. `'people per km²'`: A string that just adds some context to the printed population density value.

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.

Code Description
• `countries = ...`: This line initializes a list named `countries`. Each element within this list is another list that contains information about a country: the country's name, its land area in square kilometers, and its population.

• The `for` loop, `for i in range(len(countries)):`: This line sets up a loop that will execute once for each element in the `countries` list. `len(countries)` counts how many elements are in the `countries` list, and `range(len(countries))` generates a sequence of numbers from `0` to one less than the number of elements in the `countries` list. The variable `i` sequentially takes on each value in this range.

• Inside the loop, `if type(countries[i]) is list:` checks whether each element accessed by `countries[i]` is a list. Given the structure of our `countries` list, this condition will always be true.

• `pop_dens = round(countries[i][2]/countries[i][1], 2)`: This line calculates the population density for the country at the current index `i` by dividing the population (third element, index `2`) by the land area (second element, index `1`). The `round` function is used to round the resulting population density to two decimal places.

• `print(countries[i][0], pop_dens, 'people per km²')`: This command prints the name of the country (first element, index `0` of `countries[i]`), followed by the calculated population density (`pop_dens`), and the string `'people per km²'`. This output gives a clear and formatted display of the population density for each country listed in the `countries` array.
• As you can see, the revised result is much clearer and easier to understand.

Tudo estava claro?

Seção 6. Capítulo 1