Lab 2 - Logic of Compound Statements
Lab 2 - Logic of Compound Statements
2. Logical Connectives
2.1. Conjunction, Disjunction, Negation
We use logical operators like AND, OR, and NOT to combine or modify propositions.
• AND (Conjunction): p and q is true if both p and q are true.
• OR (Disjunction): p or q is true if at least one of p or q is true.
• NOT (Negation): not p is true if p is false.
# OR operation (Disjunction)
p_or_q = p or q
print(f"p OR q: {p_or_q}")
1
TON DUC THANG UNIVERSITY
Faculty of Information Technology
3. Truth Tables
A truth table shows the truth values of propositions for all possible combinations. Here’s
an example of generating a truth table for p, q, p AND q, p OR q, and p → q:
# List of all possible truth values for p and q
propositions = [(True, True), (True, False), (False, True), (False, False)]
for p, q in propositions:
p_and_q = p and q
p_or_q = p or q
implication = not p or q
print(f"{p:<5} | {q:<5} | {p_and_q:<8} | {p_or_q:<8} | {implication}")
Output:
p | q | p AND q | p OR q | p → q
--------------------------------------------
True | True | True | True | True
True | False | False | True | False
False | True | False | True | True
False | False | False | False | True
2
TON DUC THANG UNIVERSITY
Faculty of Information Technology
5. Exercises
Exercise 1: Evaluation
Create a Python function that checks if the following expressions hold true for given
values of p, q and r. Return True if the implication holds, otherwise return False.
1. p→q
2. ~p ^ q
3. p ^ (q ^ r)
4. p ^ (~q v r)
3
TON DUC THANG UNIVERSITY
Faculty of Information Technology