What is the output of this puzzle?
# Logic Statements A = True # Temperatures are high B = True # Arctic ice is melting C = True # Sea levels are rising D = False # All regions become hot def follows(A,B): """ Returns True if 'A -> B' in words: 'B follows from A' """ return not A or B # Do we have a climate change? climate_change = follows(follows(A,B),C) if not D: print(climate_change) else: print(D)
This puzzle shows the basic usage of boolean logic operators in Python. Boolean logic is crucial for coding. Try finding any real-world code project that does not use boolean logic. You will most certainly fail. Hence, to become a code master, you must master boolean logic first.
We define four variables with various boolean assignments.
Each boolean variable can be either ‘True’ or ‘False’.
Consider two logical variables A and B. You have to understand three concepts.
(i) The expression ‘A and B’ is True, if and only if both variables A and B are already True.
(ii) The expression ‘A or B’ is True, if and only if at least one variable is already True.
(iii) The expression ‘not A’ is True, if and only if A is False.
In the puzzle, we define our own function
follows that takes two arguments A and B and returns ‘A -> B’. In words: ‘The consequence B follows from the premise A’. You can deduct anything from a wrong premise. Thus, ‘A -> B’ is True if A is False. You can deduct only true statements from a true premise. Thus, ‘A -> B’ is True if B is True.
The result of our nested call of the
follows function is True: ‘(A -> B) -> C’ is True as all three variables are True. After passing the if condition, we state that climate change is indeed happening.
Are you a master coder?
Test your skills now!