Assigning a new value to an already existing variable
How do you assign a new value to an already created variable? In the same way. You just need to use the equal sign.
The equal sign is in fact an assignment operator. Although this may sound strange, the operator has a simple syntax and unambiguous interpretation.
It assigns the value of its right argument to the left, while the right argument may be an arbitrarily complex expression involving literals, operators and already defined variables.
Look at the code below:
var = 1
print(var)
var = var + 1
print(var)
The code sends two lines to the console:
1
2
output
No comments:
Post a Comment