Python's equivalent of && (logical-and) in an if-statement
Use and
instead of &&
.
Using IF, AND, OR together with EQUAL operand together in Python
It helps to examine the logic of this line:
if (len(x) == (4 or 6)):
The (4 or 6)
clause contains a logical or
short circuit. The value 4
is true, so it is evaluated and returned to the ==
relational comparison.
The way that or
works is that its lefthand side is evaluated for Boolean truth, and its value is returned if true. If the lefthand side is not Boolean true, then the righthand side is evaluated and its value is returned.
Because the lefthand side of the 4 or ...
is true in a Boolean sense, the righthand side is never evaluated. Python doesn't even look past 4 or
. If the left-hand value were a false value (such as 0), then the right hand side of the or
would be evaluated.
To see this in action, try print 4 or 6
. The output will be 4.
So since the 4 is a hard-coded true value, your comparison is semantically the same as if (len(x) == 4)
-- that is, since 4 is true, 6 is never evaluated.
What I suppose you really want to know is if len(x)
is either 4 or 6. You could put that to code in a couple of ways:
if(len(x) == 4 or len(x) == 6 ...
if(len(x) in (4,6) ...
How to have multiple conditions for one if statement in python
I would use
def example(arg1, arg2, arg3):
if arg1 == 1 and arg2 == 2 and arg3 == 3:
print("Example Text")
The and
operator is identical to the logic gate with the same name; it will return 1 if and only if all of the inputs are 1. You can also use or
operator if you want that logic gate.
EDIT: Actually, the code provided in your post works fine with me. I don't see any problems with that. I think that this might be a problem with your Python, not the actual language.
Python's Logical Operator AND
Python Boolean operators return the last value evaluated, not True/False. The docs have a good explanation of this:
The expression
x and y
first evaluatesx
; ifx
isfalse
, its value is returned; otherwise,y
is evaluated and the resulting value is returned.
Behaviour of bitwise AND and logical AND along with equality operator in Python 3
&
has higher precedence than ==
.
So this:
1110010110001000 == 1110010110001000 & 1 == 1 & 1000001000001101 == 1000001000001101 & 1 == 1
means
1110010110001000 == (1110010110001000 & 1) == (1 & 1000001000001101) == (1000001000001101 & 1) == 1
which means
1110010110001000 == 0 == 1 == 1 == 1
which means (due to operator chaining)
(1110010110001000 == 0) and (0 == 1) and (1 == 1) and (1 == 1)
which is false.
In summary, don't use &
to mean logical and
.
Python if statement not working correctly and no idea why
I believe you should be using "and" instead of "&" in the if statements. You can read up on the differences here:
https://www.geeksforgeeks.org/difference-between-and-and-in-python/
Using the AND and NOT Operator 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.
Related Topics
Why Is Dictionary Ordering Non-Deterministic
How to Log While Using Multiprocessing in Python
How to Detect Whether a Python Variable Is a Function
Is Python Interpreted, or Compiled, or Both
How to Execute Raw SQL in Flask-Sqlalchemy App
Parse a .Py File, Read the Ast, Modify It, Then Write Back the Modified Source Code
How to Open a File for Both Reading and Writing
Performance of Pandas Apply VS Np.Vectorize to Create New Column from Existing Columns
How to Access the Query String in Flask Routes
How to Validate a Url with a Regular Expression in Python
How to Specify New Lines on Python, When Writing on Files
How to Validate Ip Address in Python
How to Run an External Command Asynchronously from Python
Is Shared Readonly Data Copied to Different Processes for Multiprocessing
In Python, How to Read the Exif Data for an Image
In Python, How to Convert a 'Datetime' Object to Seconds