Tutorial by Examples

and

Evaluates to the second argument if and only if both of the arguments are truthy. Otherwise evaluates to the first falsey argument. x = True y = True z = x and y # z = True x = True y = False z = x and y # z = False x = False y = True z = x and y # z = False x = False y = False z =...

or

Evaluates to the first truthy argument if either one of the arguments is truthy. If both arguments are falsey, evaluates to the second argument. x = True y = True z = x or y # z = True x = True y = False z = x or y # z = True x = False y = True z = x or y # z = True x = False y = Fa...

not

It returns the opposite of the following statement: x = True y = not x # y = False x = False y = not x # y = True
Python minimally evaluates Boolean expressions. >>> def true_func(): ... print("true_func()") ... return True ... >>> def false_func(): ... print("false_func()") ... return False ... >>> true_func() or false_func() true_func(...
When you use or, it will either return the first value in the expression if it's true, else it will blindly return the second value. I.e. or is equivalent to: def or_(a, b): if a: return a else: return b For and, it will return its first value if it's false, else it r...
In Python you can compare a single element using two binary operators--one on either side: if 3.14 < x < 3.142: print("x is near pi") In many (most?) programming languages, this would be evaluated in a way contrary to regular math: (3.14 < x) < 3.142, but in Python it ...

Page 1 of 1