*args en Python created by parzibyte - https://repl.it/@parzibyte/args-en-Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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