Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created December 20, 2018 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/3c344f8da80f7fddb818e663889a72c7 to your computer and use it in GitHub Desktop.
Save parzibyte/3c344f8da80f7fddb818e663889a72c7 to your computer and use it in GitHub Desktop.
*args en Python created by parzibyte - https://repl.it/@parzibyte/args-en-Python
"""
Explicar *args en Python con una
función
@author parzibyte
"""
def promedio(*numeros):
#Saber cuántos elementos hay, imaginemos que los argumentos son un arreglo
cantidad_de_elementos = len(numeros)
#Evitar división entre 0
if cantidad_de_elementos <= 0:
return 0
suma = 0
# Justo aquí iteramos los argumentos o *args
for numero in numeros:
suma += numero
return suma / cantidad_de_elementos
print(promedio(80, 97, 114, 122, 105, 98, 117, 103)) #104.5
print(promedio(50, 20)) # 35
print(promedio(1, 5)) # 3
print(promedio(20, 1500)) # 760
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment