Logical Operators
You need to specify a complex condition in an if statement.
Use one of the logical operators: and, or, and not.
Discussion
As an example, you may want to check whether a variable x has a value between 10 and 20. For that, you would use the and operator:
>>> x = 17 >>> if x >= 10 and x <= 20: … print(‘x is in the middle’) … x is in the middle |
You can combine as many and and or statements as you need and also use brackets to group them if the expressions become complicated.
See Also
See Recipes 5.18 and 6.15.