Behaviour of Increment and Decrement Operators in Python

Behaviour of increment and decrement operators in Python

++ is not an operator. It is two + operators. The + operator is the identity operator, which does nothing. (Clarification: the + and - unary operators only work on numbers, but I presume that you wouldn't expect a hypothetical ++ operator to work on strings.)

++count

Parses as

+(+count)

Which translates to

count

You have to use the slightly longer += operator to do what you want to do:

count += 1

I suspect the ++ and -- operators were left out for consistency and simplicity. I don't know the exact argument Guido van Rossum gave for the decision, but I can imagine a few arguments:

  • Simpler parsing. Technically, parsing ++count is ambiguous, as it could be +, +, count (two unary + operators) just as easily as it could be ++, count (one unary ++ operator). It's not a significant syntactic ambiguity, but it does exist.
  • Simpler language. ++ is nothing more than a synonym for += 1. It was a shorthand invented because C compilers were stupid and didn't know how to optimize a += 1 into the inc instruction most computers have. In this day of optimizing compilers and bytecode interpreted languages, adding operators to a language to allow programmers to optimize their code is usually frowned upon, especially in a language like Python that is designed to be consistent and readable.
  • Confusing side-effects. One common newbie error in languages with ++ operators is mixing up the differences (both in precedence and in return value) between the pre- and post-increment/decrement operators, and Python likes to eliminate language "gotcha"-s. The precedence issues of pre-/post-increment in C are pretty hairy, and incredibly easy to mess up.

Is thera a way to use increment and decrement operators directly inside the print statement in python like we do in c

Yes, kind of, using an assignment expression (:=):

print(a := a + 1)

I wouldn't do this though. It needlessly convolutes your code. Just have a separate a += 1 line for the sake of clarity.

Though, this only works in Python 3.8+. If you're on an earlier version of Python, no, there is no way of doing this outside of a creative hack like:

print((exec("a += 1"), a)[1])  # DEFINATELY DO NOT USE THIS!

:= is the only sane way to reassign a variable in a context that expects an expression.

Increment and Decrement in Python

The body of the while is executed from top to bottom. You first tried to increment the value of sum by the value of the first index of the my_list then you printed the sum. If you want to see the 0 as your first output, you have to do the printing first.

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum = 0
index = 0
while sum < 10:
print(sum)
sum += my_list[index]
index += 1

output:

0
1
3
6

Also do not use built-in names(here sum) as your variable names.

Why are there no ++ and --​ operators in Python?

It's not because it doesn't make sense; it makes perfect sense to define "x++" as "x += 1, evaluating to the previous binding of x".

If you want to know the original reason, you'll have to either wade through old Python mailing lists or ask somebody who was there (eg. Guido), but it's easy enough to justify after the fact:

Simple increment and decrement aren't needed as much as in other languages. You don't write things like for(int i = 0; i < 10; ++i) in Python very often; instead you do things like for i in range(0, 10).

Since it's not needed nearly as often, there's much less reason to give it its own special syntax; when you do need to increment, += is usually just fine.

It's not a decision of whether it makes sense, or whether it can be done--it does, and it can. It's a question of whether the benefit is worth adding to the core syntax of the language. Remember, this is four operators--postinc, postdec, preinc, predec, and each of these would need to have its own class overloads; they all need to be specified, and tested; it would add opcodes to the language (implying a larger, and therefore slower, VM engine); every class that supports a logical increment would need to implement them (on top of += and -=).

This is all redundant with += and -=, so it would become a net loss.

Python integer incrementing with ++

Python doesn't support ++, but you can do:

number += 1

How to emulate the increment (++) operator?

You're trying to use a C solution because you're unfamiliar with Python's tools. Using unpacking is much cleaner than trying to emulate ++:

a, b, *c, d, e = l
c = c[0] if c else None

The *c target receives a list of all elements of l that weren't unpacked into the other targets. If this list is nonempty, then c is considered true when coerced to boolean, so c[0] if c else None takes c[0] if there is a c[0] and None otherwise.



Related Topics



Leave a reply



Submit