Why does checking a variable against multiple values with `OR` only check the first value?
("Jesse" or "jesse")
The above expression tests whether or not "Jesse"
evaluates to True
. If it does, then the expression will return it; otherwise, it will return "jesse"
. The expression is equivalent to writing:
"Jesse" if "Jesse" else "jesse"
Because "Jesse"
is a non-empty string though, it will always evaluate to True
and thus be returned:
>>> bool("Jesse") # Non-empty strings evaluate to True in Python
True
>>> bool("") # Empty strings evaluate to False
False
>>>
>>> ("Jesse" or "jesse")
'Jesse'
>>> ("" or "jesse")
'jesse'
>>>
This means that the expression:
name == ("Jesse" or "jesse")
is basically equivalent to writing this:
name == "Jesse"
In order to fix your problem, you can use the in
operator:
# Test whether the value of name can be found in the tuple ("Jesse", "jesse")
if name in ("Jesse", "jesse"):
Or, you can lowercase the value of name
with str.lower
and then compare it to "jesse"
directly:
# This will also handle inputs such as "JeSSe", "jESSE", "JESSE", etc.
if name.lower() == "jesse":
How to test multiple variables for equality against a single value?
You misunderstand how boolean expressions work; they don't work like an English sentence and guess that you are talking about the same comparison for all names here. You are looking for:
if x == 1 or y == 1 or z == 1:
x
and y
are otherwise evaluated on their own (False
if 0
, True
otherwise).
You can shorten that using a containment test against a tuple:
if 1 in (x, y, z):
or better still:
if 1 in {x, y, z}:
using a set
to take advantage of the constant-cost membership test (i.e. in
takes a fixed amount of time whatever the left-hand operand is).
Explanation
When you use or
, python sees each side of the operator as separate expressions. The expression x or y == 1
is treated as first a boolean test for x
, then if that is False, the expression y == 1
is tested.
This is due to operator precedence. The or
operator has a lower precedence than the ==
test, so the latter is evaluated first.
However, even if this were not the case, and the expression x or y or z == 1
was actually interpreted as (x or y or z) == 1
instead, this would still not do what you expect it to do.
x or y or z
would evaluate to the first argument that is 'truthy', e.g. not False
, numeric 0 or empty (see boolean expressions for details on what Python considers false in a boolean context).
So for the values x = 2; y = 1; z = 0
, x or y or z
would resolve to 2
, because that is the first true-like value in the arguments. Then 2 == 1
would be False
, even though y == 1
would be True
.
The same would apply to the inverse; testing multiple values against a single variable; x == 1 or 2 or 3
would fail for the same reasons. Use x == 1 or x == 2 or x == 3
or x in {1, 2, 3}
.
Concise way to compare a variable against multiple values
You can use in
operator:
for i in range(10):
if i in (3, 5) or math.sqrt(i) in (3, 5):
numbers.append(i)
or in case you expect each of the calculations to be in the same group of results, you can use any()
results = [1, 2, ..., too long list for single line]
expected = (3, 5)
if any([result in expected for result in results]):
print("Found!")
Just a minor nitpick, sqrt
will most likely return a float
sooner or later and this approach will be silly in the future, therefore math.isclose()
or others will help you not to encounter float "bugs" such as:
2.99999999999999 in (3, 5) # False
which will cause your condition to fail.
PHP check value against multiple values with OR-operator
The logical ||
(OR) operator doesn't work as you expect it to work. The ||
operator always evaluates to a boolean either TRUE or FALSE. So in your example your strings get converted into booleans and then compared.
If statement:
if($ext == ('txt' || 'rtf'|| 'log' || 'docx'))
Comes down to:
if($ext == (TRUE || TRUE || TRUE || TRUE))
if($ext == TRUE)
To solve this problem and get the code to work as you want it to you can use different methods.
Multiple comparison
One way to solve the problem and check your values against multiple values is, to actually compare the value against multiple values:
if($ext == "txt" || $ext == "rtf" /* || ... */)
in_array()
Another way is to use the function in_array()
and check if the value is equal to one of the array values:
if(in_array($ext, ["txt", "rtf" /* , ... */], TRUE))
Note: Second parameter is for strict comparison
switch()
You could also use switch
to check your value against multiple values and just let the case fall through.
switch($ext){
case "txt":
case "rtf":
/* case ...: */
$pClass = "text-";
break;
}
Is there a simpler way to check multiple values against one value in an if-statement?
Unfortunately there is no such construct in Java.
It this kind of comparison is frequent in your code, you can implement a small function that will perform the check for you:
public boolean oneOfEquals(int a, int b, int expected) {
return (a == expected) || (b == expected);
}
Then you could use it like this:
if(oneOfEquals(a, b, 0)) {
// ...
}
If you don't want to restrict yourselft to integers, you can make the above function generic:
public <T> boolean oneOfEquals(T a, T b, T expected) {
return a.equals(expected) || b.equals(expected);
}
Note that in this case Java runtime will perform automatic boxing and unboxing for primitive types (like int
), which is a performance loss.
What's the prettiest way to compare one value against multiple values?
Don't try to be too sneaky, especially when it needlessly affects performance.
If you really have a whole heap of comparisons to do, just format it nicely.
if (foobar === foo ||
foobar === bar ||
foobar === baz ||
foobar === pew) {
//do something
}
Check variable equality against a list of values
Using the answers provided, I ended up with the following:
Object.prototype.in = function() {
for(var i=0; i<arguments.length; i++)
if(arguments[i] == this) return true;
return false;
}
It can be called like:
if(foo.in(1, 3, 12)) {
// ...
}
Edit: I came across this 'trick' lately which is useful if the values are strings and do not contain special characters. For special characters is becomes ugly due to escaping and is also more error-prone due to that.
/foo|bar|something/.test(str);
To be more precise, this will check the exact string, but then again is more complicated for a simple equality test:
/^(foo|bar|something)$/.test(str);
Most efficient way to compare a variable to multiple values?
If the values you want to check are sufficiently small, you could create a bit mask of the values that you seek and then check for that bit to be set.
Suppose, you care about a couple of groups.
static const unsigned values_group_1 = (1 << 1) | (1 << 2) | (1 << 3);
static const unsigned values_group_2 = (1 << 4) | (1 << 5) | (1 << 6);
static const unsigned values_group_3 = (1 << 7) | (1 << 8) | (1 << 9);
if ((1 << value_to_check) & values_group_1) {
// You found a match for group 1
}
if ((1 << value_to_check) & values_group_2) {
// You found a match for group 2
}
if ((1 << value_to_check) & values_group_3) {
// You found a match for group 3
}
This approach works best for values that don't exceed the natural size your CPU likes to work with. This would typically be 64 in modern times, but may vary depending upon the specifics of your environment.
Related Topics
How to Use Stringio in Python3
How to Open a File Using the Open with Statement
How to Time a Code Segment for Testing Performance with Pythons Timeit
Replace Console Output in Python
Why Do You Need Explicitly Have the "Self" Argument in a Python Method
Using Os.Walk() to Recursively Traverse Directories in Python
"Python" Not Recognized as a Command
Making Object JSON Serializable with Regular Encoder
Single VS Double Quotes in JSON
Python Max Function Using 'Key' and Lambda Expression
Very Large Matrices Using Python and Numpy
Differencebetween a Function, an Unbound Method and a Bound Method
How Slow Is Python's String Concatenation VS. Str.Join