Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Built-in functions | Functions
Learn Python from Scratch
course content

Conteúdo do Curso

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

Built-in functions

What if I ask you to define the maximum number in the list? Surely, using acquired knowledge, you can write a loop and check if the current element is greater/less than the previous and rewrite it, otherwise - continue... But in the case of big lists, this approach is very time-harmful... This task can be solved by using some built-in function.

  • min(x, y, ...) - minimum element of x, y, ...
  • max(x, y, ...) - maximum element of x, y, ...
  • abs(x) - absolute value of number
  • round(x, n) - rounds number x to n digits
  • pow(x, n) - x raised to the power n

For example, for countries we can calculate population density (population/area) and round result to 2 digits

1234567
countries = [["USA", 9629091, 331002651], ["Canada", 9984670, 37742154], ["Germany", 357114, 83783942], ["Brazil", 8515767, 212559417], ["India", 3166391, 1380004385]] # calculate population density for countries for i in range(len(countries)): if type(countries[i]) is list: print(countries[i][0], 'has population density:', round(countries[i][2]/countries[i][1], 2))
copy

Tarefa

In the people list, all the heights are in cm. Transform it into inches (divide by 2.54), round result to 2 digits and print message "Name has height of x inches".

Tarefa

In the people list, all the heights are in cm. Transform it into inches (divide by 2.54), round result to 2 digits and print message "Name has height of x inches".

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 7. Capítulo 1
toggle bottom row

Built-in functions

What if I ask you to define the maximum number in the list? Surely, using acquired knowledge, you can write a loop and check if the current element is greater/less than the previous and rewrite it, otherwise - continue... But in the case of big lists, this approach is very time-harmful... This task can be solved by using some built-in function.

  • min(x, y, ...) - minimum element of x, y, ...
  • max(x, y, ...) - maximum element of x, y, ...
  • abs(x) - absolute value of number
  • round(x, n) - rounds number x to n digits
  • pow(x, n) - x raised to the power n

For example, for countries we can calculate population density (population/area) and round result to 2 digits

1234567
countries = [["USA", 9629091, 331002651], ["Canada", 9984670, 37742154], ["Germany", 357114, 83783942], ["Brazil", 8515767, 212559417], ["India", 3166391, 1380004385]] # calculate population density for countries for i in range(len(countries)): if type(countries[i]) is list: print(countries[i][0], 'has population density:', round(countries[i][2]/countries[i][1], 2))
copy

Tarefa

In the people list, all the heights are in cm. Transform it into inches (divide by 2.54), round result to 2 digits and print message "Name has height of x inches".

Tarefa

In the people list, all the heights are in cm. Transform it into inches (divide by 2.54), round result to 2 digits and print message "Name has height of x inches".

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 7. Capítulo 1
toggle bottom row

Built-in functions

What if I ask you to define the maximum number in the list? Surely, using acquired knowledge, you can write a loop and check if the current element is greater/less than the previous and rewrite it, otherwise - continue... But in the case of big lists, this approach is very time-harmful... This task can be solved by using some built-in function.

  • min(x, y, ...) - minimum element of x, y, ...
  • max(x, y, ...) - maximum element of x, y, ...
  • abs(x) - absolute value of number
  • round(x, n) - rounds number x to n digits
  • pow(x, n) - x raised to the power n

For example, for countries we can calculate population density (population/area) and round result to 2 digits

1234567
countries = [["USA", 9629091, 331002651], ["Canada", 9984670, 37742154], ["Germany", 357114, 83783942], ["Brazil", 8515767, 212559417], ["India", 3166391, 1380004385]] # calculate population density for countries for i in range(len(countries)): if type(countries[i]) is list: print(countries[i][0], 'has population density:', round(countries[i][2]/countries[i][1], 2))
copy

Tarefa

In the people list, all the heights are in cm. Transform it into inches (divide by 2.54), round result to 2 digits and print message "Name has height of x inches".

Tarefa

In the people list, all the heights are in cm. Transform it into inches (divide by 2.54), round result to 2 digits and print message "Name has height of x inches".

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

What if I ask you to define the maximum number in the list? Surely, using acquired knowledge, you can write a loop and check if the current element is greater/less than the previous and rewrite it, otherwise - continue... But in the case of big lists, this approach is very time-harmful... This task can be solved by using some built-in function.

  • min(x, y, ...) - minimum element of x, y, ...
  • max(x, y, ...) - maximum element of x, y, ...
  • abs(x) - absolute value of number
  • round(x, n) - rounds number x to n digits
  • pow(x, n) - x raised to the power n

For example, for countries we can calculate population density (population/area) and round result to 2 digits

1234567
countries = [["USA", 9629091, 331002651], ["Canada", 9984670, 37742154], ["Germany", 357114, 83783942], ["Brazil", 8515767, 212559417], ["India", 3166391, 1380004385]] # calculate population density for countries for i in range(len(countries)): if type(countries[i]) is list: print(countries[i][0], 'has population density:', round(countries[i][2]/countries[i][1], 2))
copy

Tarefa

In the people list, all the heights are in cm. Transform it into inches (divide by 2.54), round result to 2 digits and print message "Name has height of x inches".

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 7. Capítulo 1
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt