Python Increment By 1

How do you increase a variable by one in Python? In other popular programming languages to increment a variable by one you can use the simple increment syntax of ++ , for example i++ will make the value of i increase by 1 , but how do you do this in Python?

To increment a variable in Python use the syntax += 1 , for example to increment the variable i by 1 write i += 1 . This is the shorter version of the longer form syntax i = i + 1 .

Here is an example of increasing a variable properly using Python with the += 1 syntax as shown here:

>>> i = 10
>>> i += 1
>>> print(i)
11

Using ++ In Python

If you try to write i++ syntax in your Python code you will get a SyntaxError as shown below:

>>> def increment_me(i):
...     print(i++)
...
File "<input>", line 2
   print(i++)
            ^
SyntaxError: invalid syntax

Therefore, you cannot use this operation ( ++ ) which is found in other popular languages (such as Javascript) to increment an integer variable in Python.

So how do you increment a variable by 1 in Python ?

Increase i By More Than 1

If you want to increase the value of the step in Python you can run the syntax multiple times, or just change the constant to reflect the size of the increase for the variable.

For example, if you want to increase the size of the variable i by 2 you can write i += 2 , which will have the following effect:

>>> i = 21
>>> i += 2
>>> print(i)
23

Increase By Variable

Besides increasing a variable by a constant number you could also swap out the increasing amount by using a variable.

Here’s a simple demonstration showing what would happen if instead of having the constant 1 as the incrementing amount you have the incrementing amount refer to another variable:

>>> a = 3
>>> i = 50
>>> i += a
>>> print(i)
53
>>> a = 10
>>> print(i)
53
>>> i += a
>>> print(i)
63

You’re also not just limited to constants and variables, you can also use statements, such as the handy one-line if statement , as shown here:

>>> i = 3
>>> i += 1 if True else 2
>>> print(i)
4

As the condition of the if statement above is True the first value of 1 is used when this statement is resolved. Therefore, the outcome of the if statement is 3 + 1 being 4.

Decrement A Variable By 1

Similar to how you increment a variable by one using the syntax += 1 to decrement a variable by one, switch the + symbol with the - symbol so that the syntax now looks like so: -= 1 for example i -= 1 .

Using the same principle of modifying the constant, you could also decrement the variable by applying a negative number instead of a positive number , as seen in this example:

>>> i = 34
>>> i += -1
>>> print(i)
33

The equivalent of the above operation is to use the same syntax but to use -= instead.

For example, to reduce a variable by an amount, such as 1 , you would use the following operation:

>>> i = 34
>>> i -= 1
>>> print(i)
33

Concatenating Lists & Strings

Besides increasing a numeric variable the same shorthand syntax can also be applied to appending additional items to lists, or for concatenating strings.

Here are some additional use cases where the operation is performed on other data types:

>>> i = [1]
>>> i += [2]
>>> print(i)
[1, 2]

It’s important to note that to append lists together into one both must be of list data type . The following produces a TypeError :

>>> i = [1]
>>> i += 2
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

It also doesn’t work if the variable is declared first as an int and a list data type is appended next, as shown:

>>> i = 1
>>> i += [2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +=: 'int' and 'list'

The same also applies to strings, the only way the operation works is if both types are of type str , as shown here:

>>> i = 'hello'
>>> i += ' world'
>>> print(i)
'hello world'

Prepend Lists With += Syntax & List Comprehensions

You can also prepend lists using the += syntax on variables that are of a data type combined together with list comprehensions , as shown in this example here:

>>> i = [1, 2, 9]
>>> i += [x for x in range(100, 500, 100)]
>>> print(i)
[1, 2, 9, 100, 200, 300, 400]

SyntaxError When Declaring Data Type

Be mindful you cannot declare the type when using these types of operations in Python 3, otherwise a SyntaxError is thrown, as shown in this example:

>>> i: int = 0
>>> i: int += 1
  File "<stdin>", line 1
     i: int += 1
            ^
SyntaxError: invalid syntax

To remove the SyntaxError just remove the type declaration after the variable as shown in this example:

>>> i: int = 0
>>> i += 1
>>> print(i)
1

Summary

Does i++ work in Python? No, it doesn’t. It will return a SyntaxError . Therefore, to achieve the same purpose of incrementing a variable, i , by 1 using the syntax i += 1 or its longer form syntactical representation i = i + 1 .

The syntax += is quite useful in not only being able to increase a variable by an amount but also has further utility in being able to concatenate strings and lists provided the original data type of the variable being operated on is a string or list respectively.

You might want to see an application for incrementing a variable by 1 and how it is used when creating an incrementing file name in Python .

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.