File tree 1 file changed +99
-0
lines changed
Roadmap/02 - FUNCIONES Y ALCANCE/python
1 file changed +99
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Funciones #
2
+
3
+ # Simples
4
+ def jump ():
5
+ print ("Salto" )
6
+
7
+ jump ()
8
+
9
+ # Con retorno
10
+
11
+ def collect ():
12
+ return "Objeto"
13
+
14
+ print (collect ())
15
+
16
+ # Con argumentos
17
+
18
+ def attack (damage , name ):
19
+ return f"Has realizado { damage } de daño al enemigo llamado { name } "
20
+
21
+ print (attack (20 , "Paco" ))
22
+ print (attack (name = "Paco" , damage = 20 ))
23
+
24
+ # Con argumentos predeterminados
25
+
26
+ def salute (name = "a todos" ):
27
+ return f"¡Hola { name } !"
28
+
29
+ print (salute ())
30
+ print (salute ("Juan" ))
31
+
32
+ # Retorno de varios valores
33
+
34
+ def eat ():
35
+ return "Manzana" , "Platano"
36
+
37
+ food1 , food2 = eat ()
38
+
39
+ print (food1 )
40
+ print (food2 )
41
+
42
+ # Con un numero NO especificado de argumentos
43
+
44
+ def sum (* number ):
45
+ total = 0
46
+ for n in number :
47
+ total += n
48
+ return total
49
+
50
+ print (sum (2 , 3 , 4 , 5 ))
51
+
52
+ # Funcion dentro de una funcion #
53
+
54
+ def a ():
55
+ def b ():
56
+ print ("Test" )
57
+ b ()
58
+
59
+ a ()
60
+
61
+ # Funciones de Python #
62
+ print (len ("Prueba" ))
63
+ print (type (22 ))
64
+ print (int (23.52 ))
65
+ print ("PRUEBA" .lower ())
66
+ print ("PRUEBA" .capitalize ())
67
+
68
+ # Variables locales y globales #
69
+
70
+ # Variable global
71
+ var = 8 # Las variables declaradas fuera de una función se consideran globales
72
+
73
+ def test ():
74
+ # Variable local
75
+ local = 3
76
+
77
+ print (var )
78
+
79
+ test ()
80
+ # print(local) # Da error porque estamos intentando acceder a una variable local, es decir, fue declarada dentro de una función
81
+
82
+ # Extra #
83
+
84
+ def count (str1 , str2 ):
85
+ n = 0
86
+ for i in range (1 , 101 ):
87
+ if i % 3 == 0 and i % 5 == 0 :
88
+ print (str1 + str2 )
89
+ elif i % 3 == 0 :
90
+ print (str1 )
91
+ elif i % 5 == 0 :
92
+ print (str2 )
93
+ else :
94
+ print (i )
95
+ n += 1
96
+
97
+ return n
98
+
99
+ print (count ("Primero" , "Segundo" ))
You can’t perform that action at this time.
0 commit comments