Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende *args | An Unknown Number of Arguments
Python Functions: From Zero to Hero

book
*args

*args is needed when we want to pass an unknown number of unnamed arguments. If we put * before the name of the variable, this name will take not one argument, but several. Arguments are passed as a tuple and are available inside the function under the same name as the parameter name, only without *. Let's look at the example.

def grocery_cart(*prices_of_all_fruits):
total_price = 0
for i in prices_of_all_fruits:
total_price = total_price + i
print(total_price)
grocery_cart(10, 7, 12, 13)
123456
def grocery_cart(*prices_of_all_fruits): total_price = 0 for i in prices_of_all_fruits: total_price = total_price + i print(total_price) grocery_cart(10, 7, 12, 13)
copy

It's time to practice.

Tarea

Swipe to start coding

You have to implement a function, named multiply_elements, that will take in an unknown number of arguments and multiply all of them together. Run created a function for this set of numbers:

  • (3, 6, 1, 2)

Solución

def multiply_elements(*numbers):
product = 1
for i in numbers:
product = product * i
print(product)


multiply_elements(3, 6, 1, 2)

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 2
# Write your code below
def multiply_elements(*numbers):
product = ___
for ___ in ___:
product =___ * ___
print(product)

# Testing
multiply_elements(3, 6, 1, 2)

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt