Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Built-in Functions in Python | Functions in Python
Introduction to Python(ihor)

bookBuilt-in Functions in Python

Imagine you have to find the largest number within the list. With what you've learned, you could write a loop that compares each element to the current largest value, updating it when a bigger number is found. Fortunately, Python provides built-in functions that make this task easier and efficient.

For example, if we need to calculate the population density for a set of countries in the countries list, we simply divide the population by the land area.

1234567891011
# Initial data countries = [["USA", 9629091, 331002651], ["Canada", 9984670, 37742154], ["Germany", 357114, 83783942], ["Brazil", 8515767, 212559417], ["India", 3166391, 1380004385]] # Iterating over external list for i in range(len(countries)): if type(countries[i]) is list: # Computing population density for a country pop_dens = countries[i][2]/countries[i][1] print(countries[i][0], pop_dens, 'people per km²')
copy

The list contained five nested sub-lists. A loop iterated through the main list, checking if each item was a list. If so, the population density was calculated by dividing the third item (population) by the second item (area).

1234567891011
# Initial data countries = [["USA", 9629091, 331002651], ["Canada", 9984670, 37742154], ["Germany", 357114, 83783942], ["Brazil", 8515767, 212559417], ["India", 3166391, 1380004385]] # Iterating over external list for i in range(len(countries)): if type(countries[i]) is list: # Computing population density for a country pop_dens = round(countries[i][2]/countries[i][1], 2) print(countries[i][0], pop_dens, 'people per km²')
copy

However, the results were difficult to read due to more than 10 decimal places. To improve readability, you can use the round() function to limit the output to 2 decimal places. This function takes two arguments: the first is the number to be rounded, and the second specifies the number of decimal places to retain.

question mark

Which function would you use to round a number to a specific number of decimal places?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 6. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 1.67

bookBuilt-in Functions in Python

Stryg for at vise menuen

Imagine you have to find the largest number within the list. With what you've learned, you could write a loop that compares each element to the current largest value, updating it when a bigger number is found. Fortunately, Python provides built-in functions that make this task easier and efficient.

For example, if we need to calculate the population density for a set of countries in the countries list, we simply divide the population by the land area.

1234567891011
# Initial data countries = [["USA", 9629091, 331002651], ["Canada", 9984670, 37742154], ["Germany", 357114, 83783942], ["Brazil", 8515767, 212559417], ["India", 3166391, 1380004385]] # Iterating over external list for i in range(len(countries)): if type(countries[i]) is list: # Computing population density for a country pop_dens = countries[i][2]/countries[i][1] print(countries[i][0], pop_dens, 'people per km²')
copy

The list contained five nested sub-lists. A loop iterated through the main list, checking if each item was a list. If so, the population density was calculated by dividing the third item (population) by the second item (area).

1234567891011
# Initial data countries = [["USA", 9629091, 331002651], ["Canada", 9984670, 37742154], ["Germany", 357114, 83783942], ["Brazil", 8515767, 212559417], ["India", 3166391, 1380004385]] # Iterating over external list for i in range(len(countries)): if type(countries[i]) is list: # Computing population density for a country pop_dens = round(countries[i][2]/countries[i][1], 2) print(countries[i][0], pop_dens, 'people per km²')
copy

However, the results were difficult to read due to more than 10 decimal places. To improve readability, you can use the round() function to limit the output to 2 decimal places. This function takes two arguments: the first is the number to be rounded, and the second specifies the number of decimal places to retain.

question mark

Which function would you use to round a number to a specific number of decimal places?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 6. Kapitel 1
some-alt