''' Purpose: introduce the logical operators ''' print() print( '##### Logical values (bool)' ) print() # True and False are boolean values (a boolean is a new type in Python) # The same way lists, strings, ints are types, so are booleans. print( True ) print( False ) print(); input( 'Enter when ready: ' ); print() print( '##### Logical and' ) a = True b = False print() and1 = ( a and a ) and2 = ( a and b ) and3 = ( b and a ) and4 = ( b and b ) print( a, 'and', a, '=', and1 ) print( a, 'and', b, '=', and2 ) print( b, 'and', a, '=', and3 ) print( b, 'and', b, '=', and4 ) print(); input( 'Enter when ready: ' ); print() print() print( '##### Logical or' ) print() or1 = ( a or a ) or2 = ( a or b ) or3 = ( b or a ) or4 = ( b or b ) print( a, 'or', a, '=', or1 ) print( a, 'or', b, '=', or2 ) print( b, 'or', a, '=', or3 ) print( b, 'or', b, '=', or4 ) print(); input( 'Enter when ready: ' ); print() print( '##### Logical negation' ) print() negate1 = ( not a ) negate2 = ( not b ) print( 'not', a, '=', negate1 ) print( 'not', b, '=', negate2 ) print(); input( 'Enter when ready: ' ); print() print( '##### Logical inclusion' ) print() x = [ 3, 1, 4, 1, 5 ] i = 1 j = 2 has1 = ( i in x ) has2 = ( j in x ) print( i, 'in', x, '=', has1 ) print( j, 'in', x, '=', has2 ) print(); input( 'Enter when ready: ' ); print() print( '##### Logical exclusion' ) print() x = [ 3, 1, 4, 1, 5 ] i = 1 j = 2 doesnot1 = ( i not in x ) doesnot2 = ( j not in x ) print( i, 'not in', x, '=', doesnot1 ) print( j, 'not in', x, '=', doesnot2 ) # Logical Operators # and - both things must be true to be true (otherwise result is false) # or - both things must be false to be false (otherwise result is true) # not - opposite of true/false result # in - see if something is in a sequence (like a character in a string / element in a list) # not in - see if something isn't in a sequence (like a character in a string / element in a list) # Relational operators # > greater than # < less than # >= greater than or equal to # <= less than or equal to # == equal to # != not equal to (different)