Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is a new feature or bug about operator "- -"? #172

Open
daidai21 opened this issue Jan 13, 2020 · 2 comments
Open

This is a new feature or bug about operator "- -"? #172

daidai21 opened this issue Jan 13, 2020 · 2 comments

Comments

@daidai21
Copy link

print(3 - - 2)
print(3 + + 2)
"""output
5
5
"""
@evanmags
Copy link

Can you explain your question a bit more?

The way I understand what I think you are asking is that this isn't a bug at all.

Python will interpret 3--2 as 3 - (-2) subtracting a negative is the same as adding the positive. So this expression evaluates to 3+2. 3++2 evaluates to the exact same thing 3+(+2) or 3+2. so as you can see both are equal to 5

@satwikkansal
Copy link
Owner

There's already an example in the minor ones section along the similar lines



Given that a is a number, ++a and --a are both valid Python statements but don't behave the same way as compared with similar statements in languages like C, C++, or Java.

>>> a = 5
>>> a
5
>>> ++a
5
>>> --a
5

💡 Explanation:

    There is no ++ operator in Python grammar. It is actually two + operators.
    ++a parses as +(+a) which translates to a. Similarly, the output of the statement --a can be justified.
    This StackOverflow thread discusses the rationale behind the absence of increment and decrement operators in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants