Using the AND and NOT Operator in Python
Question:
Here is my custom class that I have that represents a triangle. I’m trying to write code that checks to see if self.a
, self.b
, and self.c
are greater than 0, which would mean that I have Angle, Angle, Angle.
Below you will see the code that checks for A and B, however when I use just self.a != 0
then it works fine. I believe I’m not using &
correctly. Any ideas? Here is how I am calling it: print myTri.detType()
class Triangle:
# Angle A To Angle C Connects Side F
# Angle C to Angle B Connects Side D
# Angle B to Angle A Connects Side E
def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
def detType(self):
#Triangle Type AAA
if self.a != 0 & self.b != 0:
return self.a
#If self.a > 10:
#return AAA
#Triangle Type AAS
#elif self.a = 0:
#return AAS
#Triangle Type ASA
#Triangle Type SAS
#Triangle Type SSS
#else:
#return unknown
Answers:
Use the keyword and
, not &
because &
is a bit operator.
Be careful with this… just so you know, in Java and C++, the &
operator is ALSO a bit operator. The correct way to do a boolean comparison in those languages is &&
. Similarly |
is a bit operator, and ||
is a boolean operator. In Python and
and or
are used for boolean comparisons.
It’s called and
and or
in Python.
You should write :
if (self.a != 0) and (self.b != 0) :
“&
” is the bit wise operator and does not suit for boolean operations. The equivalent of “&&
” is “and” in Python.
A shorter way to check what you want is to use the “in” operator :
if 0 not in (self.a, self.b) :
You can check if anything is part of a an iterable with “in”, it works for :
- Tuples. I.E :
"foo" in ("foo", 1, c, etc)
will return true
- Lists. I.E :
"foo" in ["foo", 1, c, etc]
will return true
- Strings. I.E :
"a" in "ago"
will return true
- Dict. I.E :
"foo" in {"foo" : "bar"}
will return true
As an answer to the comments :
Yes, using “in” is slower since you are creating an Tuple object, but really performances are not an issue here, plus readability matters a lot in Python.
For the triangle check, it’s easier to read :
0 not in (self.a, self.b, self.c)
Than
(self.a != 0) and (self.b != 0) and (self.c != 0)
It’s easier to refactor too.
Of course, in this example, it really is not that important, it’s very simple snippet. But this style leads to a Pythonic code, which leads to a happier programmer (and losing weight, improving sex life, etc.) on big programs.
Here is my custom class that I have that represents a triangle. I’m trying to write code that checks to see if self.a
, self.b
, and self.c
are greater than 0, which would mean that I have Angle, Angle, Angle.
Below you will see the code that checks for A and B, however when I use just self.a != 0
then it works fine. I believe I’m not using &
correctly. Any ideas? Here is how I am calling it: print myTri.detType()
class Triangle:
# Angle A To Angle C Connects Side F
# Angle C to Angle B Connects Side D
# Angle B to Angle A Connects Side E
def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
def detType(self):
#Triangle Type AAA
if self.a != 0 & self.b != 0:
return self.a
#If self.a > 10:
#return AAA
#Triangle Type AAS
#elif self.a = 0:
#return AAS
#Triangle Type ASA
#Triangle Type SAS
#Triangle Type SSS
#else:
#return unknown
Use the keyword and
, not &
because &
is a bit operator.
Be careful with this… just so you know, in Java and C++, the &
operator is ALSO a bit operator. The correct way to do a boolean comparison in those languages is &&
. Similarly |
is a bit operator, and ||
is a boolean operator. In Python and
and or
are used for boolean comparisons.
It’s called and
and or
in Python.
You should write :
if (self.a != 0) and (self.b != 0) :
“&
” is the bit wise operator and does not suit for boolean operations. The equivalent of “&&
” is “and” in Python.
A shorter way to check what you want is to use the “in” operator :
if 0 not in (self.a, self.b) :
You can check if anything is part of a an iterable with “in”, it works for :
- Tuples. I.E :
"foo" in ("foo", 1, c, etc)
will return true - Lists. I.E :
"foo" in ["foo", 1, c, etc]
will return true - Strings. I.E :
"a" in "ago"
will return true - Dict. I.E :
"foo" in {"foo" : "bar"}
will return true
As an answer to the comments :
Yes, using “in” is slower since you are creating an Tuple object, but really performances are not an issue here, plus readability matters a lot in Python.
For the triangle check, it’s easier to read :
0 not in (self.a, self.b, self.c)
Than
(self.a != 0) and (self.b != 0) and (self.c != 0)
It’s easier to refactor too.
Of course, in this example, it really is not that important, it’s very simple snippet. But this style leads to a Pythonic code, which leads to a happier programmer (and losing weight, improving sex life, etc.) on big programs.