teslariu

long variable

Sep 20th, 2022
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Argumentos de longitud variable
  5. """
  6. def sumar(numeros):
  7.    total = 0
  8.    for numero in numeros:
  9.        total += numero
  10.    return total
  11.    
  12. print(sumar([1,2,3,4,5,6]))
  13.  
  14.  
  15.  
  16. def sumar2(*args):
  17.    total = 0
  18.    for n in args:
  19.        total += n
  20.    return total
  21.    
  22. print(sumar2(1,2,3))
  23.  
  24.  
  25.  
  26. def sumar3(**kwargs):
  27.    total = 0
  28.    for n in kwargs:
  29.        total += kwargs[n]
  30.    return total
  31.  
  32. print(sumar3(a=1, b=2, c=3))
  33. """
  34. # podría hacer esto:
  35. def todo(x,y,z,k=11,*args, w=23, **kwargs):
  36.     print(x)
  37.     print(y)
  38.     print(z)
  39.     print(args)
  40.     print(w)
  41.     print(kwargs)
  42.    
  43. todo(1,2,2,10,11,12, a=1000,b=2000)    
Advertisement
Add Comment
Please, Sign In to add comment