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
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 .
b. is not Operator in Python 2 is a number, and '2' is a string.
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.
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 !!")
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With