Multiple Boolean operations like (l and r ) ==None: [duplicate] - python

I'm trying to learn python and came across some code that is nice and short but doesn't totally make sense
the context was:
def fn(*args):
return len(args) and max(args)-min(args)
I get what it's doing, but why does python do this - ie return the value rather than True/False?
10 and 7-2
returns 5. Similarly, changing the and to or will result in a change in functionality. So
10 or 7 - 2
Would return 10.
Is this legit/reliable style, or are there any gotchas on this?

TL;DR
We start by summarising the two behaviour of the two logical operators and and or. These idioms will form the basis of our discussion below.
and
Return the first Falsy value if there are any, else return the last
value in the expression.
or
Return the first Truthy value if there are any, else return the last
value in the expression.
The behaviour is also summarised in the docs, especially in this table:
Operation
Result
x or y
if x is false, then y, else x
x and y
if x is false, then x, else y
not x
if x is false, then True, else False
The only operator returning a boolean value regardless of its operands is the not operator.
"Truthiness", and "Truthy" Evaluations
The statement
len(args) and max(args) - min(args)
Is a very pythonic concise (and arguably less readable) way of saying "if args is not empty, return the result of max(args) - min(args)", otherwise return 0. In general, it is a more concise representation of an if-else expression. For example,
exp1 and exp2
Should (roughly) translate to:
r1 = exp1
if r1:
r1 = exp2
Or, equivalently,
r1 = exp2 if exp1 else exp1
Similarly,
exp1 or exp2
Should (roughly) translate to:
r1 = exp1
if not r1:
r1 = exp2
Or, equivalently,
r1 = exp1 if exp1 else exp2
Where exp1 and exp2 are arbitrary python objects, or expressions that return some object. The key to understanding the uses of the logical and and or operators here is understanding that they are not restricted to operating on, or returning boolean values. Any object with a truthiness value can be tested here. This includes int, str, list, dict, tuple, set, NoneType, and user defined objects. Short circuiting rules still apply as well.
But what is truthiness?
It refers to how objects are evaluated when used in conditional expressions. #Patrick Haugh summarises truthiness nicely in this post.
All values are considered "truthy" except for the following, which are
"falsy":
None
False
0
0.0
0j
Decimal(0)
Fraction(0, 1)
[] - an empty list
{} - an empty dict
() - an empty tuple
'' - an empty str
b'' - an empty bytes
set() - an empty set
an empty range, like range(0)
objects for which
obj.__bool__() returns False
obj.__len__() returns 0
A "truthy" value will satisfy the check performed by if or while
statements. We use "truthy" and "falsy" to differentiate from the
bool values True and False.
How and Works
We build on OP's question as a segue into a discussion on how these operators in these instances.
Given a function with the definition
def foo(*args):
...
How do I return the difference between the minimum and maximum value
in a list of zero or more arguments?
Finding the minimum and maximum is easy (use the inbuilt functions!). The only snag here is appropriately handling the corner case where the argument list could be empty (for example, calling foo()). We can do both in a single line thanks to the and operator:
def foo(*args):
return len(args) and max(args) - min(args)
foo(1, 2, 3, 4, 5)
# 4
foo()
# 0
Since and is used, the second expression must also be evaluated if the first is True. Note that, if the first expression is evaluated to be truthy, the return value is always the result of the second expression. If the first expression is evaluated to be Falsy, then the result returned is the result of the first expression.
In the function above, If foo receives one or more arguments, len(args) is greater than 0 (a positive number), so the result returned is max(args) - min(args). OTOH, if no arguments are passed, len(args) is 0 which is Falsy, and 0 is returned.
Note that an alternative way to write this function would be:
def foo(*args):
if not len(args):
return 0
return max(args) - min(args)
Or, more concisely,
def foo(*args):
return 0 if not args else max(args) - min(args)
If course, none of these functions perform any type checking, so unless you completely trust the input provided, do not rely on the simplicity of these constructs.
How or Works
I explain the working of or in a similar fashion with a contrived example.
Given a function with the definition
def foo(*args):
...
How would you complete foo to return all numbers over 9000?
We use or to handle the corner case here. We define foo as:
def foo(*args):
return [x for x in args if x > 9000] or 'No number over 9000!'
foo(9004, 1, 2, 500)
# [9004]
foo(1, 2, 3, 4)
# 'No number over 9000!'
foo performs a filtration on the list to retain all numbers over 9000. If there exist any such numbers, the result of the list comprehension is a non-empty list which is Truthy, so it is returned (short circuiting in action here). If there exist no such numbers, then the result of the list comp is [] which is Falsy. So the second expression is now evaluated (a non-empty string) and is returned.
Using conditionals, we could re-write this function as,
def foo(*args):
r = [x for x in args if x > 9000]
if not r:
return 'No number over 9000!'
return r
As before, this structure is more flexible in terms of error handling.

Quoting from Python Docs
Note that neither and nor or restrict the value and type they return
to False and True, but rather return the last evaluated argument. This
is sometimes useful, e.g., if s is a string that should be replaced by
a default value if it is empty, the expression s or 'foo' yields the
desired value.
So, this is how Python was designed to evaluate the boolean expressions and the above documentation gives us an insight of why they did it so.
To get a boolean value just typecast it.
return bool(len(args) and max(args)-min(args))
Why?
Short-circuiting.
For example:
2 and 3 # Returns 3 because 2 is Truthy so it has to check 3 too
0 and 3 # Returns 0 because 0 is Falsey and there's no need to check 3 at all
The same goes for or too, that is, it will return the expression which is Truthy as soon as it finds it, cause evaluating the rest of the expression is redundant.
Instead of returning hardcore True or False, Python returns Truthy or Falsey, which are anyway going to evaluate to True or False. You could use the expression as is, and it will still work.
To know what's Truthy and Falsey, check Patrick Haugh's answer

and and or perform boolean logic, but they return one of the actual values when they are comparing. When using and, values are evaluated in a boolean context from left to right. 0, '', [], (), {}, and None are false in a boolean context; everything else is true.
If all values are true in a boolean context, and returns the last value.
>>> 2 and 5
5
>>> 2 and 5 and 10
10
If any value is false in a boolean context and returns the first false value.
>>> '' and 5
''
>>> 2 and 0 and 5
0
So the code
return len(args) and max(args)-min(args)
returns the value of max(args)-min(args) when there is args else it returns len(args) which is 0.

Is this legit/reliable style, or are there any gotchas on this?
This is legit, it is a short circuit evaluation where the last value is returned.
You provide a good example. The function will return 0 if no arguments are passed, and the code doesn't have to check for a special case of no arguments passed.
Another way to use this, is to default None arguments to a mutable primitive, like an empty list:
def fn(alist=None):
alist = alist or []
....
If some non-truthy value is passed to alist it defaults to an empty list, handy way to avoid an if statement and the mutable default argument pitfall

Gotchas
Yes, there are a few gotchas.
fn() == fn(3) == fn(4, 4)
First, if fn returns 0, you cannot know if it was called without any parameter, with one parameter or with multiple, equal parameters :
>>> fn()
0
>>> fn(3)
0
>>> fn(3, 3, 3)
0
What does fn mean?
Then, Python is a dynamic language. It's not specified anywhere what fn does, what its input should be and what its output should look like. Therefore, it's really important to name the function correctly. Similarly, arguments don't have to be called args. delta(*numbers) or calculate_range(*numbers) might describe better what the function is supposed to do.
Argument errors
Finally, the logical and operator is supposed to prevent the function to fail if called without any argument. It still fails if some argument isn't a number, though:
>>> fn('1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fn
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> fn(1, '2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fn
TypeError: '>' not supported between instances of 'str' and 'int'
>>> fn('a', 'b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fn
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Possible alternative
Here's a way to write the function according to the "Easier to ask for forgiveness than permission." principle:
def delta(*numbers):
try:
return max(numbers) - min(numbers)
except TypeError:
raise ValueError("delta should only be called with numerical arguments") from None
except ValueError:
raise ValueError("delta should be called with at least one numerical argument") from None
As an example:
>>> delta()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in delta
ValueError: delta should be called with at least one numerical argument
>>> delta(3)
0
>>> delta('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta('a', 'b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta('a', 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta(3, 4.5)
1.5
>>> delta(3, 5, 7, 2)
5
If you really don't want to raise an exception when delta is called without any argument, you could return some value which cannot be possible otherwise (e.g. -1 or None):
>>> def delta(*numbers):
... try:
... return max(numbers) - min(numbers)
... except TypeError:
... raise ValueError("delta should only be called with numerical arguments") from None
... except ValueError:
... return -1 # or None
...
>>>
>>> delta()
-1

Is this legit/reliable style, or are there any gotchas on this?
I would like to add to this question that it not only legit and reliable but it also ultra practical. Here is a simple example:
>>>example_list = []
>>>print example_list or 'empty list'
empty list
Therefore you can really use it at your advantage. In order to be conscise this is how I see it:
Or operator
Python's or operator returns the first Truth-y value, or the last value, and stops
And operator
Python's and operator returns the first False-y value, or the last value, and stops
Behind the scenes
In python, all numbers are interpreted as True except for 0. Therefore, saying:
0 and 10
is the same as:
False and True
Which is clearly False. It is therefore logical that it returns 0

Yes. This is the correct behaviour of and comparison.
At least in Python, A and B returns B if A is essentially True including if A is NOT Null, NOT None NOT an Empty container (such as an empty list, dict, etc). A is returned IFF A is essentially False or None or Empty or Null.
On the other hand, A or B returns A if A is essentially True including if A is NOT Null, NOT None NOT an Empty container (such as an empty list, dict, etc), otherwise it returns B.
It is easy to not notice (or to overlook) this behaviour because, in Python, any non-null non-empty object evaluates to True is treated like a boolean.
For example, all the following will print "True"
if [102]:
print "True"
else:
print "False"
if "anything that is not empty or None":
print "True"
else:
print "False"
if {1, 2, 3}:
print "True"
else:
print "False"
On the other hand, all the following will print "False"
if []:
print "True"
else:
print "False"
if "":
print "True"
else:
print "False"
if set ([]):
print "True"
else:
print "False"

to understand in simple way,
AND : if first_val is False return first_val else second_value
eg:
1 and 2 # here it will return 2 because 1 is not False
but,
0 and 2 # will return 0 because first value is 0 i.e False
and => if anyone false, it will be false. if both are true then only it will become true
OR : if first_val is False return second_val else first_value
reason is, if first is false it check whether 2 is true or not.
eg:
1 or 2 # here it will return 1 because 1 is not False
but,
0 or 2 # will return 2 because first value is 0 i.e False
or => if anyone false, it will be true. so if first value is false no matter what 2 value suppose to be.
so it returns second value what ever it can be.
if anyone is true then it will become true. if both are false then it will become false.

Related

Wrong logical result when asking substring in string (python) [duplicate]

I'm trying to learn python and came across some code that is nice and short but doesn't totally make sense
the context was:
def fn(*args):
return len(args) and max(args)-min(args)
I get what it's doing, but why does python do this - ie return the value rather than True/False?
10 and 7-2
returns 5. Similarly, changing the and to or will result in a change in functionality. So
10 or 7 - 2
Would return 10.
Is this legit/reliable style, or are there any gotchas on this?
TL;DR
We start by summarising the two behaviour of the two logical operators and and or. These idioms will form the basis of our discussion below.
and
Return the first Falsy value if there are any, else return the last
value in the expression.
or
Return the first Truthy value if there are any, else return the last
value in the expression.
The behaviour is also summarised in the docs, especially in this table:
Operation
Result
x or y
if x is false, then y, else x
x and y
if x is false, then x, else y
not x
if x is false, then True, else False
The only operator returning a boolean value regardless of its operands is the not operator.
"Truthiness", and "Truthy" Evaluations
The statement
len(args) and max(args) - min(args)
Is a very pythonic concise (and arguably less readable) way of saying "if args is not empty, return the result of max(args) - min(args)", otherwise return 0. In general, it is a more concise representation of an if-else expression. For example,
exp1 and exp2
Should (roughly) translate to:
r1 = exp1
if r1:
r1 = exp2
Or, equivalently,
r1 = exp2 if exp1 else exp1
Similarly,
exp1 or exp2
Should (roughly) translate to:
r1 = exp1
if not r1:
r1 = exp2
Or, equivalently,
r1 = exp1 if exp1 else exp2
Where exp1 and exp2 are arbitrary python objects, or expressions that return some object. The key to understanding the uses of the logical and and or operators here is understanding that they are not restricted to operating on, or returning boolean values. Any object with a truthiness value can be tested here. This includes int, str, list, dict, tuple, set, NoneType, and user defined objects. Short circuiting rules still apply as well.
But what is truthiness?
It refers to how objects are evaluated when used in conditional expressions. #Patrick Haugh summarises truthiness nicely in this post.
All values are considered "truthy" except for the following, which are
"falsy":
None
False
0
0.0
0j
Decimal(0)
Fraction(0, 1)
[] - an empty list
{} - an empty dict
() - an empty tuple
'' - an empty str
b'' - an empty bytes
set() - an empty set
an empty range, like range(0)
objects for which
obj.__bool__() returns False
obj.__len__() returns 0
A "truthy" value will satisfy the check performed by if or while
statements. We use "truthy" and "falsy" to differentiate from the
bool values True and False.
How and Works
We build on OP's question as a segue into a discussion on how these operators in these instances.
Given a function with the definition
def foo(*args):
...
How do I return the difference between the minimum and maximum value
in a list of zero or more arguments?
Finding the minimum and maximum is easy (use the inbuilt functions!). The only snag here is appropriately handling the corner case where the argument list could be empty (for example, calling foo()). We can do both in a single line thanks to the and operator:
def foo(*args):
return len(args) and max(args) - min(args)
foo(1, 2, 3, 4, 5)
# 4
foo()
# 0
Since and is used, the second expression must also be evaluated if the first is True. Note that, if the first expression is evaluated to be truthy, the return value is always the result of the second expression. If the first expression is evaluated to be Falsy, then the result returned is the result of the first expression.
In the function above, If foo receives one or more arguments, len(args) is greater than 0 (a positive number), so the result returned is max(args) - min(args). OTOH, if no arguments are passed, len(args) is 0 which is Falsy, and 0 is returned.
Note that an alternative way to write this function would be:
def foo(*args):
if not len(args):
return 0
return max(args) - min(args)
Or, more concisely,
def foo(*args):
return 0 if not args else max(args) - min(args)
If course, none of these functions perform any type checking, so unless you completely trust the input provided, do not rely on the simplicity of these constructs.
How or Works
I explain the working of or in a similar fashion with a contrived example.
Given a function with the definition
def foo(*args):
...
How would you complete foo to return all numbers over 9000?
We use or to handle the corner case here. We define foo as:
def foo(*args):
return [x for x in args if x > 9000] or 'No number over 9000!'
foo(9004, 1, 2, 500)
# [9004]
foo(1, 2, 3, 4)
# 'No number over 9000!'
foo performs a filtration on the list to retain all numbers over 9000. If there exist any such numbers, the result of the list comprehension is a non-empty list which is Truthy, so it is returned (short circuiting in action here). If there exist no such numbers, then the result of the list comp is [] which is Falsy. So the second expression is now evaluated (a non-empty string) and is returned.
Using conditionals, we could re-write this function as,
def foo(*args):
r = [x for x in args if x > 9000]
if not r:
return 'No number over 9000!'
return r
As before, this structure is more flexible in terms of error handling.
Quoting from Python Docs
Note that neither and nor or restrict the value and type they return
to False and True, but rather return the last evaluated argument. This
is sometimes useful, e.g., if s is a string that should be replaced by
a default value if it is empty, the expression s or 'foo' yields the
desired value.
So, this is how Python was designed to evaluate the boolean expressions and the above documentation gives us an insight of why they did it so.
To get a boolean value just typecast it.
return bool(len(args) and max(args)-min(args))
Why?
Short-circuiting.
For example:
2 and 3 # Returns 3 because 2 is Truthy so it has to check 3 too
0 and 3 # Returns 0 because 0 is Falsey and there's no need to check 3 at all
The same goes for or too, that is, it will return the expression which is Truthy as soon as it finds it, cause evaluating the rest of the expression is redundant.
Instead of returning hardcore True or False, Python returns Truthy or Falsey, which are anyway going to evaluate to True or False. You could use the expression as is, and it will still work.
To know what's Truthy and Falsey, check Patrick Haugh's answer
and and or perform boolean logic, but they return one of the actual values when they are comparing. When using and, values are evaluated in a boolean context from left to right. 0, '', [], (), {}, and None are false in a boolean context; everything else is true.
If all values are true in a boolean context, and returns the last value.
>>> 2 and 5
5
>>> 2 and 5 and 10
10
If any value is false in a boolean context and returns the first false value.
>>> '' and 5
''
>>> 2 and 0 and 5
0
So the code
return len(args) and max(args)-min(args)
returns the value of max(args)-min(args) when there is args else it returns len(args) which is 0.
Is this legit/reliable style, or are there any gotchas on this?
This is legit, it is a short circuit evaluation where the last value is returned.
You provide a good example. The function will return 0 if no arguments are passed, and the code doesn't have to check for a special case of no arguments passed.
Another way to use this, is to default None arguments to a mutable primitive, like an empty list:
def fn(alist=None):
alist = alist or []
....
If some non-truthy value is passed to alist it defaults to an empty list, handy way to avoid an if statement and the mutable default argument pitfall
Gotchas
Yes, there are a few gotchas.
fn() == fn(3) == fn(4, 4)
First, if fn returns 0, you cannot know if it was called without any parameter, with one parameter or with multiple, equal parameters :
>>> fn()
0
>>> fn(3)
0
>>> fn(3, 3, 3)
0
What does fn mean?
Then, Python is a dynamic language. It's not specified anywhere what fn does, what its input should be and what its output should look like. Therefore, it's really important to name the function correctly. Similarly, arguments don't have to be called args. delta(*numbers) or calculate_range(*numbers) might describe better what the function is supposed to do.
Argument errors
Finally, the logical and operator is supposed to prevent the function to fail if called without any argument. It still fails if some argument isn't a number, though:
>>> fn('1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fn
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> fn(1, '2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fn
TypeError: '>' not supported between instances of 'str' and 'int'
>>> fn('a', 'b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fn
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Possible alternative
Here's a way to write the function according to the "Easier to ask for forgiveness than permission." principle:
def delta(*numbers):
try:
return max(numbers) - min(numbers)
except TypeError:
raise ValueError("delta should only be called with numerical arguments") from None
except ValueError:
raise ValueError("delta should be called with at least one numerical argument") from None
As an example:
>>> delta()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in delta
ValueError: delta should be called with at least one numerical argument
>>> delta(3)
0
>>> delta('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta('a', 'b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta('a', 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta(3, 4.5)
1.5
>>> delta(3, 5, 7, 2)
5
If you really don't want to raise an exception when delta is called without any argument, you could return some value which cannot be possible otherwise (e.g. -1 or None):
>>> def delta(*numbers):
... try:
... return max(numbers) - min(numbers)
... except TypeError:
... raise ValueError("delta should only be called with numerical arguments") from None
... except ValueError:
... return -1 # or None
...
>>>
>>> delta()
-1
Is this legit/reliable style, or are there any gotchas on this?
I would like to add to this question that it not only legit and reliable but it also ultra practical. Here is a simple example:
>>>example_list = []
>>>print example_list or 'empty list'
empty list
Therefore you can really use it at your advantage. In order to be conscise this is how I see it:
Or operator
Python's or operator returns the first Truth-y value, or the last value, and stops
And operator
Python's and operator returns the first False-y value, or the last value, and stops
Behind the scenes
In python, all numbers are interpreted as True except for 0. Therefore, saying:
0 and 10
is the same as:
False and True
Which is clearly False. It is therefore logical that it returns 0
Yes. This is the correct behaviour of and comparison.
At least in Python, A and B returns B if A is essentially True including if A is NOT Null, NOT None NOT an Empty container (such as an empty list, dict, etc). A is returned IFF A is essentially False or None or Empty or Null.
On the other hand, A or B returns A if A is essentially True including if A is NOT Null, NOT None NOT an Empty container (such as an empty list, dict, etc), otherwise it returns B.
It is easy to not notice (or to overlook) this behaviour because, in Python, any non-null non-empty object evaluates to True is treated like a boolean.
For example, all the following will print "True"
if [102]:
print "True"
else:
print "False"
if "anything that is not empty or None":
print "True"
else:
print "False"
if {1, 2, 3}:
print "True"
else:
print "False"
On the other hand, all the following will print "False"
if []:
print "True"
else:
print "False"
if "":
print "True"
else:
print "False"
if set ([]):
print "True"
else:
print "False"
to understand in simple way,
AND : if first_val is False return first_val else second_value
eg:
1 and 2 # here it will return 2 because 1 is not False
but,
0 and 2 # will return 0 because first value is 0 i.e False
and => if anyone false, it will be false. if both are true then only it will become true
OR : if first_val is False return second_val else first_value
reason is, if first is false it check whether 2 is true or not.
eg:
1 or 2 # here it will return 1 because 1 is not False
but,
0 or 2 # will return 2 because first value is 0 i.e False
or => if anyone false, it will be true. so if first value is false no matter what 2 value suppose to be.
so it returns second value what ever it can be.
if anyone is true then it will become true. if both are false then it will become false.

Why does Python switch from 1 or 0 to True or False?

I'm learning about logic gates and was messing around with them when I found something interesting. This is my code:
for i in range (4):
p = math.floor(i/2)
q = i % 2
a = p and q
b = not(not(p or q) or not(q) or not(p))
print(str(p) +"\t"+ str(q) + "\t"+ str(a)+"\t"+str(b))
And this is the result:
0 0 0 False
0 1 0 False
1 0 0 False
1 1 1 True
Why doesn't Python either always print a 1 or a 0 or always print True or False?
math.floor returns a number.
Operators between two numbers (such as modulo) also return numbers.
not returns a bool, not an int
Because not has to create a new value, it returns a boolean value regardless of the type of its argument - spec
If you want it to be an int (and simplify the print statement)
print('\t'.join(map(str, [p, q, a, int(b)])
The not unary operator always returns a bool value. It needs to, as it needs to return the boolean inverse of whatever value it was passed, which is always a new value (not its argument).
The real odd part of your chart is actually the third column, which shows that the and operator does return numbers if it's passed them on both sides. You might expect and to be a boolean operator just like not, but it's slightly different. The reason is that and always returns one of its arguments. If the first argument is falsey, that value is what is returned. If the first argument is truthy, the second argument is returned.
You can see this if you test with values other than just 0 and 1:
print(3 and 2) # prints 2
print([] and [1,2]) # prints []
The or operator also does this (in a slightly different manner), but that's covered up in your calculation of b by the not calls.
This behavior of and and or is called short-circuiting. It's big advantage is that it lets Python avoid interpreting expressions it doesn't need to get the value of the boolean expression. For instance, this expression will not call the slow_function:
result = falsey_value and slow_function()
It also lets you guard expressions that would cause exceptions if they were evaluated:
if node is not None and node.value > x:
...
If the node variable is None in that code, evaluating node.value would give an AttributeError (since None doesn't have a value attribute). But because and short circuits, the first expression prevents the second expression from being evaluated when it's invalid.

How does lambda x: (x % 2 and 'odd' or 'even') work [duplicate]

I'm trying to learn python and came across some code that is nice and short but doesn't totally make sense
the context was:
def fn(*args):
return len(args) and max(args)-min(args)
I get what it's doing, but why does python do this - ie return the value rather than True/False?
10 and 7-2
returns 5. Similarly, changing the and to or will result in a change in functionality. So
10 or 7 - 2
Would return 10.
Is this legit/reliable style, or are there any gotchas on this?
TL;DR
We start by summarising the two behaviour of the two logical operators and and or. These idioms will form the basis of our discussion below.
and
Return the first Falsy value if there are any, else return the last
value in the expression.
or
Return the first Truthy value if there are any, else return the last
value in the expression.
The behaviour is also summarised in the docs, especially in this table:
Operation
Result
x or y
if x is false, then y, else x
x and y
if x is false, then x, else y
not x
if x is false, then True, else False
The only operator returning a boolean value regardless of its operands is the not operator.
"Truthiness", and "Truthy" Evaluations
The statement
len(args) and max(args) - min(args)
Is a very pythonic concise (and arguably less readable) way of saying "if args is not empty, return the result of max(args) - min(args)", otherwise return 0. In general, it is a more concise representation of an if-else expression. For example,
exp1 and exp2
Should (roughly) translate to:
r1 = exp1
if r1:
r1 = exp2
Or, equivalently,
r1 = exp2 if exp1 else exp1
Similarly,
exp1 or exp2
Should (roughly) translate to:
r1 = exp1
if not r1:
r1 = exp2
Or, equivalently,
r1 = exp1 if exp1 else exp2
Where exp1 and exp2 are arbitrary python objects, or expressions that return some object. The key to understanding the uses of the logical and and or operators here is understanding that they are not restricted to operating on, or returning boolean values. Any object with a truthiness value can be tested here. This includes int, str, list, dict, tuple, set, NoneType, and user defined objects. Short circuiting rules still apply as well.
But what is truthiness?
It refers to how objects are evaluated when used in conditional expressions. #Patrick Haugh summarises truthiness nicely in this post.
All values are considered "truthy" except for the following, which are
"falsy":
None
False
0
0.0
0j
Decimal(0)
Fraction(0, 1)
[] - an empty list
{} - an empty dict
() - an empty tuple
'' - an empty str
b'' - an empty bytes
set() - an empty set
an empty range, like range(0)
objects for which
obj.__bool__() returns False
obj.__len__() returns 0
A "truthy" value will satisfy the check performed by if or while
statements. We use "truthy" and "falsy" to differentiate from the
bool values True and False.
How and Works
We build on OP's question as a segue into a discussion on how these operators in these instances.
Given a function with the definition
def foo(*args):
...
How do I return the difference between the minimum and maximum value
in a list of zero or more arguments?
Finding the minimum and maximum is easy (use the inbuilt functions!). The only snag here is appropriately handling the corner case where the argument list could be empty (for example, calling foo()). We can do both in a single line thanks to the and operator:
def foo(*args):
return len(args) and max(args) - min(args)
foo(1, 2, 3, 4, 5)
# 4
foo()
# 0
Since and is used, the second expression must also be evaluated if the first is True. Note that, if the first expression is evaluated to be truthy, the return value is always the result of the second expression. If the first expression is evaluated to be Falsy, then the result returned is the result of the first expression.
In the function above, If foo receives one or more arguments, len(args) is greater than 0 (a positive number), so the result returned is max(args) - min(args). OTOH, if no arguments are passed, len(args) is 0 which is Falsy, and 0 is returned.
Note that an alternative way to write this function would be:
def foo(*args):
if not len(args):
return 0
return max(args) - min(args)
Or, more concisely,
def foo(*args):
return 0 if not args else max(args) - min(args)
If course, none of these functions perform any type checking, so unless you completely trust the input provided, do not rely on the simplicity of these constructs.
How or Works
I explain the working of or in a similar fashion with a contrived example.
Given a function with the definition
def foo(*args):
...
How would you complete foo to return all numbers over 9000?
We use or to handle the corner case here. We define foo as:
def foo(*args):
return [x for x in args if x > 9000] or 'No number over 9000!'
foo(9004, 1, 2, 500)
# [9004]
foo(1, 2, 3, 4)
# 'No number over 9000!'
foo performs a filtration on the list to retain all numbers over 9000. If there exist any such numbers, the result of the list comprehension is a non-empty list which is Truthy, so it is returned (short circuiting in action here). If there exist no such numbers, then the result of the list comp is [] which is Falsy. So the second expression is now evaluated (a non-empty string) and is returned.
Using conditionals, we could re-write this function as,
def foo(*args):
r = [x for x in args if x > 9000]
if not r:
return 'No number over 9000!'
return r
As before, this structure is more flexible in terms of error handling.
Quoting from Python Docs
Note that neither and nor or restrict the value and type they return
to False and True, but rather return the last evaluated argument. This
is sometimes useful, e.g., if s is a string that should be replaced by
a default value if it is empty, the expression s or 'foo' yields the
desired value.
So, this is how Python was designed to evaluate the boolean expressions and the above documentation gives us an insight of why they did it so.
To get a boolean value just typecast it.
return bool(len(args) and max(args)-min(args))
Why?
Short-circuiting.
For example:
2 and 3 # Returns 3 because 2 is Truthy so it has to check 3 too
0 and 3 # Returns 0 because 0 is Falsey and there's no need to check 3 at all
The same goes for or too, that is, it will return the expression which is Truthy as soon as it finds it, cause evaluating the rest of the expression is redundant.
Instead of returning hardcore True or False, Python returns Truthy or Falsey, which are anyway going to evaluate to True or False. You could use the expression as is, and it will still work.
To know what's Truthy and Falsey, check Patrick Haugh's answer
and and or perform boolean logic, but they return one of the actual values when they are comparing. When using and, values are evaluated in a boolean context from left to right. 0, '', [], (), {}, and None are false in a boolean context; everything else is true.
If all values are true in a boolean context, and returns the last value.
>>> 2 and 5
5
>>> 2 and 5 and 10
10
If any value is false in a boolean context and returns the first false value.
>>> '' and 5
''
>>> 2 and 0 and 5
0
So the code
return len(args) and max(args)-min(args)
returns the value of max(args)-min(args) when there is args else it returns len(args) which is 0.
Is this legit/reliable style, or are there any gotchas on this?
This is legit, it is a short circuit evaluation where the last value is returned.
You provide a good example. The function will return 0 if no arguments are passed, and the code doesn't have to check for a special case of no arguments passed.
Another way to use this, is to default None arguments to a mutable primitive, like an empty list:
def fn(alist=None):
alist = alist or []
....
If some non-truthy value is passed to alist it defaults to an empty list, handy way to avoid an if statement and the mutable default argument pitfall
Gotchas
Yes, there are a few gotchas.
fn() == fn(3) == fn(4, 4)
First, if fn returns 0, you cannot know if it was called without any parameter, with one parameter or with multiple, equal parameters :
>>> fn()
0
>>> fn(3)
0
>>> fn(3, 3, 3)
0
What does fn mean?
Then, Python is a dynamic language. It's not specified anywhere what fn does, what its input should be and what its output should look like. Therefore, it's really important to name the function correctly. Similarly, arguments don't have to be called args. delta(*numbers) or calculate_range(*numbers) might describe better what the function is supposed to do.
Argument errors
Finally, the logical and operator is supposed to prevent the function to fail if called without any argument. It still fails if some argument isn't a number, though:
>>> fn('1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fn
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> fn(1, '2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fn
TypeError: '>' not supported between instances of 'str' and 'int'
>>> fn('a', 'b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fn
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Possible alternative
Here's a way to write the function according to the "Easier to ask for forgiveness than permission." principle:
def delta(*numbers):
try:
return max(numbers) - min(numbers)
except TypeError:
raise ValueError("delta should only be called with numerical arguments") from None
except ValueError:
raise ValueError("delta should be called with at least one numerical argument") from None
As an example:
>>> delta()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in delta
ValueError: delta should be called with at least one numerical argument
>>> delta(3)
0
>>> delta('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta('a', 'b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta('a', 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta(3, 4.5)
1.5
>>> delta(3, 5, 7, 2)
5
If you really don't want to raise an exception when delta is called without any argument, you could return some value which cannot be possible otherwise (e.g. -1 or None):
>>> def delta(*numbers):
... try:
... return max(numbers) - min(numbers)
... except TypeError:
... raise ValueError("delta should only be called with numerical arguments") from None
... except ValueError:
... return -1 # or None
...
>>>
>>> delta()
-1
Is this legit/reliable style, or are there any gotchas on this?
I would like to add to this question that it not only legit and reliable but it also ultra practical. Here is a simple example:
>>>example_list = []
>>>print example_list or 'empty list'
empty list
Therefore you can really use it at your advantage. In order to be conscise this is how I see it:
Or operator
Python's or operator returns the first Truth-y value, or the last value, and stops
And operator
Python's and operator returns the first False-y value, or the last value, and stops
Behind the scenes
In python, all numbers are interpreted as True except for 0. Therefore, saying:
0 and 10
is the same as:
False and True
Which is clearly False. It is therefore logical that it returns 0
Yes. This is the correct behaviour of and comparison.
At least in Python, A and B returns B if A is essentially True including if A is NOT Null, NOT None NOT an Empty container (such as an empty list, dict, etc). A is returned IFF A is essentially False or None or Empty or Null.
On the other hand, A or B returns A if A is essentially True including if A is NOT Null, NOT None NOT an Empty container (such as an empty list, dict, etc), otherwise it returns B.
It is easy to not notice (or to overlook) this behaviour because, in Python, any non-null non-empty object evaluates to True is treated like a boolean.
For example, all the following will print "True"
if [102]:
print "True"
else:
print "False"
if "anything that is not empty or None":
print "True"
else:
print "False"
if {1, 2, 3}:
print "True"
else:
print "False"
On the other hand, all the following will print "False"
if []:
print "True"
else:
print "False"
if "":
print "True"
else:
print "False"
if set ([]):
print "True"
else:
print "False"
to understand in simple way,
AND : if first_val is False return first_val else second_value
eg:
1 and 2 # here it will return 2 because 1 is not False
but,
0 and 2 # will return 0 because first value is 0 i.e False
and => if anyone false, it will be false. if both are true then only it will become true
OR : if first_val is False return second_val else first_value
reason is, if first is false it check whether 2 is true or not.
eg:
1 or 2 # here it will return 1 because 1 is not False
but,
0 or 2 # will return 2 because first value is 0 i.e False
or => if anyone false, it will be true. so if first value is false no matter what 2 value suppose to be.
so it returns second value what ever it can be.
if anyone is true then it will become true. if both are false then it will become false.

Boolean expressions in Python

I have a Python script I'm running that tests for the conjunction of two conditions, one of which is easy to verify and the other hard. Say I write it as easy_boole and hard_boole in Python. Will the interpreter always check easy_boole first and then return False if easy_boole == False? Is the interpreter optimized in general to resolve these kinds of statements as quickly as possible?
Yes, both and and or are so called short-circuit operators. The evaluation of an and expression ends as soon as a value is falsy, the evaluation of an or expression ends as soon as a value is truthy.
You can find the relevant documentation here.
Here is a piece of code with which you can observe this behavior yourself:
def fib(n):
if n <= 2:
return 1
return fib(n-1) + fib(n-2)
print(False and fib(100)) # prints False immediately
print(True and fib(100)) # takes very, very long
print(fib(100) and False) # takes very, very long
So with that in mind, always use easy_boole and hard_boole.
Just open up a REPL and try:
>>> False and 1 / 0
False
>> True or 1 / 0
True
>>> False or 1 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
That means that Python indeed evaluates boolean statements lazily.
P.S. It's a duplicate
From Python Documentation:
The expression x and y first evaluates x; if x is false, its value is
returned; otherwise, y is evaluated and the resulting value is
returned.
So as long as x is False, the expression will be evaluated to False
Yes, python evaluates if statements lazily. For example, in following code:
if f() and g():
print("GOOD")
else:
print("BAD")
Python interpreter will first check f() statement and if f() is False, it will immediately jump on else statement.

Using reduce mul to return 1 instead of None on blank list

I'm trying to return 1 instead of None when I pass an empty list through reduce(mul, a). My code:
from operator import mul
def product_list(a):
for b in a:
b = reduce(mul, a)
if b == None:
return 1
return b
print product_list([])
No matter where I place the if statement to catch for a blank list, I still receive None as output. I am still learning basics, but this makes no sense to me. I've even tried
from operator import mul
def product_list(a):
if a == None:
return 1
else:
for b in a:
b = reduce(mul, a)
if b == None or a == None:
return 1
return b
print product_list([])
just to see if it would catch the None and return 1. Does reduce() not act the way I think that it does, or is there an obvious mistake in my code that prohibits returning 1 and forces a return of None?
When a is an empty list, your function doesn't return anything, and the default return value is None.
Test for the empty list at the top:
if not a:
return 1
In your second function you only test for if a == None, but an empty list [] is never equal to None. Note that the idiomatic way to test for None is using the is object identity test instead:
if a is None:
By testing for not a instead, you catch both the case where a is an empty list and a being None.
Your code otherwise makes little sense. You loop over a but return and exit the function in the first iteration:
for b in a:
b = reduce(mul, a)
if b == None:
return 1
return b # exit the function here, having only looked at the first element in `a`.
However, I had to fix the indentation in your post and may have misunderstood the indentation of those return statements, in which case you would get a NameError instead when passing in an empty list.
You can pass a third value to reduce, which is used as a starter value.
In [6]: reduce(mul, [], 1)
Out[6]: 1
This is the best way to deal with an empty list. The case None should really be dealt with elsewhere, because it's a different kind of error: it's nothing wrong with the semantics of the program, it's because someone else has given you bad data. You should catch that explicitly, as e.g.
if not isinstance(..., collections.Iterable):
# do something
Of course, reduce will raise an error if you pass it something not iterable, and that may suffice for you.
Note that you're not passing an empty list to reduce as you say. Try it:
>>> reduce(operator.mul, [])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: reduce() of empty sequence with no initial value
I think that perhaps you don't understand the function reduce. We can't hold it against you though -- it's not used much in python code.
Perhaps you wanted to define a function like this:
from operator import mul
def product_list(a):
try:
return reduce(mul,a)
except TypeError:
return 1
Now you can try it:
print product_list([1,2,3,4]) #24
print product_list([]) #1
if a is None or len(a) == 0:
return 1
Check the empty list condition as above.

Categories