Python

Python Increment by 1

When you’re acquainted with Python, you’re probably aware that the Increment and Decrement expressions (both before and after) aren’t supported. Python was created to be understandable and consistent. In linguistics having ++ and — expressions, a beginner programmer frequently makes the mistake of confusing the distinctions among increment/decrement expressions, post and pre (both in priority and return value). In comparison to many other programming languages, basic increment and decrement expressions are not quite as necessary. In this tutorial, we will learn about the increment by 1 operator in Python code. Make sure you must have a python tool installed and configured on your system. Hence, we have installed the Spyder Python tool on our system.

Example 01:

Our first example would be looking at how to add increment by 1 in any python code. Open the Spyder tool first, and name the code file as test.py. In the code area, write out the below python code to increment 1 in an integer type variable. We have added the python support in our spyder page first. You can see we have defined an integer x having a value of 0. After that, we have incremented this variable x with 1 using the “+=” operator within. After that, we have printed the variable x to see whether the increment by 1 work properly or not. Save the code and click on the “Run” button to execute the python code.

# -*- coding: utf-8 -*-
#!/user/bin/python
x = 0
x +=1
print(x)

The output windows show us that the value of variable x has been incremented by 1 as it was initially 0. This means that the syntax used above to increment any variable by 1 is workable and reliable.

Example 02:

Let’s take a look at different ways of incrementing a variable by 1. In this case, we have again added python support in the Spyder window. After that, we have declared a variable x with a value of 2. On the 4th line, we have used the increment “+” sign to add 1 in the previous value of x, and the result has been again saved into the variable x. This means the last value has been overriding here. After that, the print statement will print the new overridden value. Save your code to see the results. Tap on the “Run” button to interpret the code.

# -*- coding: utf-8 -*-
#!/user/bin/python
x = 2
x = x + 1
print(x)

The output shows that the value 2 of the x variable has been incremented by 1 and becomes 3. Then this value was again saved into the variable x and printed out.

Example 03:

As we have mentioned above that the increment and decrement operators cannot be used in the python programming language as they are of no use here. Let’s check if it’s true or not to clear the understanding. Hence, we have updated the code and initialized a variable “n” having a value of 2. Then we have used the pre-increment operator to increment its value and saved that value into variable “n” again. After the print statement, we have saved the code and executed it via the “Run” sign.

# -*- coding: utf-8 -*-
#!/user/bin/python
n = 2
n = ++n
print(n)

When we have executed the code, we know that the original value has not been incremented, and the output shows the same original value in its result. This means that the pre-increment operator is not working here and is of no use while used in the programming.

Let’s check the post-increment operator now. We have used the same code here again while replacing the pre-increment operator with a post-increment operator, as shown in the code below.

# -*- coding: utf-8 -*-
#!/user/bin/python
n = 2
n = n++
print(n)

The output of the above code returns a Syntax Error saying that the syntax is invalid. This proves that the post and pre-increment or decrement operators are of no use in python.

Example 04:

Let’s have a look at a simple example to increment a variable with 1. We have used a variable having a value of 0 at first. The original value has been printed out, and then the value has been incremented by 1 using the “+=” sign. Then the new value should be 1 now. The new value will be printed out. Then we have again used the “+=” operator to increment the value by 30 this time and printed it out. Save the code and execute it via the “Run” button.

# -*- coding: utf-8 -*-
#!/user/bin/python
x = 0
print(x)
x +=1
print(x)
x +=30
print(x)

The output below is showing the expected results. It first displays the original value 0, and after the increment of 1, it prints 1. In the end, value 1 has been increment by 30, and it becomes 31.

Example 05:

Let’s use the increment by 1 operator on any string type value and see its results. First of all, we took an integer variable “x” as we did in the above example. The variable x has an original value of 0. Its value has been incremented by 1 and after that by 31.  This is the same case as we discussed above. Here comes another variable, “y” having the value “Aqsa” in it. Then we have used the “+=” sign to increment the value of variable “y” with 1. Logically it is wrong because the integer value cannot be increment in the string value. So, we should get an error upon executing this code. So, save your code and execute it.

# -*- coding: utf-8 -*-
#!/user/bin/python
x = 0
print(x)
x +=1
print(x)
x +=30
print(x)
y = 'Aqsa'
y +-1
print(y)

When we printed out the code, the increment performed on the integer type variable “x” has been successful and displayed the incremented value every time. But in the case of variable “y”, it throws an exception of “TypeError” saying that the string type data can only be concatenated with string instead of integer type data.

Let’s modify the code and increment the integer “y” value by a string type value, as shown in the code below. Save your code and run the file to see them working.

# -*- coding: utf-8 -*-
#!/user/bin/python
x = 0
print(x)
x +=1
print(x)
x +-30
print(x)
y = 'Aqsa'
y += 'Yasin'
print(y)

This time displayed all the incremented values, including the string type increment value in the output. This is because the + sign can be taken as concatenation for strings and cannot increment the integer value into some sort of string value.

Example 06:

Understand that we cannot use pre and post-increment or decrement operators in “for” loops as well. Hence, we have been using the “+=” operator in a while loop to print the values of a list.

# -*- coding: utf-8 -*-
#!/user/bin/python
list = [ 12, 14, 16, 18, 20]
x = 0
while(x <len(list)):
    print(list[x])
    x += 1

Upon execution of the code, we have got the values of the list one after another in a sequence.

Example 07:

Let’s see the effect of increment by 1 into a variable “ID” this time. We have initialized a variable “x” with value 2 and checked its “ID” first. After that, we have to increment it by 1 and checked its “ID” once again. Save and Run the code.

# -*- coding: utf-8 -*-
#!/user/bin/python
x = 2 print(x)
print(id(x))
x +=1
print(x)
print(id(x))

While the execution of code, the output shows two different “IDs” before and after the increment by 1. This means every time we increment or change a variable, its dynamics also change.

# -*- coding: utf-8 -*-
#!/user/bin/python
x = y = z=2
print(id(x))
print(id(y))
print(id(z))
x +=1
print(id(x))
print(id(y))
print(id(z))

Conclusion:

This tutorial discussed and seen how the post and pre-increment or decrement operators fail in python. We have also seen how to use different ways to increment any variable by 1. Hope this article will be helpful to you while using Python.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.