Python Forum
Proper way to do the OR statement?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Proper way to do the OR statement?
#1
I've swear I've seen this before, but I can't remember what the problem is. This works:

if btstats.loc[i,'Status'] == 'WINNER' or btstats.loc[i,'Status'] == 'LOSER':
This only executes if 'WINNER':

if btstats.loc[i,'Status'] == ('WINNER' or 'LOSER'):
This only executes if 'LOSER':

if btstats.loc[i,'Status'] == ('LOSER' or 'WINNER'):
Neither of the last two give me what I want, but to me they all look like valid ways of composing an OR statement. What am I missing?
Reply
#2
Try printing things out to see what's going on. In this example,test == 'this' or test == 'that'will succeed if test is equal to either of those two. For the last two, just see what('Winner' or 'Loser')is equal to.
test = 'this'
if test == 'this' or test == 'that' :
	print ('this')
test = 'that'
if test == 'this' or test == 'that' :
	print ('that')

print ('Winner' or 'Loser')
print ('Loser' or 'Winner')
Output:
this that Winner Loser
Mark17 likes this post
Reply
#3
If you want to compare against multiple elements, use in and a container.
if btstats.loc[i,'Status'] in ('WINNER', 'LOSER'):
BashBedlam and Mark17 like this post
Reply
#4
A or B evaluates to A if A is "truey", else B. B is never evaluated

None, empty collections, empty strings and False are "falsey". Notice that in all these cases B (the right side expression) is printed.
print(None or [])
print("" or [])
print([] or None)
print(None or False)
Output:
[] [] None False
To demonstrate when something is evaluated for truey-ness I wrote a class with a __bool__() method. When Python sees an object with a dunder bool method it uses that method to evaluate the truey-ness of the object. My dunder bool method prints a message so I can see when it is called.
class X:
    def __bool__(self):
        print("I am False", end = "-")
        return False

x = X()
print("x or None", x or None)
print("None or x", None or x)
Output:
I am False-x or None None None or x <__main__.X object at 0x000002EE09878400>
From the output you can see that x.__bool__() called in the first example but not the second. The second example returns x as the result of the "or", but there is no need to evaluate the truey-ness of x.

Things change if we need to know if the "or" expression is truey of falsey. In this example the "or" statement returns x as before. but this time the "if" statement needs to know if x is truey or falsey to decide which branch of code to execute.
x = X()
if (None or x):
    print("Truey")
else:
    print("Falsey")
Output:
I am False-Falsey
So how does this apply to your examples?
if btstats.loc[i,'Status'] == 'WINNER' or btstats.loc[i,'Status'] == 'LOSER':
I am going to replace this with:
A = (btstats.loc[i,'Status'] == 'WINNER')
B = (btstats.loc[i,'Status'] == 'LOSER')
if A or B:
We know the or statement returns A if A is truey, else it returns B. We also know that the "if" statement is going to evaluate the result of the "or" to determine which branch of code to execute. If bstats.loc[i, 'Status'] is either 'WINNER" or 'LOSER' the code following the if statement os execited.
if btstats.loc[i,'Status'] == ('WINNER' or 'LOSER'):
I am going to replace this with:
A = 'WINNER'
B = 'LOSER'
if bstats.loc[i, 'Status'] == (A or B):
We know both A and B are truey. The only str that is falsey is an empty str (""). Since A or B returns A if A is truey, this code is the same as:
if bstats.loc[i, 'Status'] == 'WINNER:
That probably isn't what you wanted.
Mark17 likes this post
Reply
#5
Because this comes up often there is a stickied thread in Tutorials > Common pitfalls and what to do.
Multiple expressions with "or" keyword
bowlofred and Mark17 like this post
Reply
#6
This is really confusing to me (even the sticky). I think I'm going to focus on deanhystad's comment:

> A or B evaluates to A if A is "truey", else B. B is never evaluated

Also, bowlofred's suggestion:

> if btstats.loc[i,'Status'] in ('WINNER', 'LOSER'):

Also, I'll remember this solution, which also seems correct:

> if btstats.loc[i,'Status'] == 'WINNER' or btstats.loc[i,'Status'] == 'LOSER':

Thanks for the comments, everyone!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial issues with proper loops and binary jttolleson 16 2,588 Nov-02-2023, 08:39 PM
Last Post: deanhystad
  Getting proper x,y axis values pyhill00 8 1,635 Jul-29-2022, 06:48 PM
Last Post: pyhill00
  proper use of 'end' in a 'with' statement ccrider27 1 2,049 Mar-18-2020, 10:33 PM
Last Post: buran
  Proper use of if..elif..else statement nick1941 2 2,403 Mar-06-2020, 11:22 PM
Last Post: nick1941
  Proper Layout of Code Question TheJax 2 2,190 Feb-08-2020, 06:14 PM
Last Post: TheJax
  Unable to do the proper split using re.sub incase of missing data. Karz 1 1,854 Nov-17-2019, 05:58 PM
Last Post: buran
  getopt with tuple not working proper Frank123456 0 1,867 Aug-21-2019, 12:46 PM
Last Post: Frank123456
  proper syntax for itertuples? ilcaa72 1 1,995 Jun-06-2019, 02:41 AM
Last Post: scidam
  Need help excluding Named Entity (NE) and proper nouns (NNE) from text analysis disruptfwd8 0 2,342 May-15-2018, 12:10 AM
Last Post: disruptfwd8

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020