89DEVs

How to use i++ in Python?

++ and -- operators

Most programming languages like C++, Java, or PHP have a ++ and -- operator, to increment or decrement a variable by one. This is quite useful for example in loops to increment a counter with each iteration. In the PHP programming language, a counter can be increased and decreased by one as shown below. # example of the ++ operator in PHP $counter = 0; # increment counter by 1 $counter++; # decrement counter by 1 $counter--;

is there i++ operator in Python?

We can answer this question, by simply writing two lines of code to test if Python has an ++ operator or not: First, we define a variable and then we try to increment it by 1 using the ++ operator. # testing ++ operator in Python number = 1 number++ The code returns a SyntaxError because Python doesn't have ++ or -- operators. SyntaxError: invalid syntax Using ++ in your code doesn't work, the alternative we can use instead is the += operator. It is the equivalent of the ++ operator and does exactly the same. It is even more useful because it allows also to increase a variable by a different value for example by 2. This is the fastest way to increment in Python, the variable is incremented and then reassigned. number = 1 number += 2 print(number) The variable number is increased by 2 and the result is 3. 3 Using the += operator is still more concise than just reassigning the increased value as done below. number = 1 number = number + 2 print(number) The result is the same, an integer value of 3 is returned. But the code is much longer and we repeated the variable. So it is recommended to use the += operator instead. 3

why does python not have ++ and -- operators?

The reason why Python doesn't have ++ and -- operators is simple. In Python integers are immutable, they can not be changed. The ++ and -- operators would change the current object, while the -= operator returns a different object.

increment by 1 using += operator

To increment a number or a counter by 1, use the += operator as shown below. # increment by 1 using += operator # declare number and set value to 1 number = 1 # increment number by 1 using += operator number += 1 # print number print(number) The variable number is declared and the integer value 1 is assigned to it. Then number is increased by 1, using the += operator. Finally, the increased number is printed and the result is as expected an integer value of 2. 2 Since Python doesn't have an ++ operator it also doesn't have an -- operator.

decrement by 1 using -= operator

To decrement a variable by 1, use the -= operator as shown below. # decrement by 1 using -= operator # declare number and set value to 3 number = 3 # decrement number by 1 number -= 1 # print number print(number) The variable number is declared and an integer value of 3 is assigned to it. Then the variable number is decreased by 1, using the -= operator. Finally, the decreased number is printed and the result is as expected an integer value of 2. 2

increment counter/variable in for loop

The ++ operator is a short form of writing variable = variable + 1 and the most used case is to increase a counter or variable in a loop. In Python, use the += operator to increase a counter in a loop as shown below. # increment counter in a for loop words = ['hello', 'world', 'python'] counter = 0 for item in words: counter += 1 print(counter) In this example, the list words contains 3 elements of type string. Then, a variable named counter is declared and the integer value 0 is assigned to it. The for loop iterates through all elements of the words list and the counter is increased by one each time, using the += operator. Finally, after the loop is finished the increased counter is printed and the result is an integer value of 3. The counter has been increased three times, once for each element of the for loop. 3

increment counter/variable in while loop

Similar to how we used the += operator in the for loop, we can use it in the while loop as the example below shows. # increment counter in a while loop counter = 0 while counter < 6: counter += 1 print(counter) In this example, the variable counter is declared and the integer value 0 is assigned to it. The while loop is iterating until the counter is small than 6 and within each iteration the counter is increased by 1. After the while loop, the increased counter is printed and as expected its value is 6. 6

writing a function for ++

Another way to replace the ++ operator in Python is to simply write a function with the same funtionality. # plus1 function to increment by 1 def plus1(number): return number + 1 counter = 0 counter = plus1(counter) print(counter) In this example, the function plus1 is defined and it accepts one number as argument. It then returns the number increased by 1. Then, the counter variable is defined and the integer value 0 is assigned to it. And after that the plus1() function is called and the counter is passed as argument. Finally, the increased value of 1 is printed. 1 It works the same way for the -- operator. The only difference is that the minus1 function reduces the number by 1, instead of increasing it by 1. # minus1 function to decrement by 1 def minus1(number): return number - 1 counter = 2 counter = minus1(counter) print(counter) The minus1 function is defined and accepts one number as argument. It returns the number - 1. Then the counter variable is set to an integer value of 2. Next the minus1() function is called and counter is passed as an argument. Finally, the decreased value of 1 is printed as shown below. 1

                
        

Summary


Click to jump to section