Logical Operators - Exercises
Contents
Logical Operators - Exercises#
We’ll run through how to demonstrate a mathematical logical proof “de Morgan’s” law, using logical operators. We’ll do this by asking you to write a number of functions to compute logical operations, and then explore this law, and how to prove it:
Exercise#
Write a function, logical_1 that takes two inputs P and Q and returns the logical operation
NOTE: We provide the solution here for you to see an example. You’ll then have to do this yourself in the next exercise!
def logical_1(P, Q):
return not (P or Q)
Test that your function works by calling it with the 4 possible inputs.
Again we give the solution:
As P and Q are boolean, they each have two possible values; True or False. So we can only call it as follows:
print(logical_1(True, False))
print(logical_1(True, True))
print(logical_1(False, False))
print(logical_1(False, True))
print("\n\nThis should have read\nFalse\nFalse\nTrue\nFalse")
False
False
True
False
This should have read
False
False
True
False
Exercise#
Write a function, logical_2 that takes two inputs P and Q and returns the logical operation
Test this for the possible values of input values, same as above!
Exercise#
One of de Morgan’s Laws states that
From your functions above, show that this is always true. HINT there is only 2 possible values of \(P\) and 2 possible values of \(Q\) so if this is True for all the 4 possible combinations, it is always true. The functions for the 2 exercises above should also be used directly here.
Exercise#
de Morgan’s other Law states that
Also demonstrate, following the same process as above, that this is always true.