Increment and Decrement Operators in Python

58

In other object-oriented programming languages such as C++ and Java, increment (++) and decrement (–) operators add or subtract a specific number from a variable to perform a specific task in a function. Interestingly there are no increment and decrement operators in Python.

To perform an increment or decrement operation in Python, we can combine assignment and arithmetic operations, i.e., +=. For example, if you want to increment the value in x by 2, then you can write this statement in Python: x += 2 which is equivalent to this statement: x = x + 2

Also ReadPython Dictionary Length [How To Get Length]

Let’s now understand increment and decrement operators in Python with some examples.

Increment operator in Python

The increment operator is used to increment a variable.

Syntax:

variable=variable+value

(or)

variable+=value

Where the variable is the name that holds a value and value is to add to the variable.

Example 1:

In this example, we will create a variable with a value of 34 and perform increment operations.

#create a variable that holds a value
my_variable=34

#display actual value
print(my_variable)

#increment 34 by 7
my_variable+=7
print("After adding 7 - ",my_variable)

#increment  by 10
my_variable+=10
print("After adding 10 - ",my_variable)

#increment  by 100
my_variable+=100
print("After adding 100 - ",my_variable)

Output:

34
After adding 7 -  41
After adding 10 -  51
After adding 100 -  151

We can also add strings using the increment operator.

Also ReadHow To Concatenate Arrays in Python [With Examples]

Example 2: In this example, we create a string variable and add another string using the increment operator.

#create a variable that holds a string
my_variable="Hello"

#display actual string
print(my_variable)

#add the string - Codeitbro to the actual string
my_variable+=" Codeitbro"
print(my_variable)

Output:

Hello
Hello Codeitbro

We can also increment the values present in the list.

Also ReadWhat Does The Python Percent Sign Mean?

Example: In this example, we will create a list of 5 integer values and add 6 to each element.

#create a variable that holds a list of integer elements
my_variable=[34,3,234,23,32]

#display actual list
print(my_variable)

#add every element in the list with 6
#using for loop
for i in range(len(my_variable)):
  print(my_variable[i]+6)

Output:

[34, 3, 234, 23, 32]
40
9
240
29
38

We can also increment the values present in the tuple.

Also ReadHow to Create an Empty Array In Python

Example: In this example, we will create a tuple of 5 integer values and add 6 to each element.

#create a variable that holds a tuple of integer elements
my_variable=(34,3,234,23,32)

#display actual tuple
print(my_variable)

#add every element in the tuple with 6
#using for loop
for i in range(len(my_variable)):
  print(my_variable[i]+6)

Output:

(34, 3, 234, 23, 32)
40
9
240
29
38

Also ReadWhat Is Python Used For? Applications of Python

Using Increment Operator in while Loop

We will use the increment operator in the while loop to iterate all the elements.

Example: In this example, we will create a list with 5 elements and display all of them using a while loop by incrementing one by one value.

#create a variable that holds a tuple of integer elements
my_variable=(34,3,234,23,32)

#display actual tuple
print(my_variable)

i=0

#iterate every element using while loop
#using for loop
while(i < len(my_variable)):
  print(my_variable[i])
  i+=1

Output:

(34, 3, 234, 23, 32)
34
3
234
23
32

We can specify increment value as step-in for the loop.

Also ReadHow To Display A Calendar In Python

So by this, it will skip the n number of increments,

Syntax:

for iterator in range(start,stop,step):
  print(iterator)

Where,

  1. An iterator is used to iterate the elements.
  2. Start is the starting number.
  3. Stop is the ending number
  4. And, step is used to increment the iterator.

Example: In this example, we will iterate numbers from 0 to 10 by incrementing 3.

for i in range(0,10,3):
  #display
   print(i)

Output:

0
3
6
9

Also ReadHow To Sort a List of Tuples in Python

Decrement operator in Python (–)

The decrement operator is used to decrement a variable.

Syntax:

variable=variable-value

(or)

variable-=value

Where the variable is the name that holds a value, and the value is to be subtracted from the variable.

Also ReadArithmetic Operators in Python [With Examples]

Example: In this example, we will create a variable with a value of 34 and perform decrement operations.

#create a variable that holds a value 
my_variable=34 

#display actual value 
print(my_variable) 

#decrement 34 by 7 
my_variable-=7 
print("After subtracting  7 - ",my_variable)  

#decrement by 10
my_variable-=10 
print("After subtracting 10 - ",my_variable) 

#decrement by 100 
my_variable-=100 
print("After subtracting 100 - ",my_variable)

Output:

34
After subtracting  7 -  27
After subtracting 10 -  17
After subtracting 100 -  -83

We can also decrement the values present in the list.

Also ReadHow to Create Python Empty Set

Example: In this example, we will create a list of 5 integer values and subtract 6 to each element.

#create a variable that holds a list of integer elements
my_variable=[34,3,234,23,32]

#display actual list
print(my_variable)

#subtract every element in the list with 6
#using for loop
for i in range(len(my_variable)):
  print(my_variable[i]-6)

Output:

[34, 3, 234, 23, 32]
28
-3
228
17
26

We can also decrement the values present in the tuple.

Also ReadPython Matrix Using Numpy [With Examples]

Example: In this example, we will create a tuple of 5 integer values and subtract 6 to each element.

#create a variable that holds a tuple of integer elements
my_variable=(34,3,234,23,32)

#display actual tuple
print(my_variable)

#subtract  every element in the tuple with 6
#using for loop
for i in range(len(my_variable)):
  print(my_variable[i]-6)

Output:

(34, 3, 234, 23, 32)
28
-3
228
17
26

Also ReadHow to Exit Python Program [4 Methods]

Using Decrement Operator in while Loop

We will use the decrement operator in the while loop to iterate all the elements.

Example:

In this example, we will create a list with 5 elements and display all of them using a while loop by incrementing one by one value.

#create a variable that holds a tuple of integer elements
my_variable=(34,3,234,23,32)

#display actual tuple
print(my_variable)

i=0

#iterate every element using while loop
#using for loop
while(i >= len(my_variable)):
  print(my_variable[i])
  i-=1

Output:

(34, 3, 234, 23, 32)

We can specify increment value as step-in for the loop.

Also ReadHow to Convert Python Tuples to Lists

So by this, it will skip the n number of decrements,

Syntax:

for iterator in range(start,stop,step):
  print(iterator)

Where,

  1. An iterator is used to iterate the elements.
  2. Start is the starting number.
  3. Stop is the ending number
  4. And, Step is used to decrement the iterator.

Example:

In this example, we will iterate numbers from 10 to 0 by incrementing 3.

for i in range(10,0,-3):
  #display
   print(i)

Output:

10
7
4
1

Also ReadHow to Handle String Index Out of Range Error In Python

Summary

In this tutorial, we discussed increment and decrement operators in Python. We also discussed that they are not only used to add/subtract values but are also helpful in many applications like iterations. Also, they are applied to data structures like list, tuple, set, dictionary, etc.

Also Read: