Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created Dec 20, 2018
Embed
What would you like to do?
*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