Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Writing your own functions (4/5) | Functions
Learn Python from Scratch
course content

Contenido del 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

bookWriting your own functions (4/5)

By this time we always returned some information in the function body. But rarely we don't need to return and save any values after function execution, for example, if we need to just print something.

Remember our dictionary with some countries' information? Let's write a function that will print all the information we needed with only two arguments: dictionary and country name.

1234567891011121314
# data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) # define a function def country_information(d, name): print("Country:", name) print("Area:", d[name][0], 'sq km') print("Population:", round(d[name][1]/1000000, 2), 'mln') # test our function country_information(countries_dict, "Brazil") country_information(countries_dict, "Germany")
copy

Tarea

Define a function people_information with one argument d (this will be people_d dictionary!) and name (which will be the name of the person - key in this dictionary) which prints the following output:

Name:
Age:
Height:

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 7. Capítulo 5
toggle bottom row

bookWriting your own functions (4/5)

By this time we always returned some information in the function body. But rarely we don't need to return and save any values after function execution, for example, if we need to just print something.

Remember our dictionary with some countries' information? Let's write a function that will print all the information we needed with only two arguments: dictionary and country name.

1234567891011121314
# data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) # define a function def country_information(d, name): print("Country:", name) print("Area:", d[name][0], 'sq km') print("Population:", round(d[name][1]/1000000, 2), 'mln') # test our function country_information(countries_dict, "Brazil") country_information(countries_dict, "Germany")
copy

Tarea

Define a function people_information with one argument d (this will be people_d dictionary!) and name (which will be the name of the person - key in this dictionary) which prints the following output:

Name:
Age:
Height:

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 7. Capítulo 5
toggle bottom row

bookWriting your own functions (4/5)

By this time we always returned some information in the function body. But rarely we don't need to return and save any values after function execution, for example, if we need to just print something.

Remember our dictionary with some countries' information? Let's write a function that will print all the information we needed with only two arguments: dictionary and country name.

1234567891011121314
# data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) # define a function def country_information(d, name): print("Country:", name) print("Area:", d[name][0], 'sq km') print("Population:", round(d[name][1]/1000000, 2), 'mln') # test our function country_information(countries_dict, "Brazil") country_information(countries_dict, "Germany")
copy

Tarea

Define a function people_information with one argument d (this will be people_d dictionary!) and name (which will be the name of the person - key in this dictionary) which prints the following output:

Name:
Age:
Height:

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

By this time we always returned some information in the function body. But rarely we don't need to return and save any values after function execution, for example, if we need to just print something.

Remember our dictionary with some countries' information? Let's write a function that will print all the information we needed with only two arguments: dictionary and country name.

1234567891011121314
# data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) # define a function def country_information(d, name): print("Country:", name) print("Area:", d[name][0], 'sq km') print("Population:", round(d[name][1]/1000000, 2), 'mln') # test our function country_information(countries_dict, "Brazil") country_information(countries_dict, "Germany")
copy

Tarea

Define a function people_information with one argument d (this will be people_d dictionary!) and name (which will be the name of the person - key in this dictionary) which prints the following output:

Name:
Age:
Height:

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 7. Capítulo 5
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt