I would like to write a statement in python with logical implication. Something like:
if x => y:
do_sth()
Of course, I know I could use:
if (x and y) or not x:
do_sth()
But is there a logical operator for this in python?
Join Stack Overflow to learn, share knowledge, and build your career.
I would like to write a statement in python with logical implication. Something like:
if x => y:
do_sth()
Of course, I know I could use:
if (x and y) or not x:
do_sth()
But is there a logical operator for this in python?
p => q
is the same as not(p) or q
, so you could try that!
implies(x, y)
- might help with transferring the idea more, if such a construct occurs often enough to warrant a name.
– user2246674
May 6 '13 at 19:39
p and not(q)
– Sylvain
Jan 15 '19 at 10:36
Just because it's funny: x => y could be bool(x) <= bool(y)
in python.
True
should be -1
and False
should be 0
for booleans! (Instead of the current Python convention of True == 1
.) Because then we'd have x => y
matching y <= x
(which looks like a right-to-left implication) for booleans.
– Mark Dickinson
Oct 7 '15 at 18:44
Your question asks if there is a single logical operator for this in Python, the simple answer is no: The docs list boolean operations, and Python simply doesn't have anything like that.
Obviously, as Juampi's answer points out, there are logically equivalent operations that are a little shorter, but no single operators as you asked.
There is a converse implication operator:
if y ** x:
do_sth()
This reads: If y is implied by x.
Credits to https://github.com/cosmologicon/pywat
x => y
, but is not an operator for that purpose. This is the power operator, and is not a logical operator, but a numerical one. It does not return True
or False
, but a number. This is slower, and could potentially introduce bugs, not to mention being incredibly unclear and hard to read. I would highly recommend against ever doing this, and instead would use not(p) or q
as per Juampi's answer.
– Gareth Latty
Nov 22 '15 at 14:02
+
operator is arguably not for the purpose of doing "Hell" + "o"
. Regarding the power operator **
for Booleans that is not properly overloaded and does not return True
/False
, IMO this is a Python's bug or misfeature. If A
and B
are two sets, then A^B
(A
to the power B
) is the standard notation for the set of functions B -> A
. Under Curry–Howard correspondence, a function B -> A
represents a proof of B => A
.
– Alexey
Jul 12 '19 at 9:00
Additional details based on what I have found here and there as I was looking for an implication operator : you can use a clever hack to define your own operators. Here is a running example annotated with sources leading me to this result.
#!/usr/bin/python
# From http://code.activestate.com/recipes/384122/ (via http://stackoverflow.com/questions/932328/python-defining-my-own-operators)
class Infix:
def __init__(self, function):
self.function = function
def __ror__(self, other):
return Infix(lambda x, self=self, other=other: self.function(other, x))
def __rlshift__(self, other):
return Infix(lambda x, self=self, other=other: self.function(other, x))
def __or__(self, other):
return self.function(other)
def __rshift__(self, other):
return self.function(other)
def __call__(self, value1, value2):
return self.function(value1, value2)
from itertools import product
booleans = [False,True]
# http://stackoverflow.com/questions/16405892/is-there-an-implication-logical-operator-in-python
# http://jacob.jkrall.net/lost-operator/
operators=[
(Infix(lambda p,q: False), "F"),
(Infix(lambda p,q: True), "T"),
(Infix(lambda p,q: p and q), "&"),
(Infix(lambda p,q: p or q) , "V"),
(Infix(lambda p,q: p != q) , "^"),
(Infix(lambda p,q: ((not p) or not q)), "nad"),
(Infix(lambda p,q: ((not p) and not q)), "nor"),
(Infix(lambda p,q: ((not p) or q)), "=>"),
]
for op,sym in operators:
print "\nTruth tables for %s" % sym
print "\np\tq\tp %s q\tq %s p" % (sym,sym)
for p,q in product(booleans,repeat=2):
print "%d\t%d\t%d\t%d" % (p,q,p |op| q,q |op| p)
print "\np\tq\tr\tp %s q\tq %s r\t(p %s q) %s r\tp %s (q %s r)\tp %s q %s r" % (sym,sym,sym,sym,sym,sym,sym,sym)
for p,q,r in product(booleans,repeat=3):
print "%d\t%d\t%d\t%d\t%d\t%d\t\t%d\t\t%d" % (p,q,r,p |op| q,q |op| r, (p |op| q) |op| r, p |op| (q |op| r), p |op| q |op| r)
assert( (p |op| q) |op| r == p |op| q |op| r)
I would argue a more readable one-liner would be
x_implies_y = y if x else True
In your original example:
if (y if x else True): do_sth()
I found XOR to be a good solution. you can change A implies B to not A or B. Then you use xor to negate A like this
A^1 or B
Since A xor(^) 1 is equal to not A
You can use the comparison operator <=
to get an implication for two variables.
Examples:
A B A <- B A <= B
0 0 1 1
0 1 1 1
1 0 0 0
1 1 1 1