Python Increment By 1 | Python Decrement by 1

In this Python tutorial, I will teach you about Python increment by 1.

While creating a small game in Python, I implemented the concept of increment and decrement operators to update the player score.

In this tutorial, I have explained how you can increment the variable value by one, not only one but also increment or decrement it by a certain amount.

At the end of this tutorial, you will be able to use the operators ‘+=’ and ‘-=’.

What does Python Increment By 1 mean?

Python Increments by 1 means increasing the value by a specific amount. In other programming languages like C, C++, etc., the operator ‘++’ is used to increment the value by a certain amount, but in Python, there is no ‘++’ operator to increment the value by a certain amount.

Increment Operator in Python

However, in Python, there is a way to increment the value by a specific amount; that way is using the ‘+=’ operator.

The syntax is given below.

variable +=increment_value

Where,

  • variable +=increment_value: This means increasing the variable value by increment_value.

For example, suppose you are creating a program to count the number of attendees entering an event. Initially, the variable count will have a value of zero.

But as soon as attendees enter the event, the value of the count variable must increase by one for each attendee, and you can do that using the ‘+=’ operator, as shown below.

attendee_count = 0
attendee_count+=1

print('Attendee count', attendee_count)

In the above code, the Python increment variable attendee_count is initialized with 0; as one attendee enters the event, the value of attendee_count is incremented by 1 using the attendee_count+=1 which is equivalent to attendee_count = attendee_count + 1.

Increment Operator in Python

Thus, the value of attendee_count becomes 1, as you can see in the above output.

READ:  Matplotlib plot a line (Detailed Guide)

If you again call attendee_count+=1, the variable attendee_count becomes 2 as the value increases by one.

This is how you can use the Python increment operator ‘+=’ to increment the variable by 1.

Increment and Decrement Operators in Python

In the preceding section, you learned about increment operation ‘+=’, there is also a decrement operator which is ‘-=’ that decreases the value by specific amount.

The syntax is given below.

variable -=decrement_value

Where,

  • variable -=decrement_value: It means decreasing the variable value by decrement_value.

Again, let’s take the same attendees’ example, but this time, when attendees leave the event, the variable attendee_count value decreases by 1.

For example, if 50 attendees are present at the event, the value of attendee_count is 50, as shown below.

attendee_count = 50

Now, one attendee leaves the event, so the value of attendee_count must decrease, which can be done using the ‘-=’ operator, as shown below.

attendee_count -=1
print('Attendee count', attendee_count)

The Python decrement variable attendee_count is initialized with 50 in the above code. As one attendee leaves the event, the value of attendee_count is decremented by 1 using attendee_count-=1, equivalent to attendee_count = attendee_count—1.

Increment Operator in Python

Thus, the value of attendee_count becomes 49, as you can see in the above output.

This is how you can use the Python decrement operator ‘-=’ to decrement the variable by 1.

Difference between increment and decrement operators in Python

AspectIncrement Operator in PythonDecrement Operator in Python
Operators UsedWe use ‘+=’ or ‘+’ operators as increment operator in PythonWe use ‘-=’ or ‘-‘ operators as decrement operator in Python
EffectsIncreases the variable by a specified value.Decreases the variable by a specified value.
Syntaxx += 1‘ or ‘x = x + 1y -= 1‘ or ‘y = y – 1
UsageTypically used in loops, counters, or variable updates.Commonly used in loops, counters, or variable updates.
Can we use two strings in Python but with str and numbers?Throw ValueError cannot be used with two strings in Python or string with numbers.
List of differences between increment and decrement operators in Python

I hope you understand how to increment and decrement the variable value by 1 using the ‘+=’ and ‘-=’ operators.

READ:  How to convert a NumPy tuple to a list in Python [3 Methods]

Conclusion

In this Python tutorial, you learned about Python increment by 1 with syntax and examples.

You also learned about the decrement operator ‘-=’ to decrease the variable value by a certain amount. Additionally, you knew the difference between increment and decrement operators in Python.

You may like to read: