Skip to content

Commit 1b0919d

Browse files
committed
mouredev#2 - Python
1 parent 11cb8e0 commit 1b0919d

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# 02
2+
# Primero que son funciones basicas que representen las diferentes posibilidades de
3+
4+
5+
# funcion con parametro
6+
def mi_funcion(x): # Funcion con parametro
7+
#codigo
8+
print("what's up bro?")
9+
return
10+
11+
mi_funcion(1) # si o si toca enviarle un parametro para que se ejecute asi no haga nada con ese parametro
12+
13+
# funcion sin parametro
14+
def mi_funcion_sin_parametro():
15+
print("Aqui no se usa parametro")
16+
return
17+
18+
mi_funcion_sin_parametro()
19+
20+
# funcion sin parametro pero con retorno
21+
def funcion_sin_parametro_pero_con_retorno(): # las funciones puedes nombrarse de forma larga
22+
return "que pedo"
23+
24+
x = funcion_sin_parametro_pero_con_retorno() # aqui la funcion se crea para que retorne algo
25+
print(x)
26+
27+
# funcion con parametro y retorno
28+
def sumar(a, b):
29+
return a + b
30+
31+
print(sumar(2, 5))
32+
33+
# funciones con parametros por defecto
34+
def saludar(name="invitado"):
35+
print(f"Hola, {name}")
36+
37+
saludar()
38+
saludar("boludo")
39+
40+
# funciones con parametros variables
41+
def mostrar_args(*args, **kwargs):
42+
print("args:", args)
43+
print("kwargs:", kwargs)
44+
45+
mostrar_args(1, 2, 3, name="Brian", age=24)
46+
47+
"""
48+
Por lo tanto los parametros son datos que la funcion necesita.
49+
Mientras que el retorno es el resultado que entrega la funcion
50+
"""
51+
52+
# funciones anidadas
53+
def funcion_externa():
54+
print("Soy la función externa")
55+
56+
def funcion_interna():
57+
print("Soy la función interna")
58+
59+
# Llamamos a la interna dentro de la externa
60+
funcion_interna()
61+
62+
# Ejecuto la externa
63+
funcion_externa()
64+
# funcion_interna() son puede ser llamada fuera de la funcion mas grande
65+
66+
# funcion interna con retorno
67+
def externa(x):
68+
def interna(y):
69+
return y * 2
70+
return interna(x)
71+
72+
print(externa(5)) # 10
73+
74+
# funcion closure
75+
def multiplicador(n):
76+
def multiplicar(x):
77+
return x * n
78+
return multiplicar
79+
80+
doble = multiplicador(2)
81+
triple = multiplicador(3)
82+
83+
print(doble(5)) # 10
84+
print(triple(5)) # 15
85+
print(doble(2)) # 4
86+
87+
# Ejemplo random
88+
def caluladora_simple(x, tipo):
89+
def suma(y):
90+
return x + y
91+
def restar(y):
92+
return x - y
93+
def multiplicar(y):
94+
return x * y
95+
def dividir(y):
96+
return x / y
97+
# return {"sumar": suma, "restar": restar, "multiplicar": multiplicar, "dividir": dividir}
98+
if tipo == "suma":
99+
return suma
100+
elif tipo == "restar":
101+
return restar
102+
elif tipo == "multiplicar":
103+
return multiplicar
104+
elif tipo == "dividir":
105+
return dividir
106+
107+
x = caluladora_simple(2, "suma")
108+
y = x(3)
109+
print(y)
110+
111+
x = caluladora_simple(2, "restar")
112+
y = x(3)
113+
print(y)
114+
115+
x = caluladora_simple(2, "multiplicar")
116+
y = x(3)
117+
print(y)
118+
119+
x = caluladora_simple(2, "dividir")
120+
y = x(3)
121+
print(y)
122+
123+
124+
125+
def calculadora(n):
126+
def sumar(x):
127+
return x + n
128+
def multiplicar(x):
129+
return x * n
130+
def restar(x):
131+
return x - n
132+
133+
# Retorno un diccionario con varias funciones
134+
return {"sumar": sumar, "multiplicar": multiplicar, "restar": restar}
135+
136+
ops = calculadora(10)
137+
138+
print(ops ) # 15
139+
print(ops ) # 50
140+
print(ops ) # -5
141+
142+
143+
144+
x = 10 # variable global
145+
146+
def mostrar():
147+
print(x) # puede acceder a la global
148+
149+
mostrar() # 10
150+
print(x) # 10
151+
152+
###
153+
def funcion():
154+
y = 5 # variable local
155+
print(y)
156+
157+
funcion() # 5
158+
# print(y) # ❌ Error: NameError (no existe fuera de la función)
159+
160+
161+
162+
# Crear una funcion que que reciba 2 para metros de texto y retorne los numeros del 1 al 100
163+
def cadena_num(a, b):
164+
x = 0
165+
y = 0
166+
for i in range (1, 101):
167+
if i % 3 == 0:
168+
x += 1
169+
print(a)
170+
elif i % 5 == 0:
171+
x += 1
172+
print(b)
173+
else:
174+
y += 1
175+
print(i)
176+
return print(f"Numero de numeros impresos: {y}\nNumero de Textos impresos: {x}")
177+
178+
cadena_num("tres", "cinco")

0 commit comments

Comments
 (0)