Skip to content Skip to sidebar Skip to footer

How Do Boolean Operators Work In 'if' Conditions?

I am currently new to Python and am trying to run a few simple lines of code. I cannot understand how Python is evaluating this syntax after the if statement. Any explanations will

Solution 1:

You probably want:

if number in(1, 2, 3):

Solution 2:

Python has boolean values, such as True and False, and it also has falsy values, such as any empty list, tuple, or dictionary, an empty string, 0, and None. Truthy values are the opposite of that, namely anything that's defined.

Python's orevaluates and short-circuts on the first element that returns a truthy value.

So, the expression (1 or 2 or 3) is going to return 1.

If you want to compare against all elements, then you're looking for the in keyword:

if number in (1, 2, 3):
    # Do logic

Post a Comment for "How Do Boolean Operators Work In 'if' Conditions?"