2.13. Updating Variables — Foundations of Python Programming (2024)

One of the most common forms of reassignment is an update where the newvalue of the variable depends on the old. For example,

x = x + 1

This means get the current value of x, add one, and then update x with the newvalue. The new value of x is the old value of x plus 1. Although this assignment statement maylook a bit strange, remember that executing assignment is a two-step process. First, evaluate theright-hand side expression. Second, let the variable name on the left-hand side refer to this newresulting object. The fact that x appears on both sides does not matter. The semantics of the assignmentstatement makes sure that there is no confusion as to the result. The visualizer makes this very clear.

x = 6
x = x + 1

If you try to update a variable that doesn’t exist, you get an error becausePython evaluates the expression on the right side of the assignment operatorbefore it assigns the resulting value to the name on the left.Before you can update a variable, you have to initialize it, usually with asimple assignment. In the above example, x was initialized to 6.

Updating a variable by adding something to it is called an increment; subtracting iscalled a decrement. Sometimes programmers talk about incrementing or decrementing without specifying by how much; when they do they usually mean by 1. Sometimes programmers also talk about bumping a variable, which means the same as incrementing it by 1.

Incrementing and decrementing are such common operations that programming languages often include special syntax for it. In Python += is used for incrementing, and -= for decrementing. In some other languages, there is even a special syntax ++ and -- for incrementing or decrementing by 1. Python does not have such a special syntax. To increment x by 1 you have to write x += 1 or x = x + 1.

Imagine that we wanted to not increment by one each time but instead add together thenumbers one through ten, but only one at a time.

After the initial statement, where we assign s to 1, we can add the current value ofs and the next number that we want to add (2 all the way up to 10) and then finallyreassign that that value to s so that the variable is updated after each line in thecode.

This will be tedious when we have many things to add together. Later you’ll read about aneasier way to do this kind of task.

Check your understanding

    What is printed when the following statements execute?

    x = 12x = x - 1print(x)
  • 12
  • The value of x changes in the second statement.
  • -1
  • In the second statement, substitute the current value of x before subtracting 1.
  • 11
  • Yes, this statement sets the value of x equal to the current value minus 1.
  • Nothing. An error occurs because x can never be equal to x - 1.
  • Remember that variables in Python are different from variables in math in that they (temporarily) hold values, but can be reassigned.

    What is printed when the following statements execute?

    x = 12x = x - 3x = x + 5x = x + 1print(x)
  • 12
  • The value of x changes in the second statement.
  • 9
  • Each statement changes the value of x, so 9 is not the final result.
  • 15
  • Yes, starting with 12, subtract 3, than add 5, and finally add 1.
  • Nothing. An error occurs because x cannot be used that many times in assignment statements.
  • Remember that variables in Python are different from variables in math in that they (temporarily) hold values, but can be reassigned.

Construct the code that will result in the value 134 being printed.

 mybankbalance = 100mybankbalance = mybankbalance + 34print(mybankbalance) 

    Which of the following statements are equivalent?

  • x = x + y
  • x is updated to be the old value of x plus the value of y.
  • y += x
  • y is updated to be the old value of y plus the value of x.
  • x += x + y
  • This updates x to be its old value (because of the +=) plus its old value again (because of the x on the right side) plus the value of y, so it's equivalent to x = x + x + y
  • x += y
  • x is updated to be the old value of x plus the value of y.
  • x++ y
  • ++ is not a syntax that means anything in Python.

You have attempted of activities on this page

2.13. Updating Variables — Foundations of Python Programming (2024)

References

Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 5812

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.