Advertisement
teslariu

funciones1

Oct 7th, 2023
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # funciones
  4. # integradas: print(), del(), list(), int(), float(),range(),sum(),min() ,max()
  5. #  lambda, filter, map, zip
  6.  
  7. # funciones propias del usuario
  8. """
  9. def imprimir(lista):
  10.     for item in lista:
  11.         print(item)
  12.        
  13.        
  14. nombres = ["Ale","Ana"]
  15. numeros = [1,2,3]
  16.  
  17. imprimir(nombres)
  18. imprimir(numeros)
  19. imprimir("Soy una frase")
  20.          
  21.  
  22. def suma(a,b):
  23.     return a + b
  24.    
  25. total = suma(23,21)
  26. print(suma(23,21))
  27.    
  28. # funcion con parametros x defecto
  29. def saludar(edad, nombre="desconocido"):
  30.     print(f"Hola {nombre} de {edad} años")
  31.    
  32. saludar(25,"Pepe")
  33. saludar(18)
  34.  
  35. ### Ambito global y pasaje por referencia  
  36. def f():
  37.     global a  3 # pasaje por referencia
  38.     a = 3
  39.     print(f"a dentro de la funcion: {a}")
  40.    
  41. a = 10
  42. f()
  43. print(f"a fuera de la funcion: {a}")
  44.  
  45.  
  46. # pasaje por valor
  47. def f(a):
  48.     a=0
  49.     print(f"a adentro: {a}")
  50.    
  51. a = 10
  52. f(a)
  53. print(f"a fuera: {a}")
  54.  
  55.  
  56. # EL COMPORTAMIENTO (por valor, por referencia) ESTA DEFINIDO POR EL tipo
  57. # de VARIABLE
  58. print("comportamiento POR REFERENCIA de una lista")
  59. def f(a):
  60.     a.append(100)
  61.     print(f"Lista a adentro: {a}")
  62.    
  63. a = [10,20,30]
  64. f(a)
  65. print(f"Lista a fuera: {a}")
  66.  
  67. print("\ncomportamiento POR VALOR de una lista")
  68. def f(a):
  69.     a = []  # CREO UNA LISTA NUEVA LOCAL
  70.     print(f"Lista a adentro: {a}")
  71.    
  72. a = [10,20,30]
  73. f(a)
  74. print(f"Lista a fuera: {a}")
  75.  
  76.  
  77. # argumentos de long variable:
  78. def sumar(*args):
  79.     print(args)
  80.     print(type(args))
  81.     total = 0
  82.     for n in args:
  83.         total += n
  84.     return total
  85.  
  86.  
  87. print(sumar(1,2,3,4))
  88. print()
  89.  
  90.  
  91. # argumentos con keywords
  92. def sumar(**kwargs):
  93.     print(type(kwargs))
  94.     print(kwargs)
  95.     total = 0
  96.     for n in kwargs:
  97.         total += kwargs[n]
  98.     return total
  99.    
  100. print(sumar(a=10, b=20, c=30))
  101.  
  102. """
  103. # orden de los argumentos
  104. def f(a,b,c,d,*args,x=100,y=200,z=300,**kwargs):
  105.     print(f"a:{a}")
  106.     print(f"b:{b}")
  107.     print(f"c:{c}")
  108.     print(f"d:{d}")
  109.     print(f"args:{args}")
  110.     print(f"x:{x}")
  111.     print(f"y:{y}")
  112.     print(f"z:{z}")
  113.     print(f"kwargs:{kwargs}")
  114.    
  115.    
  116. f(1,2,3,4,"Pepe","Ana","Lucia",p="Juan",q=12536878)
  117.  
  118.    
  119.    
  120.    
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement