Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is i++++++++i valid in python?

Tags:

python

I "accidentally" came across this weird but valid syntax

i=3
print i+++i #outputs 6
print i+++++i #outputs 6
print i+-+i #outputs 0
print i+--+i #outputs 6 

(for every even no: of minus symbol, it outputs 6 else 0, why?)

Does this do anything useful?

Update (Don't take it the wrong way..I love python): One of Python's principle says There should be one-- and preferably only one --obvious way to do it. It seems there are infinite ways to do i+1

like image 458
SysAdmin Avatar asked May 21 '10 16:05

SysAdmin


People also ask

What does 2 == mean in Python?

In Python and many other programming languages, a single equal mark is used to assign a value to a variable, whereas two consecutive equal marks is used to check whether 2 expressions give the same value .

What does 2 mean in Python?

b. is not Operator in Python 2 is a number, and '2' is a string.

Is A ++ valid in Python?

Practical Data Science using PythonIn Python these operators won't work. In Python variables are just labels to objects in memory. In Python numeric objects are immutable. Hence by a++ (if a=10) we are trying to increment value of 10 object to 11 which is not allowed.

How to check if a password is valid or invalid in Python?

Then we check if the pattern defined by pat is followed by the input string passwd. If so, the search method returns true, which would allow the password to be valid. print("Password is valid.") print("Password invalid !!")

Why is my syntax not valid in Python?

Often, the cause of invalid syntax in Python code is a missed or mismatched closing parenthesis, bracket, or quote. These can be hard to spot in very long lines of nested parentheses or longer multi-line blocks.

What are the advantages of using validation in Python?

It helps to improve the security of code. Validation in python prevents third-party users from mishandling the code accidentally or intentionally. It can be used to check if the input data type is correct or not. It can be used to check if there are no invalid values in the given input.

How to validate if the input is an integer in Python?

Python program using a flag to validate if the input given by the user is an integer.#Datatype check. Python program uses flag and exception to validate the type of input given by the user and determine if it lies within a given range. #Range Check. Python program using the flag to check the length of the input string.


1 Answers

Since Python doesn't have C-style ++ or -- operators, one is left to assume that you're negating or positivating(?) the value on the left.

E.g. what would you expect i + +5 to be?

i=3
print i + +(+i) #outputs 6
print i + +(+(+(+i))) #outputs 6
print i + -(+i) #outputs 0
print i + -(-(+i)) #outputs 6 

Notably, from the Python Grammar Specification, you'll see the line:

factor: ('+'|'-'|'~') factor | power

Which means that a factor in an expression can be a factor preceded by +, -, or ~. I.e. it's recursive, so if 5 is a factor (which it is because factor->power->NUMBER), then -5 is a factor and so are --5 and --------5.

like image 87
dash-tom-bang Avatar answered Sep 28 '22 01:09

dash-tom-bang