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.
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 optimizea += 1
into theinc
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.
Python integer incrementing with ++
Python doesn't support ++
, but you can do:
number += 1
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.
Python ++ operator doesn't work
There isn't a ++
operator in python. You're applying unary +
twice to the variable.
Why doesn't the operator module have a function for logical or?
The or
and and
operators can't be expressed as functions because of their short-circuiting behavior:
False and some_function()
True or some_function()
in these cases, some_function()
is never called.
A hypothetical or_(True, some_function())
, on the other hand, would have to call some_function()
, because function arguments are always evaluated before the function is called.
increment operator ++i does not cause an error in python
Take a look - it's just a sign:
>>> i = 3
>>> +i
3
>>> ++i
3
>>> +++i
3
>>> -i
-3
>>> --i
3
>>> ---i
-3
Related Topics
Increment a Python Floating Point Value by the Smallest Possible Amount
How to Use the 'JSON' Module to Read in One JSON Object at a Time
How to Start and Stop a Thread
Python: How to Make the Ansi Escape Codes to Work Also in Windows
How to Compare Multiple Variables to the Same Value
How to Handle Elements Inside Shadow Dom from Selenium
Python Regex Match or Operator
Python Pandas Max Value in a Group as a New Column
Correct Code to Remove the Vowels from a String in Python
Is It Ever Useful to Use Python's Input Over Raw_Input
Move an Object Every Few Seconds in Pygame
Why Do Some Functions Have Underscores "_" Before and After the Function Name
How to Remove/Delete a Folder That Is Not Empty