Skip to content

Commit 588c6fc

Browse files
committed
#2 - Python
1 parent 7bc2422 commit 588c6fc

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
"""
2+
Funciones definidas por el usuario
3+
"""
4+
5+
# Simple
6+
7+
def saludo():
8+
print("Hola, Python!")
9+
10+
saludo()
11+
12+
# Con retorno
13+
14+
def devuelve_saludo() -> str:
15+
return "Hola, Python!"
16+
17+
saludar = devuelve_saludo()
18+
print(saludar)
19+
20+
# Con un argumento
21+
def args_saludo(saludo: str, nombre: str) -> str:
22+
print(f"{saludo}, {nombre}")
23+
24+
args_saludo("Hi", "Marcos")
25+
26+
# Con un argumento predeterminado
27+
28+
def default_arg_saludo(nombre:str = "Amigo") -> str:
29+
print(f"Hola, {nombre}!")
30+
31+
default_arg_saludo("Marco")
32+
default_arg_saludo()
33+
34+
# Con argumentos y retorno
35+
36+
def return_args_saludo(saludo: str, nombre: str) -> str:
37+
return f"{saludo}, {nombre}!"
38+
39+
print(return_args_saludo("Hi", "Marcos"))
40+
41+
# Con retorno de varios valores
42+
43+
def multiple_return_saludo():
44+
return "Hola", "Python"
45+
46+
saluda, nombre = multiple_return_saludo()
47+
print(saluda)
48+
print(nombre)
49+
50+
# Con un número variable de argumentos
51+
52+
def variable_arg_greet(*names):
53+
for name in names:
54+
print(f"Hola, {name}")
55+
variable_arg_greet("Python", "Marcos", "Dibu", "Comunidad")
56+
57+
# Con un número variable de argumentos con palabra clave
58+
59+
def variable_key_arg_greet(**names):
60+
for param, name in names.items():
61+
print(f"{name} ({param})")
62+
63+
variable_key_arg_greet(language="Python",name= "Marcos",age= 21)
64+
65+
"""
66+
Funciones dentro de funciones
67+
"""
68+
69+
def outer_function():
70+
def inner_function():
71+
print("Función interna: Hola, Python!")
72+
inner_function() # Hay que llamarla para que se ejecute
73+
74+
outer_function()
75+
76+
"""
77+
Funciones del lenguaje (built-in)
78+
"""
79+
80+
print(len("Marcos"))
81+
print(type(21))
82+
print("Marcos".upper())
83+
84+
"""
85+
Varialbes locales y globales
86+
"""
87+
88+
global_var = "Python"
89+
90+
print(global_var)
91+
92+
def hello_python():
93+
local_var = "Hola"
94+
print(f"{local_var}, {global_var}")
95+
96+
97+
print(global_var)
98+
# print(local_var) No se puede acceder desde fuera de la función
99+
100+
hello_python()
101+
102+
103+
"""
104+
Extra
105+
"""
106+
107+
def extra(texto_1: str, texto_2: str) -> int:
108+
numers = 0
109+
for i in range(1, 101):
110+
if i % 3 == 0 and i % 5 == 0:
111+
print(f"{texto_1}{texto_2}")
112+
elif i % 3 == 0:
113+
print(f"{texto_1}")
114+
elif i % 5 == 0:
115+
print(f"{texto_2}")
116+
else:
117+
print(i)
118+
numers += 1
119+
120+
return f"Cantidad de numeros impresos: {numers}"
121+
122+
print(extra("Fizz", "Buzz"))

0 commit comments

Comments
 (0)