Advertisement
teslariu

funciones

Aug 10th, 2023
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Este script implementa varios ejemplos para visualizar como maneja Python
  6. las funciones
  7.  
  8. def sumar(a,b):
  9.    return a + b
  10.    
  11.    
  12. print(sumar(10,25))
  13.  
  14. def imprimir(l):
  15.    for item in l:
  16.        print(item)
  17.        
  18.        
  19. imprimir([1,2,3])
  20. imprimir(["Ale","Juana"])
  21.  
  22.  
  23.  
  24. # funciones con argumentos por defecto
  25. def saludar(edad,nombre="desconocido"):
  26.    print(f"Hola {nombre}, tenés {edad} años")
  27.    
  28. saludar(22, "Ana")
  29. saludar(55)
  30.  
  31. # argumentos de longitud variable
  32. # no se recomienda
  33. def sumar(numeros):
  34.    total = 0
  35.    for n in numeros:
  36.        total += n
  37.    return total
  38.  
  39.  
  40. numeros = [1,25,14,78,10]
  41.  
  42. print(sumar(numeros))  
  43.  
  44. def sumar(*args):
  45.    total = 0
  46.    for n in args:
  47.        total += n
  48.    return total
  49.    
  50.  
  51.  
  52.  
  53. print(sumar(1,2,102,-25,14587))  
  54.  
  55.  
  56.  
  57. #  argumentos con keyword
  58. def sumar(**kwargs):
  59.    total = 0
  60.    for n in kwargs:
  61.        total += kwargs[n]
  62.    return total
  63.  
  64. print(sumar(a=1, b=2, c=102, x=-25,  d=14587))
  65.  
  66.  
  67. # orden de argumentos
  68. def f(a,b,c,*args,d=15,e=19,**kwargs):
  69.    print(a)
  70.    print(b)
  71.    print(c)
  72.    print(args)
  73.    print(d)
  74.    print(e)
  75.    print(kwargs)
  76.    
  77. f("Soy a", "Soy B", "Soy C", True, 10.25,"Chau", 11, 5,x=1212223, y=1234414)
  78.  
  79. # funciones lambda o anonimas o inline:
  80. def sumar(a,b):
  81.    print(a+b)
  82.    
  83. f = lambda a,b : a + b  
  84. print(f(4,1))  
  85.  
  86.  
  87. # Funciones de orden superior: son funciones que llaman a otras funciones
  88. # generalmenet, se escriben funciones como lambda para ser usadas por
  89. # otras
  90.  
  91. # Ej: imprimir la siguiente lista de nombres correctamente
  92. # Usaremos funciones integradas map() y lambda
  93. # map(funcion, coleccion)
  94.  
  95. nombres = ['jUaN ', '    aNa   ', '  pABlO', 'tiTo   ', '   juANA']
  96.  
  97. lista_correcta = list(map(lambda nombre:nombre.strip().lower().capitalize(), nombres))
  98. print(lista_correcta)
  99.  
  100.  
  101. numeros = [12,25,33,25,25,14,15]
  102.  
  103. def cubos(n):
  104.    return n**3
  105.  
  106. cubos_numeros = list(map(cubos,numeros))
  107.  
  108. print(cubos_numeros)
  109. """
  110. # filter es como map pero FILTRA resultados True or False
  111.  
  112. def bisiesto(anio):
  113.     if not anio%400 or (not anio%4 and anio%100):
  114.         return True
  115.     return False
  116.  
  117. os = [0,12,158,1250,1685,1800,1810,1996,2000,2002,2004,2006,2005,2020]
  118. años_bisiestos = list(filter(bisiesto,os))
  119.  
  120. print(años_bisiestos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement