Wednesday, July 27, 2022

How to update variables in Python?

When developing programs sometimes it is useful to update the old value assigned to a variable with a new value. So when an assignment statement updates a variable the new value of the variable depends on the old variable value. In general, form increasing the value of the existing variable value can be defined as:
variable_name = variable_name + value
In the previous code assigning a new value to the existing varaiable_name value is done by addition of the old variable_name value (the value that is previously assigned to variable variable_name) and the new value. Decreasing the variable value can be done similarly. In general, decreasing the value of the variable with a specific value can be defined as:
variable_name = variable_name - value
In the previous code assigning a new value to the existing variable_name value is done with subtraction of the old variable_name value (the value that is previously assigned to the variable variable_name) and the value.
Example 1Create variable a1 and assign the value of 0 to the variable. Then print the variable and its value. Update the value of variable a1 by 5 and print the new value.
Solution:The variable a1 is defined and value of 0 is assigned to this variable.
a1 = 0
Now the variable a1 with is value as the output using print and format functions.
print("a1 = {}".format(a1))
After running the previous two lines of code the following output is obtained.
a1 = 0
The next step is to increase the variable a1 value by 5. In other words, we have to update the variable value i.e. assign the new value to a variable.
a1 = a1 + 5
Finally, we are going to show the new value of variable a1 as output using the print and format function.
print("a1 = {}".format(a1))
After running the previous two lines of code the following output is obtained.
a1 = 5
The entire input code for this example:
a1 = 0
print("a1 = {}".format(a1))
a1 = 5
print("a1 = {}".format(a1))
After running the entire code the following output is obtained.
a1 = 0
a1 = 5

Example 2 Create variable a2 and assign a value of 50 to this variable. Update variable value by reducing the initial variable value by 25. Print variable value before and after variable assessment/update.
Solution:First step is to create variable a2 and assign the value of 50 to the variable.
a2 = 50
The second step is to show the value of the variable a2 using print and format functions.
print("a2 = {}".format(a2))
After running the previous two lines of code the following output is obtained.
a2 = 50
The third step is to update the variable a2 value by subtracting the original value by 25.
a2 = a2 - 25
The fourth and final step is to show the variable a2 value as output again using print and format functions.
print("a2 = {}".format(a2))
After running the last two lines of code the following output is obtained.
a2 = 25
The entire code used in Example 2 is given below.
a1 = 50
print("a2 = {}".format(a2))
a2 = a2 - 25
print("a2 = {}".format(a2))
After running the entire four lines of code the following output is obtained.
a1 = 50
a2 = 25
It is important to know that if you update a variable that is not initialized the Python will return NameError: name 'variable_name' is not defined.
Example 3 Update value of variable N1 by 5 and variable N2 by -5.
Solution:The variable N1 is increased by the value of 5.
N1 = N1 + 5
Running the previous code Python will generate the following error.
NameError: name 'N1' is not defined
The variable N2 value will be decreased by the value of 5.
N2 = N2 - 5
Running the previous code line will generate the same error as before.
NameError: name 'N2' is not defined

From the previous example (Example 3) it can be concluded that the variable must be initialized before updating its value. In all previous cases, we have updated the initial variable value by large values. Generally, when doing iterations it is usually useful to update a variable by adding 1 and this is called increment. When subtracting the variable value by 1 is called decrement.
Example 4Initialize the variable a3 and a4 and assign to those variables values of 0 and 5. Then print out the initial variable values and increment variable a3 value and decrement variable a4 value. Finally, print out the results.
Solution:First step is to create variables a3 and a4 and assigned to them values 0 and 5, respectively.
a3 = 0
a4 = 5
Using the print and format functions we will show the initial values of variables a3 and a4.
print("a3 = {}".format(a3))
print("a4 = {}".format(a4))
The output of the previous four lines of code is given below.
a3 = 0
a4 = 5
The next step is to increment variable a3 and decrement variable a4 and print out the updated values.
a3 = a3 + 1
a4 = a4 - 1
print("a3 = {}".format(a3))
print("a4 = {}".format(a4))
After running the previous 4 lines of code the following output is generated.
a3 = 1
a4 = 4
As seen from the results the value of variable a3 is incremented or updated/increased from 0 to 1 and the value of the variable a4 is decremented from 5 to 4.

No comments:

Post a Comment