3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) [Clang 6.0 (clang-600.0.57)] Python Type "help", "copyright", "credits" or "license" for more information. # Booleans # Boolean data can only take on one of two values: True and False True True False False type(True) # These two are reserved keywords in Python..no assigning variables with these # names! True = "hi" Traceback (most recent call last): Python Shell, prompt 7, line 1 Syntax Error: can't assign to keyword: , line 1, pos 0 type(True), type(False) # Boolean values are designed to represent the "truth values" of statements # in Python # one way to get a truth value: comparison operators 5 > 4 True 12 < 3 False a = 12 a < 3 False 13 - a >= 0 True # interval comparison 3 < 5 < 7 True 2 <= 2 < 3 True 1 < 7 < 3 False 1 < 7 > 3 True # We can check equality, if two values are 'equal' to each other a 12 a == 12 True a == 17 - 1 False # note the difference between == and = # == is the equality operator # = is the assignment operator # Comparing different types: 7 == 7 True 7 == 7.0 True '7' == 7.0 False '7.0' == 7.0 False '7'== 7 False '7.0' != 7.0 True "a" < "b" True # Alphabetical order: earlier letters are 'smaller' "A" < "b" True "B" < "a" True "#" < "*" True # Boolean/logical operators # Boolean (or logical) operators operate on boolean values, just like arithmetic operators # operate on numeric values # arithmetic: 2 + 3, where '+' is the operator # boolean: True and False, where 'and' is the boolean operator # 'Boolean' and 'Logical' values are the same thing # and: t1 = True t2 = True # Given two boolean values, t1, t2, 't1 and t2' gives back # True if and only if (iff) t1 and t2 are both True t1 and t2 True t2 = False t1 and t2 False # or: # Given two boolean values, t1, t2, 't1 or t2' gives back True iff # *at least one* of t1 or t2 is True t1 = True t2 = True t1 or t2 True t2 = False t1 or t2 True t1 True t1 = False t1 or t2 False # not # Given one boolean value, t, 'not t' returns True iff t is False, and vice versa t1 False not t1 True t2 = True not t2 False not True or False False not (True or False) False # not has a higher precedance than and / or not False and False False (not False) and False False not (False and False) True not False and False False # It is possible to convert other types to bools bool(0) False bool(1) True bool(2) True bool("hello") True bool("") False bool(0.0) False bool(-1) True # For numbers, everything except 0 gets converted to True # For strings, everything except "" gets converted to True # These conversions are not used too often in practise, but good to know # Short-circuiting False and 4 / 0 False 4 / 0 and False Traceback (most recent call last): Python Shell, prompt 92, line 1 builtins.ZeroDivisionError: division by zero # 'True and 4/ 0' would also give an error since the and has to check if second # one is true bool(4 / 0) Traceback (most recent call last): Python Shell, prompt 93, line 1 builtins.ZeroDivisionError: division by zero # some more common errors: a = 3 b = 6 # How do we check that "a and b are both greater than 5"? a and b > 5 # do not do this True (a > 5) and (b > 5) # use brackets, and separate the comparisons! False # Just for fun: True > False True True < False False