# Increment and Decrement operators in Python

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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.79765606,"math_prob":0.9682283,"size":7144,"snap":"2023-40-2023-50","text_gpt3_token_len":1677,"char_repetition_ratio":0.21358544,"word_repetition_ratio":0.36313292,"special_character_ratio":0.24552071,"punctuation_ratio":0.10901163,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9955838,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-02T02:43:32Z\",\"WARC-Record-ID\":\"<urn:uuid:1e15aabc-5424-453d-b651-e1944379ca63>\",\"Content-Length\":\"109967\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:445a6ff1-4960-4492-847d-4845f4ca4988>\",\"WARC-Concurrent-To\":\"<urn:uuid:c46fa8e4-0499-445f-9f1c-23f074810000>\",\"WARC-IP-Address\":\"67.202.92.24\",\"WARC-Target-URI\":\"https://pythonguides.com/increment-and-decrement-operators-in-python/\",\"WARC-Payload-Digest\":\"sha1:UYGSV52CCVOWM4F4ORIFNXNV7QD3C5CD\",\"WARC-Block-Digest\":\"sha1:I4U2UMYDMYP4X4F6MR7DTT4TZNWX3KEM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100309.57_warc_CC-MAIN-20231202010506-20231202040506-00380.warc.gz\"}"}