Incrementing

incrementing

Incrementing

Video Transcript

In this section, we're going to learn about incrementing. Incrementing is when you increase the value of a variable by some amount. We actually saw an example of this in action in the previous section:
count = 0
command = ""
while command != "quit":
  command = input("Enter a command: ")
  count = count + 1
  print(f"The value of count is {count}.")
Here, the variable count is initialized to 0, and on every iteration of the loop, count's value is increased by 1 using the line count = count + 1. If count's value is 0, then this line tells the computer to take the current value of count (which is 0), add 1 (to get 1), and save it back in the variable. So now, instead of having a value of 0, count has a value of 1. On the next iteration, the computer again takes the value of count (which is now 1), adds 1 (to get 2), and saves it back in the variable again. This way, the value of count gets higher and higher on every iteration of the loop.
We can do this not just with addition, but with any math expression we want, and instead of increasing by 1, it will update the variable according to the expression:
count = 0
command = ""
while command != "quit":
  command = input("Enter a command: ")
  count = count * -5 / 2 - 1
  print(f"The value of count is {count}.")
Last section Next section