In this tutorial, you’ll learn how to emulate the Python increment and decrement operators. You’ll learn why no increment operator exists in Python like it does in languages like C++ or JavaScript. You’ll learn some Pythonic alternatives to emulate the increment and decrement operators. This will include learning how to increase a value by 1 in Python, or by any other number. You’ll also learn how increment strings.
Why is the increment operator important? It may seem pedantic to want to learn how to increase or decrease the value of a variable by the value of one. However, it actually has many very valuable applications. For example, you may want to run a while loop for a given number of times. This can be done easily while decreasing a variables numbers. Alternatively, you may want to develop a game where it’s important to keep track of the number of turns a player has played.
The Quick Answer: Use +=
or -=
Augmented Assignment
# Increment a Value in Python using +=
value = 1
value += 1
print(value)
# Returns: 2
# Decrement a Value in Python using -=
another_value = 10
another_value -= 1
print(another_value)
# Returns: 9
Table of Contents
How to Increment a Value by 1 in Python
If you are familiar with other programming languages, such as C++ or Javascript, you may find yourself looking for an increment operator. This operator typically is written as a++
, where the value of a
is increased by 1. In Python, however, this operator doesn’t exist.
Let’s try to implement the increment operator in Python and see what happens:
# Attempting to run the increment operator
a = 4
a++
# Returns: SyntaxError: invalid syntax
We can see that when we try to run the ++
operator that a SyntaxError
is returned. This is because Python interprets only the first +
operator and cannot interpret what to do with the second.
Is there a ++ operator in Python? No, there is no ++ operator in Python. This was a clear design decision by the developers of the Python language. Whatever the design reason actually was is unknown. What is known is that the operator doesn’t exist in Python. Because of this we need an alternative.
One alternative is simply to modify the original variable:
# Incrementing the a variable by 1
a = 4
a = a + 1
print(a)
# Returns: 5
What is actually happening here? Because integers are immutable, when we add 1 to the integer we actually create a new variable. Let’s confirm this by checking the IDs of the variables before and after modification:
# Finding IDs of the same variable
a = 4
print(id(a))
a = a + 1
print(id(a))
# Returns:
# 4338534848
# 4338534880
We can see that the ID of the variable has changed when we mutated.
Python Increment Shorthand: The Augmented Assignment Operator
The approach shown above works, but it’s also a bit tedious. We can implement an alternative to this approach. This alternative is the augmented assignment operator. The way that works is by reassigning the value to itself in a modified way.
Let’s see what this looks like:
# Using the Augmented Assignment Operator
a = 4
a += 1
print(a)
# Returns: 5
Similar to the approach above, this does create an entirely new variable (since integers are immutable). Let’s confirm this again by simply printing the IDs:
a = 4
print(id(a))
a += 1
print(id(a))
# Returns:
# 4339296704
# 4339296736
One of the benefits of this approach is that we can actually increment the value by whatever we like (rather than being limited to 1). For example, if we wanted to increment the value by two, we could simply write a += 2
.
In the next section, you’ll learn how to emulate the --
decrement operator in Python.
How to Decrement a Value by 1 in Python
Similar to incrementing a value in Python, decrementing a value works a little differently than you might expect coming from other languages. Because there is no decrement operator in Python, we need to use augmented assignment.
We can simply use the -=
augment assignment operator to decrement a value. Let’s see what this looks like:
# Decrementing a value by 1 in Python
a = 4
a -= 1
print(a)
# Returns: 3
While this approach is slightly longer than write a--
, it’s the shortest workaround that is allowable in Python. Because Python integers are immutable, we need a shorthand way to reassign a value to itself.
In the next section, you’ll learn how to use the augmented assignment operator to increment a value in a Python while loop.
How to Increment a Value in a Python While Loop
Python while loops will continue to execute a Python statement (or statements) while a condition is true. In many cases, you can easily set a loop to continue running until a value is equal to a certain value. For example, you can set a loop to run ten times by incrementing a value by 1:
# Incrementing a Value in a Python While Loop
a = 1
while a <= 10:
print(a)
a += 1
# Returns:
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 10
This is an extremely simplified example, but because of the versatility of Python while loops we can actually take this much, much further. For example, we could embed conditions into the loop or append to different data structures.
How to Increment a Python String
In this final section, you’ll explore how to increment a Python string. Because strings, like integers, are immutable in Python, we can use the augmented assignment operator to increment them.
This process works similar to string concatenation, where normally you’d add two strings together. Let’s see what this may look like:
string1 = 'datagy'
string1 = string1 + '.io'
print(string1)
# Returns: datagy.io
We can simplify this using the augmented assignment operator:
string1 = 'datagy'
string1 += '.io'
print(string1)
# Returns: datagy.io
There are a few important notes about this:
- The
-=
augmented assignment operator will raise aTypeError
- Both types must be the same. If appending an integer with
+=
, you must first convert the integer to a string using thestr()
function
Conclusion
In this tutorial, you learned how to emulate the increment and decrement operators, ++
and --
, in Python. You learned why these operators don’t work and how to increment with Python using the augmented assignment operators, +=
and -=
. You then learned how to increment and decrement in a Python while loop and how to increment strings in Python.
To learn more about the augmented assignment operators, check out the official documentation here.
Additional Resources
To learn more about related topics, check out the following articles:
Pingback: Python Break, Continue and Pass: Python Flow Control • datagy
Pingback: Python Defaultdict: Overview and Examples • datagy
Pingback: How to Decrement a For Loop in Python • datagy
Pingback: Working with External Libraries in Python • datagy