 # Increment Operation in Python

What is the procedure for performing a Python increment operation? You might want to try extending a comparable increment functionality to Python if you’re coming from a language like C++ or Java.

However, as we will see later in this essay, this is not the case. Let’s have a look at how we may use Python’s Increment (++) operation to achieve comparable results.

## Increment Operation in Python

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

### 1)Examples of Increment operation in Python

Before we get into the specific changes, let’s have a look at how you increment a variable in Python.
The code below demonstrates how practically all Python programmers increment integers or related variables.

Implementation Examples:

1)Incrementing number by 1

# given number
number = 19
# increment it by 1
number += 1
print(number)


Output:

20

Explanation:

We started by taking a number and increasing its value by one, giving us 20 in this case.

2)Incrementing number by 200

# given number
number = 19
# increment it by 200
number += 200
print(number)


Output:

219

Explanation:

We started by taking a number and increasing its value by 200, giving us 219 in this case.

3)Incrementing string variable by 1

Let us see what will happen if we increment the string variable by 1

# given number
variable = "vicky"
# increment it by 1
variable += 1
print(variable)


Output:

Traceback (most recent call last):
File "/home/ff2eb412892e1f6025a70b5a2cffdb91.py", line 4, in <module>
variable += 1
TypeError: must be str, not int

Explanation:

We got that error because we incremented a string variable with an integer.

4)Incrementing string with another string(string concatenation)

In the above case we incremented the string with number let us see what’s the output when we increment the string with string.

# given number
variable = "vicky"
# increment it by another string
variable += "cirus"
print(variable)


Output:

vickycirus

Explanation:

Here it concatenated the two strings.

5)Incrementing float value by 1

Let us see what will happen if we increment the float value with  integer(1 in this case)

# given number
variable = 1.4
# increment it by another string
variable +=1
print(variable)


Output:

2.4

Explanation:

We started by taking a decimal number and increasing its value by 1, giving us 2.4 in this case.

6)Incrementing float value with float

Let us see what will happen if we increment the float value with float value

# given number
variable = 1.5
# increment it by another string
variable +=1.6
print(variable)


Output:

3.1

Explanation:

We started by taking a decimal number and increasing its value by 1.6, giving us 3.1 in this case.

### 2)Can we use ++ operator in Python?

Let us check that by implementing it.

Below is the implementation:

# given number
variable = 7
# increment it by another string
variable++
print(variable)


Output:

 File "/home/b1d4000b0d8d478263f6b7213398eaf8.py", line 4
variable++
^
SyntaxError: invalid syntax

There is, however, an issue. Python does not allow the usage of the ++ “operator” by default. The ++ operator, also known as the increment operator in C++ and Java, does not exist in Python.

### 3)Why does Python lack a ++ operator?

The decision to exclude the ++ operator from Python is a design choice. People in charge of adding new features to the Python language believed that a CPP-style increment operator was unnecessary.

When the Python interpreter parses the a++ symbol from our input, it does it in the following way:

• Because the binary + operator is the addition operator, a++ will be handled as a, +, and +. However, Python expects a number following the first + operator. As a result, it will generate a syntax error on a++ because the second + is not a number.

Similarly, the pre-increment ++a will be handled as follows:

• In Python, the unary + operator refers to the identity operator. This simply returns the integer that comes after it. This is why it is an integer identity operation.
• For example, the value of +7 is simply 7, whereas the value of +-7 is -7. This is a unary operator that only works with real numbers.
• The ++a is parsed as + and +a, while the second +a is handled as (+a), which is merely a sign.
• As a result, +(+(a)) evaluates to a.

So, even if we intended to increase the value of a by one, we can’t do so using the ++ symbols because this type of operator doesn’t exist.

To perform this type of increase, we must utilize the += operator.

like

number+=1
number-=1

### 4)How does += Works/Evaluated

You could think it’s an assignment statement because there’s a = symbol.

This is not, however, a typical assignment statement. An augmented assignment statement is what it’s called.

In a standard assignment statement, the right-hand side is evaluated first, then the left-hand side is assigned.

# 4 + 9 is evaluated to 13, before assigning to number
number = 4 + 9

In this enhanced assignment statement, however, the left side is evaluated first, followed by the right side. This is done so that the modified value can be written in-place to the left side.

# Reads the value of number, before adding 8 to it in-place
number += 8

Without using a reassigning statement like a = a + 1, this is the only way to increase a variable. However, in this case, the choice is irrelevant because the interpreter would optimize the code at runtime.