Skip to content

Commit 3a58755

Browse files
authored
Merge pull request #8928 from Irenetitor/pr/05-Python
05-Python
2 parents f402a01 + bb00458 commit 3a58755

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#Value and Reference
2+
3+
# Data types passed by value
4+
5+
my_int_a = 5
6+
my_int_b = my_int_a # my_int_b gets a *copy* of the value 5
7+
my_int_b = 15 # changing my_int_b does NOT affect my_int_a
8+
9+
print(my_int_a) # 5
10+
print(my_int_b) # 15
11+
12+
# Data types passed by reference
13+
14+
my_list_a = [1, 2]
15+
my_list_b = my_list_a # both point to the same list in memory
16+
my_list_b.append(3) # modifies the *same* object
17+
18+
print(my_list_a) # [1, 2, 3]
19+
print(my_list_b) # [1, 2, 3]
20+
21+
# Functions with data passed by value
22+
23+
def my_int_func(my_int: int):
24+
my_int = 100 # Inside function: 100
25+
print("Inside function:", my_int)
26+
27+
my_int_c = 50
28+
my_int_func(my_int_c)
29+
print("Outside function:", my_int_c) # Outside function: 50
30+
31+
# Functions with data passed by reference
32+
33+
def my_list_func(my_list: list):
34+
my_list.append(3) # modifies the original list
35+
my_list_d = my_list
36+
my_list_d.append(4) # also modifies the same object
37+
print("Inside function:", my_list) # [1, 2, 3, 4]
38+
print("Alias variable:", my_list_d) # [1, 2, 3, 4]
39+
40+
my_list_c = [1, 2]
41+
my_list_func(my_list_c)
42+
print("Outside function:", my_list_c) # [1, 2, 3, 4]
43+
44+
45+
#Extra
46+
47+
bon1 = 12
48+
bon2 = 32
49+
50+
def pro_v(bon_1:int, bon_2:int):
51+
yum = bon_1
52+
bon_1 = bon_2
53+
bon_2 = yum
54+
return bon_1, bon_2
55+
56+
bon3, bon4 = pro_v(bon1, bon2)
57+
print(f"{bon1}, {bon2}")
58+
print(f"{bon3}, {bon4}")
59+
60+
#--------------------------------------------------------------------------
61+
62+
bun1 = [23, 56]
63+
bun2 = [21, 45]
64+
65+
def pro_r(bun_1:list, bun_2:list):
66+
yum = bun_1
67+
bun_1 = bun_2
68+
bun_2 = yum
69+
return bun_1, bun_2
70+
71+
bun3, bun4 = pro_v(bun1, bun2)
72+
print(f"{bun1}, {bun2}")
73+
print(f"{bun3}, {bun4}")

0 commit comments

Comments
 (0)