Passing Variables by Value in Python
In Python, all data is divided into primitive (strings, numbers) and complex objects (lists, sets, dictionaries, and so on). At the same time, the transfer of variables of each type has its own characteristics. For example, by value, you can transfer variables that belong only to primitive data types.
Let's say we have two variables. The first one contains one in its value, and the second variable is equal to the first:
num1 = 1
num2 = num1
print(num2) # 1
If you overwrite num1 after declaring the second variable, the value of num2 will remain the same. This is because only the value of the first variable is copied to num2. And any subsequent changes made to num1 will not affect num2. This is where variable passing by value works:
num1 = 1
num2 = num1
num1 = 3
print(num2) # 1
What will be the result of running the following code:
num1 = 10
num2 = num1
num1 = 5
print(num2)
What will be the result of running the following code:
num1 = 8
num2 = num1 - 2
print(num2)
What will be the result of running the following code:
txt1 = 'abcde'
txt2 = 'abcde'
txt1 = txt1.upper()
print(txt2)
What will be the result of running the following code:
txt1 = 'abcde'
txt2 = txt1
txt1 = txt1.title()
print(txt1)
print(txt2)