|
| 1 | +### valor y referencia ### |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +## tipos de datos por valor (int, float, str, tuple, bool) |
| 6 | +""" |
| 7 | +por valor implica que cuando se asigna una variable a otra o se pasa como argumento, se copia el valor real del objeto |
| 8 | +en ese momento |
| 9 | +""" |
| 10 | + |
| 11 | +my_int_a = 10 |
| 12 | +my_int_b = 20 |
| 13 | +my_int_b = my_int_a |
| 14 | + |
| 15 | +print(my_int_a) #out 10 |
| 16 | +print(my_int_b) #out 20 |
| 17 | + |
| 18 | +my_int_b += 5 |
| 19 | +print(my_int_b) #out: 15 |
| 20 | + |
| 21 | +my_int_a = 10 |
| 22 | +my_int_b = 20 |
| 23 | +my_int_b = my_int_a |
| 24 | + |
| 25 | +my_int_a = 30 |
| 26 | + |
| 27 | +print(my_int_a) #out: 30 |
| 28 | +print(my_int_b) #out: 10 |
| 29 | + |
| 30 | +#muestra la direccion de memoria |
| 31 | +print(id(my_int_a)) |
| 32 | +print(id(my_int_b)) |
| 33 | + |
| 34 | +# tipos de datos por referencia (list, dict, set) |
| 35 | +""" |
| 36 | +Por referencia implica que las variables contienen una referencia al objeto en memoria, no el valor directamente. |
| 37 | +Esto significa que si se modifica el contenido del objeto usando una de las referencias, los cambios se reflejan en todas |
| 38 | +las referencias. |
| 39 | +En el caso por referencia, No copian su valor, sino que heredan su dirección de memoria. |
| 40 | +""" |
| 41 | +my_list_a = [10, 20] |
| 42 | +my_list_b = [30, 40] |
| 43 | + |
| 44 | +print(my_list_a) |
| 45 | +print(my_list_b) |
| 46 | + |
| 47 | +my_list_b = my_list_a |
| 48 | + |
| 49 | +my_list_a.append(30) |
| 50 | + |
| 51 | +print(my_list_a) |
| 52 | +print(my_list_b) |
| 53 | + |
| 54 | +#muestra la direccion de memoria |
| 55 | +print(id(my_list_a)) |
| 56 | +print(id(my_list_b)) |
| 57 | + |
| 58 | +## funcion que recibe variables por valor: |
| 59 | + |
| 60 | +my_int_c = 10 |
| 61 | + |
| 62 | +def my_int_func(my_int): |
| 63 | + my_int = 20 |
| 64 | + |
| 65 | + return my_int |
| 66 | + |
| 67 | +valor_en_funcion = my_int_func(my_int_func) |
| 68 | +print(f"el valor dentro de la funcion es: {valor_en_funcion}, el valor fuera de la funcion es :{ my_int_c}") |
| 69 | + |
| 70 | + |
| 71 | +## funcion que recibe variables por referencia: |
| 72 | + |
| 73 | +my_list_c = [10, 20] |
| 74 | + |
| 75 | +def my_list_func(my_list): |
| 76 | + my_list.append(30) |
| 77 | + |
| 78 | + return my_list |
| 79 | + |
| 80 | +valor_referencia_en_funcion = my_list_func(my_list_c) |
| 81 | +print(f"el valor de referencia dentro de la funcion es: {valor_referencia_en_funcion}, el valor fuera de la funcion es :{ my_list_c}") |
| 82 | + |
| 83 | +""" |
| 84 | +Ejercicio Dificultad Extra: |
| 85 | +""" |
| 86 | +print("--------------------------"*5) |
| 87 | +print("Ejercicio Extra:\n") |
| 88 | + |
| 89 | +# por valor: |
| 90 | + |
| 91 | +my_int_d = 10 |
| 92 | +my_int_e = 20 |
| 93 | + |
| 94 | +def value_changer(d, e): |
| 95 | + #intercambio por desempaquetado (swapping) |
| 96 | + d,e = e,d |
| 97 | + return d,e |
| 98 | + |
| 99 | +resultado = value_changer(my_int_d, my_int_e) |
| 100 | + |
| 101 | +print(f" el valor original es: \n my_int_d: {my_int_d}\n my_int_e: {my_int_e}") |
| 102 | +print(f" el valor cambiado es: \n my_int_d:{resultado[0]}\n my_int_e: {resultado[1]}") |
| 103 | + |
| 104 | +print("\n") |
| 105 | +print("--------------------------"*5) |
| 106 | + |
| 107 | +#por referencia: |
| 108 | + |
| 109 | +my_list_d = [10, 20] |
| 110 | +my_list_e = [20, 10] |
| 111 | + |
| 112 | +def value_changer(d, e): |
| 113 | + #intercambio por desempaquetado (swapping). |
| 114 | + # d,e = e,d |
| 115 | + |
| 116 | + #tambien puede realizarse con una variable "temporal" |
| 117 | + temp = d |
| 118 | + d = e |
| 119 | + e = temp |
| 120 | + return d,e |
| 121 | + |
| 122 | +resultado = value_changer(my_list_d, my_list_e) |
| 123 | + |
| 124 | +print(f" el valor original es: \n my_list_d: {my_list_d}\n my_list_e: {my_list_e}") |
| 125 | +print(f" el valor cambiado es: \n my_list_d:{resultado[0]}\n my_list_e: {resultado[1]}") |
0 commit comments