2.11. Updating Variables — How to Think like a Computer Scientist: Interactive Edition (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 1 is called an increment; subtracting 1 iscalled a decrement. Sometimes programmers also talk about bumpinga variable, which means the same as incrementing it by 1.

Advanced Topics

  • Topic 1: Python Beyond the Browser. This is a gentleintroduction to using Python from the command line. We’ll cover thislater, but if you are curious about what Python looks like outside of thiseBook, you can have a look here. There are also instructions forinstalling Python on your computer here.

  • Topic 2: Dive Into Python 3,this is an online textbook by Mark Pilgrim. If you have already had someprogramming experience, this book takes you off the deep end withboth feet.

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) 

Note

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.

You have attempted of activities on this page

2.11. Updating Variables — How to Think like a Computer Scientist: Interactive Edition (2024)
Top Articles
Career Outlook | Traditional Master's in Supply Chain Management
Want a Job in Supply Chain? Here’s How to Get a Foot in the Door
Edina Omni Portal
Camera instructions (NEW)
Cintas Pay Bill
Research Tome Neltharus
Phcs Medishare Provider Portal
Amtrust Bank Cd Rates
Southside Grill Schuylkill Haven Pa
Overnight Cleaner Jobs
Nwi Police Blotter
DENVER Überwachungskamera IOC-221, IP, WLAN, außen | 580950
His Lost Lycan Luna Chapter 5
Www Craigslist Louisville
Umn Pay Calendar
Legacy First National Bank
Midway Antique Mall Consignor Access
Fire Rescue 1 Login
Indiana Immediate Care.webpay.md
Readyset Ochsner.org
Nj Scratch Off Remaining Prizes
General Info for Parents
Viha Email Login
Amc Flight Schedule
Pinellas Fire Active Calls
Www.publicsurplus.com Motor Pool
Marion City Wide Garage Sale 2023
Cpt 90677 Reimbursem*nt 2023
Publix Near 12401 International Drive
2021 Tesla Model 3 Standard Range Pl electric for sale - Portland, OR - craigslist
Uky Linkblue Login
James Ingram | Biography, Songs, Hits, & Cause of Death
Smayperu
Wake County Court Records | NorthCarolinaCourtRecords.us
Newcardapply Com 21961
Bee And Willow Bar Cart
Ma Scratch Tickets Codes
Mp4Mania.net1
Junee Warehouse | Imamother
Frank 26 Forum
T&Cs | Hollywood Bowl
Keir Starmer looks to Italy on how to stop migrant boats
Noaa Duluth Mn
Gopher Hockey Forum
Www Craigslist Com Atlanta Ga
How the Color Pink Influences Mood and Emotions: A Psychological Perspective
The Plug Las Vegas Dispensary
Greg Steube Height
Game Like Tales Of Androgyny
Sml Wikia
Taterz Salad
Arre St Wv Srj
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 6086

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.