Scripting

How to Increment and Decrement in Python

How_to_Increment_and_Decrement_in_Python

Increment means adding one to a number or to a value whereas decrement means subtracting one to a number or a value.

How to Increment in Python

In this article, we are going to discuss these two operators in detail so let’s start first with the increment operator you can do that by three different methods which are explained below.

  1. By using the addition operator
  2. By using shorthand operator
  3. By user-defined function

Increment using the Addition Operator

One of the simplest methods is to use an addition operator for incrementing a number.

a=10

a=a+1

print("a=",a)

A screenshot of a computer Description automatically generated with low confidence

Figure 1: Increment using the Addition Operator

Output:

Figure 2: Output

In the above code, we have a variable “a” having value of 10. To increment “a” we add 1 to it using the + operator and get 11 as shown in figure 2.

Increment using Shorthand Operator

In python, we have shorthand operators which make the work easier. This “+=” operator can be used for incrementing a number as it adds two numbers and assigns the result to the left operand.

weight=50

weight+=1

print("increment weight=",weight)

Text Description automatically generated

Figure 3: Increment using the Shorthand Operator

Output:

Figure 4: Output

In the above example, variable weight has a value of 50. On the right side of the += shorthand operator, we have 1 which tells us that we are adding one to weight. While on the left side we have a weight variable which means that we are assigning value back to the weight variable after the increment. The new value for weight is displayed in figure 4.

Increment by User Defined Function

As python does not support unary increment and decrement operators so we can define increment as a user-defined function using the def keyword and defining it as a function will also increase the reusability.

def increment(n):

return n+1

print("increment of n is:",increment(11))

Graphical user interface, text Description automatically generated

Figure 5: Increment by User Defined Function

Output:

Figure 6: Output

In the above figure 5, we have declared a function increment using the def keyword. It takes one parameter n which is a number. This function returns a number by adding one to it. So once we define this function it can be called where needed.

How to Decrement in Python

Decrement means subtracting one from a number or from a value. There are various methods through which we can decrement a number.

  1. By using the subtraction operator
  2. By using shorthand operator
  3. By user-defined function

Decrement using the Subtraction Operator

The simplest method is to use a subtraction operator for decrementing a number.

a=10

a=a-1

print("a=",a)

A screenshot of a computer screen Description automatically generated with medium confidence

Figure 1: Decrement using the Subtraction Operator

Output:

Figure 2: Output

In the above code, we have a variable “a” having value of 10. To decrement “a” we subtract 1 from it and we get 9 as shown in figure 2.

Decrement using the Shorthand operator

In python, we have shorthand operators which make the work easier. This “-=” operator can be used for decrementing a number as it subtracts two numbers and assigns the result to the left operand.

dozen=12

dozen-=1

print("decrement dozen=",dozen)

Graphical user interface, text Description automatically generated

Figure 3: Decrement using the Shorthand Operator

Output:

Figure 4: Output

In the above example variable dozen has a value of 12. On the right side of the -= shorthand operator, we have 1 which tells us that we are subtracting one from a dozen variables. While on the left side we have a dozen variables which means we are assigning value back to a dozen variables after decrement. The new value for dozen is displayed in figure 4.

Decrement by User Defined Function

As python does not support unary increment and decrement operators so we can define decrement as a user-defined function using the def keyword. Thus, defining it as a function will increase the reusability.

def decrement(n):

return n-1

print("decrement of n is:",decrement(20))

Graphical user interface, text Description automatically generated

Figure 5: Decrement by User Defined Function

Output:

Figure 6: Output

In the above example, we have declared a function decrement using def keyword. It takes one parameter n which is a number. This function returns a number by subtracting one from it. So, once we define the decrement function in the program and can call it where needed.

Similar Posts