It's valid Python, so it might be correct. You can verify if the code is correct by trying some test cases:
def check(mStrip):
result = 'AH' in mStrip or 'MH' in mStrip or 'GL' in mStrip and not 'ML' in mStrip
print(mStrip, 'is', result)
check('AH')
check('ML')
check('AH ML')
check('GL ML')
check('XL'))
Output:
AH is True
ML is False
AH ML is True
GL ML is False
XL is False
The interesting results are that check('AH ML') is True but check('GL ML') is FALSE. I don't think that is what you want.
From the manuals:
https://docs.python.org/3/reference/expr...tion-order
Quote:The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
In the case of 'AH ML' we start with 'AH' in mStrip which is True. This is the "x" value from the description above. If x is true, it's value is returned.
The 'GL ML' case is a bit more interesting. If we evaluate each individual condition and replace them with True or False we get: False or False or True and False. Breaking this into "x" and "y" we get:
x = False or y = False or True and False
x is False, so we don't return immediately and must evaluate Y. Y is a compound statement so we break it into x and y again: x = False or y = True and False.
Once again x is False, so we have to evaluate y. x = True and y = False.
This time we have "and" instead of "or". If x is false, "and" returns false, else it evaluates y and returns y. In our case x is True, so we have to evaluate y and return that. y is False, so the "and" statement returns False.
Stringing a bunch of "and"s and "or"s is complicated and best avoided. It is also lengthy to type and difficult to read.
def check(mStrip):
result = 'ML' not in mStrip and any(x in mStrip for x in ['AH', 'MH', 'GL'])
print(mStrip, 'is', result)
check('AH')
check('ML')
check('AH ML')
check('GL ML')
check('XL'))
Output:
AH is True
ML is False
AH ML is False
GL ML is False
XL is False
I think this code produces the results you want. To be True mStrip cannot contain ML and it must contain AH or MH or GL.
I could have used "not 'ML' in mStrip", but I think " 'ML' not in mStrip" reads more naturally.
This is a "comprehension":
any(x in mStrip for x in ['AH', 'MH', 'GL'])
This part:
x in mStrip for x in ['AH', 'MH', 'GL']
Creates a list that is essentially ['AH' in mStrip, 'MH' in mStrip, 'GL' in mStrip]. The any() function returns True if any of the conditions is True.