''' Purpose: introduce the logical operators ''' print() print( '##### Logical values (bool)' ) print() # boolean values: only 2 values # True, False # must be capitalized # no quotes print( True ) print( False ) print(); input( 'Enter when ready: ' ); print() print( '##### Logical and' ) # beware of single = and double == !!!!!!!!!!!!!!!!!!!!!! # = is the assignment operator where you assign the value on the right side to the thing on the left side # what we've been doing # some_variable = some_value # == is testing whether two things are equal # 3 == 2 --> return a boolean value whether this relationship is equal --> this will be False # 3 == 3 --> True a = True b = False print() # and is another logical operator --> python will evaluate both sides of and then return a boolean value # whether this relationship is True or False # remember this table ''' and | T | F -------------- T | T | F -------------- F | F | F BOTH SIDES need to be True for the result to be True If either side of the operator is False, then the result will ALWAYS be False If BOTH sides are False, then the result will ALWAYS be False If it rains AND it's wet outside, then I wear my rain boots # both conditions need to be true before I wear my rain boots # it needs to rain AND it needs to be wet outside for me to wear my rain boots ''' 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() # or is another logical operator ''' or | T | F <-- operand 2 ----------- T | T | T ----------- F | T | F ^ | operand 1 or only needs ONE side to be True for the result to ALWAYS be True if both sides are True, result is True if both sides are False, then the result is always False If I get an A on the test OR I do my chores, then my mom gives me money # only one of these things needs to be True for my mom to give me money # EITHER I get an A, OR I do my chores for my mom to give me money # OR I do BOTH: I get an A and I do my chores, then my mom will still give me money # If I don't do any of these things (makes the If statement False), then my mom will not give me money (and yell at me) ''' 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 )