Search code examples
pythonpython-3.xif-statementbooleanboolean-logic

Is it possible to test a bool against 2 variables?


I'm trying to do something like this:

if (a == ("A" or "U")):
 do something

but it only tests against "A" when I introduce the input in the console

but it works if I do this:

if (a == "A" or a == "U")):
 do something

is it possible to achieve the second with a simpler line like the first one? thanks!


Solution

  • You can't check both the conditions using (a == ("A" or "U")) because when you execute "A" or "U" in python interpreter you will get "A" (the first truthy value) similarly when you execute "A" and "U" you will get "U" (the last truthy value).

    If you want simplified expression, you can use,

    if a in ("A", "U"):
        # TODO: perform a task