Skip to content

Commit 57d640d

Browse files
committed
#2 - Python
1 parent 187f5f7 commit 57d640d

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Función simple
2+
print("\nFunción simple")
3+
def greet():
4+
print("Hello!")
5+
6+
greet()
7+
8+
# Función con retorno
9+
print("\nFunción con retorno")
10+
def greet_return():
11+
return "Hello with return!"
12+
13+
args = greet_return()
14+
print(args) # or print(greet_return())
15+
16+
# Función con argumento
17+
print("\nFunción con argumento")
18+
def greet_args(args):
19+
print(args)
20+
21+
greet_args("Hello with args!")
22+
23+
# Función con argumento definido
24+
print("\nFunción con argumento definido")
25+
def greet_args_default(args = "Hello with default args!"):
26+
print(args)
27+
28+
greet_args_default()
29+
greet_args_default("Hello again.")
30+
31+
# Función con argumento y retorno
32+
print("\nFunción con argumento y retorno")
33+
def greet_args_return(args):
34+
return "Hello with args and return, "+ args + "!"
35+
36+
print(greet_args_return("Python"))
37+
38+
# Función con retorno de varios valores
39+
print("\nFunción con retorno de varios valores")
40+
def greet_multiple_return():
41+
return "Hello ", "with ", "multiple ", "return!"
42+
43+
a, b, c, d = greet_multiple_return()
44+
print(a + b + c + d)
45+
46+
# Función con número variable de argumentos
47+
print("\nFunción con número variable de argumentos")
48+
def greet_variable_args(*args):
49+
for name in args:
50+
print(f"Hello {name}")
51+
52+
greet_variable_args("Python ", "C ", "Java ")
53+
54+
# Función con número variable de argumentos con palabra clave
55+
print("\nFunción con número variable de argumentos con palabra clave")
56+
def greet_variable_args_key(**args):
57+
for key, name in args.items():
58+
print(f"Hello {name} ({key})")
59+
60+
greet_variable_args_key(language1 = "Python", language2 = "C", language3 = "Java")
61+
62+
# Funciones dentro de funciones
63+
print("\nFunciones dentro de funciones")
64+
def outer_function():
65+
print("I'm in the outer function.")
66+
def inner_function():
67+
print("I'm in the inner function.")
68+
inner_function() # Sin está llamada inner_function no se ejecuta
69+
70+
outer_function()
71+
72+
# Funciones built-in
73+
print("\nFunciones built-in")
74+
print(len("Hello")) # length
75+
print(type("Hello")) # class
76+
print("Hello".upper()) # uppercase
77+
print(int("10")) # convert to int
78+
print(max([1, 5, 3]))
79+
print(round(3.14159, 2)) # round with 2 decimals
80+
print(list(range(5))) # list from 0 to 4
81+
print(ord('A')) # char to unicode
82+
print(chr(65)) # unicode to char
83+
print(list("Hello")) # ['H', 'e', 'l', 'l', 'o']
84+
print(set([1, 2, 2, 3])) # {1, 2, 3}: deletes repeated numbers
85+
86+
# Variables locales y globales
87+
print("\nVariables locales y globales")
88+
89+
global_variable = "Global variable" # outside of the function
90+
91+
def local():
92+
local_variable = "Local variable"
93+
94+
def hello_python():
95+
print("Hello " + global_variable.lower())
96+
# print("Hello " + local_variable.lower()) # error: cannot be accessed from outside the function
97+
98+
hello_python()
99+
100+
'''
101+
Crea una función que reciba dos parámetros de tipo cadena de texto y retorne un número.
102+
* - La función imprime todos los números del 1 al 100. Teniendo en cuenta que:
103+
* - Si el número es múltiplo de 3, muestra la cadena de texto del primer parámetro.
104+
* - Si el número es múltiplo de 5, muestra la cadena de texto del segundo parámetro.
105+
* - Si el número es múltiplo de 3 y de 5, muestra las dos cadenas de texto concatenadas.
106+
* - La función retorna el número de veces que se ha impreso el número en lugar de los textos.
107+
'''
108+
print("\nEjercicio de dificultad extra")
109+
110+
def funct(param1,param2):
111+
number = 0
112+
for i in range(1, 101):
113+
if i % 3 == 0 and i % 5 == 0:
114+
print(param1+param2)
115+
elif i % 3 == 0:
116+
print(param1)
117+
elif i % 5 == 0:
118+
print(param2)
119+
else:
120+
print(i)
121+
number += 1
122+
return number
123+
124+
print(funct("tres","cinco"))

0 commit comments

Comments
 (0)