Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
High-Order Functions | Function as an Argument
Intermediate Python: Arguments, Scopes and Decorators
course content

Contenido del Curso

Intermediate Python: Arguments, Scopes and Decorators

Intermediate Python: Arguments, Scopes and Decorators

1. Packing and Unpacking
2. Arguments in Function
3. Function as an Argument
4. Variable Scope
5. Decorators

bookHigh-Order Functions

A high-order function in programming is a function that can either take other functions as arguments or return a function as its result, or both. This concept is closely related to first-class functions, which are treated like any other variable in a language (can be stored in a variable, passed as an argument to a function, or used in control statements). High-order functions are a key feature in functional programming, enabling more abstract or general-purpose code, facilitating code reuse, and making it easier to manipulate functions or data.

123456789
def square(x): return x * x numbers = [1, 2, 3, 4, 5] squared_numbers = map(square, numbers) # Convert the map object to a list squared_numbers_list = list(squared_numbers) print(squared_numbers_list)
copy

A classic example of a high-order function is the map function in Python. This function takes two arguments: a function and an iterable (like a list). It applies the given function to each item of the iterable and returns a map object (which can be easily converted into a list or another iterable type).

In this example:

  • square is a simple function that squares its input.
  • map is the high-order function that takes square and a list of numbers as arguments.
  • The result is each number in the list numbers squared.

Tarea

Suppose you have a list of temperatures in Celsius, and you want to convert all of them to Fahrenheit. The goal will be to apply a custom transformation to each element in the list using map.

  • Create a list of numeric values representing temperatures in Celsius.
  • Define a function that converts a temperature from Celsius to Fahrenheit.
  • Apply the custom function to each element in the list using map.
  • Convert the result from map into a list and print it to see the converted temperatures.

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 3. Capítulo 1
toggle bottom row

bookHigh-Order Functions

A high-order function in programming is a function that can either take other functions as arguments or return a function as its result, or both. This concept is closely related to first-class functions, which are treated like any other variable in a language (can be stored in a variable, passed as an argument to a function, or used in control statements). High-order functions are a key feature in functional programming, enabling more abstract or general-purpose code, facilitating code reuse, and making it easier to manipulate functions or data.

123456789
def square(x): return x * x numbers = [1, 2, 3, 4, 5] squared_numbers = map(square, numbers) # Convert the map object to a list squared_numbers_list = list(squared_numbers) print(squared_numbers_list)
copy

A classic example of a high-order function is the map function in Python. This function takes two arguments: a function and an iterable (like a list). It applies the given function to each item of the iterable and returns a map object (which can be easily converted into a list or another iterable type).

In this example:

  • square is a simple function that squares its input.
  • map is the high-order function that takes square and a list of numbers as arguments.
  • The result is each number in the list numbers squared.

Tarea

Suppose you have a list of temperatures in Celsius, and you want to convert all of them to Fahrenheit. The goal will be to apply a custom transformation to each element in the list using map.

  • Create a list of numeric values representing temperatures in Celsius.
  • Define a function that converts a temperature from Celsius to Fahrenheit.
  • Apply the custom function to each element in the list using map.
  • Convert the result from map into a list and print it to see the converted temperatures.

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 3. Capítulo 1
toggle bottom row

bookHigh-Order Functions

A high-order function in programming is a function that can either take other functions as arguments or return a function as its result, or both. This concept is closely related to first-class functions, which are treated like any other variable in a language (can be stored in a variable, passed as an argument to a function, or used in control statements). High-order functions are a key feature in functional programming, enabling more abstract or general-purpose code, facilitating code reuse, and making it easier to manipulate functions or data.

123456789
def square(x): return x * x numbers = [1, 2, 3, 4, 5] squared_numbers = map(square, numbers) # Convert the map object to a list squared_numbers_list = list(squared_numbers) print(squared_numbers_list)
copy

A classic example of a high-order function is the map function in Python. This function takes two arguments: a function and an iterable (like a list). It applies the given function to each item of the iterable and returns a map object (which can be easily converted into a list or another iterable type).

In this example:

  • square is a simple function that squares its input.
  • map is the high-order function that takes square and a list of numbers as arguments.
  • The result is each number in the list numbers squared.

Tarea

Suppose you have a list of temperatures in Celsius, and you want to convert all of them to Fahrenheit. The goal will be to apply a custom transformation to each element in the list using map.

  • Create a list of numeric values representing temperatures in Celsius.
  • Define a function that converts a temperature from Celsius to Fahrenheit.
  • Apply the custom function to each element in the list using map.
  • Convert the result from map into a list and print it to see the converted temperatures.

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!

A high-order function in programming is a function that can either take other functions as arguments or return a function as its result, or both. This concept is closely related to first-class functions, which are treated like any other variable in a language (can be stored in a variable, passed as an argument to a function, or used in control statements). High-order functions are a key feature in functional programming, enabling more abstract or general-purpose code, facilitating code reuse, and making it easier to manipulate functions or data.

123456789
def square(x): return x * x numbers = [1, 2, 3, 4, 5] squared_numbers = map(square, numbers) # Convert the map object to a list squared_numbers_list = list(squared_numbers) print(squared_numbers_list)
copy

A classic example of a high-order function is the map function in Python. This function takes two arguments: a function and an iterable (like a list). It applies the given function to each item of the iterable and returns a map object (which can be easily converted into a list or another iterable type).

In this example:

  • square is a simple function that squares its input.
  • map is the high-order function that takes square and a list of numbers as arguments.
  • The result is each number in the list numbers squared.

Tarea

Suppose you have a list of temperatures in Celsius, and you want to convert all of them to Fahrenheit. The goal will be to apply a custom transformation to each element in the list using map.

  • Create a list of numeric values representing temperatures in Celsius.
  • Define a function that converts a temperature from Celsius to Fahrenheit.
  • Apply the custom function to each element in the list using map.
  • Convert the result from map into a list and print it to see the converted temperatures.

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