# Increment and Decrement operators in Python

By: On:

In this python tutorial, you will learn about increment and decrement operators in python with examples. Here we will check:

• Increment and Decrement operators in Python
• Python increment operator
• Python decrement operator
• Increment variable in loop python
• How to increment a character in python
• Decrement operators in python for loop
• Increment operators in python for loop

## Increment and Decrement operators in Python

Let us understand Increment and Decrement operators in Python.

Python does not allow using the “(++ and –)” operators. To increment or decrement a variable in python we can simply reassign it. So, the “++” and “–” symbols do not exist in Python.

## Python increment operator

Now, let us understand about Python increment operator using an example.

In python, if you want to increment a variable we can use “+=” or we can simply reassign it “x=x+1” to increment a variable value by 1.

Example:

``````x = 20
x = x+1
print(x)``````

After writing the above code (python increment operators), Ones you will print “x” then the output will appear as a “ 21 ”. Here, the value of “x” is incremented by “1”.

You can refer to the below screenshot for increment operator in python.

In the below code, we will see another example to increment the variable using “+=”, which does the same.

Example:

``````x = 21
x+=1
print(x)``````

After writing the above code, Ones you will print “x” then the output will appear as a “ 22 ”. Here, the value of “x” is incremented by “1”.

You can refer to the below screenshot.

## Python decrement operator

Let us understand now, Python decrement operator with an example.

To decrement a variable in python we can use “-=” or “x=x-1” operators in python to reduce the value of a variable by 1.

Example:

``````x = 21
x = x-1
print(x)``````

After writing the above code (python decrement operators), Ones you will print “x” then the output will appear as a “ 20 ”. Here, the value of “x” is decremented by “1”.

You can refer to the below screenshot for decrement operator in python.

In the below code, we will see another example to decrement the variable using “-=”, which does the same.

Example:

``````x = 20
x-=1
print(x)``````

After writing the above code, Ones you will print “x” then the output will appear as a “ 19 ”. Here, the value of “x” is reduced by “1”.

You can refer to the below screenshot.

## Increment variable in loop python

Let us see how to increment variable in loop in Python.

In python, to increment a variable value in a loop, we can use the while loop directly for increasing or decreasing the iteration value.

Example:

``````my_list = [11, 12, 13, 14, 15]
i = 0
while(i < len(my_list)):
print(my_list[i])
i += 2``````

After writing the above code (increment variable in loop python), Ones you will print “my_list[i]” then the output will appear as an “ 11 13 15 ”. Here, the value is incremented by 2 in every iteration.

You can refer to the below screenshot for increment variable in loop python.

In the below code, we will see another example of the increment variable in loop python by using the range function, as the third parameter of this function will specify the step to increment its index value.

Example:

``````my_list = [11, 12, 13, 14, 15]
for i in range(0, len(my_list), 2)
print(my_list[i])``````
• After writing the above code (increment variable in loop python), Ones you will print “my_list[i]” then the output will appear as an “ 11 13 15 ”.
• Here, the range function is used to return the sequence of numbers, were starting from “0”, and specifying the position to stop, also specifying the incrementation by 2.

You can refer to the below screenshot for increment variable in loop python.

## How to increment a character in python

How to increment a character in python? It will give an error if you try to increment a character with integer value in Python.

Example:

``````a = "N"
a = a + 2
print(a)``````

After writing the above code (how to increment a character in python), Ones you will print “a” then the error will appear as a “ TypeError: can only concatenate str (not “int”) to str ”.

You can refer to the below screenshot.

To solve the above typeerror, we will be using ord() and chr() in order to avoid an error. The ord() returns the corresponding ASCII value of character and after adding the integer to it, chr() again converts it into character.

Example:

``````char = ord("N")
val = char + 2
increment = chr(val)
print(increment)``````

After writing the above code (how to increment a character in python), Ones you will print “increment” then the output will appear as a “ P “. Here, the “chr” returns the incremented character.

You can refer to the below screenshot how to increment a character in python.

## Decrement operators in python for loop

To decrement the index value inside the for loop in Python, we can use the range function as the third parameter of this function will be negative. By making the step value negative it is possible to decrement the loop counter.

Example:

``````for i in range(6, 0, -2):
print(i)``````
• After writing the above code (decrement operators in python for loop), Ones you will print “i” then the output will appear as a “ 6 4 2 ”.
• Here, the range function is used to return the sequence of numbers, here starting from “6”, and it goes on decrementing till 0 positions, also specifying the step to decrement by -2.

You can refer to the below screenshot for decrement operators in python for loop.

## Increment operators in python for loop

To increment the operator in loop python we will use the range function, as the third parameter of this function will specify the step to increment its index value.

Example:

``````for i in range(0, 6, 1)
print(i)``````
• After writing the above code (increment operators in python for loop), Ones you will print “i” then the output will appear as an “ 0 1 2 3 4 5 ”.
• Here, the range function is used to return the sequence of numbers, were starting from “0”, and the specified range to stop is 6, also the step to increment is 1.

You can refer to the below screenshot for increment operators in python for loop.