Skip to content

Commit 557f4b1

Browse files
author
Lioomx
committed
#5 - Python
1 parent 1bf0db3 commit 557f4b1

File tree

1 file changed

+63
-0
lines changed
  • Roadmap/05 - VALOR Y REFERENCIA/python

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
''' Asignación de variables'''
3+
4+
""" POR VALOR """
5+
6+
# Tipos inmutables - int, float, str, tuplas, bool
7+
8+
x = 10
9+
y = x # Se copia el valor de 'x' a 'y'
10+
y = 20 # Cambiar 'y' no afecta a 'x'
11+
12+
print(x)
13+
print(y)
14+
15+
""" POR REFERENCIA """
16+
17+
# Tipos mutables - list, dict, set
18+
19+
a = [1, 2, 3]
20+
b = a # Se asigna la referencia de 'a' a 'b'
21+
b.append(4) # Modificamos la lista usando 'b'
22+
23+
print(a)
24+
print(b)
25+
26+
''' Al ser mutables, tanto 'a' como 'b' apuntan al mismo objeto
27+
y cualquier cambio afecta a ambos
28+
'''
29+
30+
# Copiar por valor
31+
32+
a = [1, 2, 3]
33+
b = a.copy()
34+
b.append(4)
35+
36+
print(a)
37+
print(b)
38+
39+
# Funciones con datos por valor
40+
41+
def my_int_func(my_int: int):
42+
my_int = 20
43+
print(my_int)
44+
45+
my_int_c = 10
46+
my_int_func(my_int_c)
47+
print(my_int_c)
48+
49+
# Funciones con datos por referencia
50+
51+
def my_list_func(my_list: list):
52+
my_list_e = my_list
53+
my_list.append(30)
54+
55+
my_list_d = my_list_e
56+
my_list_d.append(40)
57+
58+
print(my_list_e)
59+
print(my_list_d)
60+
61+
my_list_c = [10, 20]
62+
my_list_func(my_list_c)
63+
print(my_list_c)

0 commit comments

Comments
 (0)