Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This lab gives you further practice with the Python language on problems related to truth tables in propositional logic. Please complete the following exercises and
This lab gives you further practice with the Python language on problems related to truth tables in propositional logic. Please complete the following exercises and upload your py files under the appropriate CatCourses assignments Even for the simplest programs we write, we make use of code written by others. For this lab you are provided with code that generates truth tables, which you can use for other exercises. To start, download the file logic.py from CatCourses and place it in your working directory. Among other things, the file defines a TruthTable object. Start a new project and import the TruthTable object from logic.py, using the following command: from logic import TruthTable You can now generate truth tables for any proposition. All you need to specify is the list of variables that appear in your propositions, and the propositions themselves Example Generate a truth table for the proposition p vq. Solution We first note that there are two variables, p and q, which will be represented in Python as the list ['p, 'q']. We also need to represent the proposition itself, which is the Python string 'p or q. Since the TruthTable object can generate a truth table with multiple expressions, we will provide the proposition as a list with a single element in it, ['p or q']. We are now ready to generate the truth table: myTable - TruthTableC['p'. 'q']. ['p or q']) We can now display the truth table generated above: nyTable display O The command above produces the following text: P or q 0 0 You can also generate IATEX code for representing the table: myTable.latex) The above command produces the following text: beginftabularHIclclcl hline hline 0 & 1 & 1 1 1 &0 & 1 1 1 &1 1 1 hline endtabular) The TruthTable object can also be called with multiple propositions. myTableTruthTable (['p', 'q'], ['p or q', 'p and q']) The command above generates one truth table with both propositions side by side: p orq p and q 0 0 0 0 0 When using TruthTable, we need to specify all propositions and propositional variables as Python strings, as illustrated in the examples here. The TruthTable object supports the following logical connectives: or (v), and (A),-(-),-) (-), (). Equivalence in Propositional Logioc As discussed in class, one way to prove whether two propositions are equivalent, is to generate truth tables for both propositions and check to see if all the rows match, in which case the otherwise they are not. are equivalent, In this exercise you are required to write a Python program that asks the user to enter two propositions, where both propositions the variables p and q. Your program s should then determine whether or not the two propositions are equivalent, and report the result | Here is an example of how a user might interact with your program Enter proposition 1: p and q En ter proposition 2: q and The propositions aro oquivalent In order to complete this exercise, you should study the structure of the table property of TruthTablo You can access it by saying print ayTable.table For example, here is the tablo of the proposition pA q CCCO, 0, [0, C0, 1, [0]], C01, ], 0 1, 1], [1]1] As you can see, it is one big list, made up of exactly 4 smaller lists. Each one of the smaller lists represents a row of the truth table. Each row in turn is made up of exactly 2 lists. The first one is a list of truth values of the propositional variables, and the second one is the overall truth value of the proposition(s), given the truth values in the last list If we take the above table and consider the first row, that is the list [rO, 0, [011. Bearing in mind that this is for the formula pA q, the way to interpret this list is that when the variables p and q are 0, 01, respectively, then the proposition pAq has a truth value of [0, or simply O. Remember, this is represented as a list with a single element in it because the truth table may have more than one proposition being This brings us to the following example. Let us generate a single truth table for both pAq andp. Now we have the following table: CECO, 01, 0, 01], [I0, 11, [0, 01], [01, 0], [0, 0]], C01, 1], , 1111 It is still made up of 4 lists, each of which represents a row of the truth table. Each row is still made up of two lists, as before. The first of these lists is still a specific combination of truth values, and the second list is the overall truth value of each proposition. Since we generated a truth table for 2 propositions, the list has 2 results in it. This time, taking the second row, which is C0, 1, [0, 011, we can see that when p-0 and q = l then p^ q evaluates to false, and qAp also evaluates to false, hence both 0 values in the list. Examining the whole table, we can see that the two propositions are indeed equivalent because, for every row, the result part always contains the same two values, meaning that for any possible combination of truth values, both propositions evaluate to the same result. This lab gives you further practice with the Python language on problems related to truth tables in propositional logic. Please complete the following exercises and upload your py files under the appropriate CatCourses assignments Even for the simplest programs we write, we make use of code written by others. For this lab you are provided with code that generates truth tables, which you can use for other exercises. To start, download the file logic.py from CatCourses and place it in your working directory. Among other things, the file defines a TruthTable object. Start a new project and import the TruthTable object from logic.py, using the following command: from logic import TruthTable You can now generate truth tables for any proposition. All you need to specify is the list of variables that appear in your propositions, and the propositions themselves Example Generate a truth table for the proposition p vq. Solution We first note that there are two variables, p and q, which will be represented in Python as the list ['p, 'q']. We also need to represent the proposition itself, which is the Python string 'p or q. Since the TruthTable object can generate a truth table with multiple expressions, we will provide the proposition as a list with a single element in it, ['p or q']. We are now ready to generate the truth table: myTable - TruthTableC['p'. 'q']. ['p or q']) We can now display the truth table generated above: nyTable display O The command above produces the following text: P or q 0 0 You can also generate IATEX code for representing the table: myTable.latex) The above command produces the following text: beginftabularHIclclcl hline hline 0 & 1 & 1 1 1 &0 & 1 1 1 &1 1 1 hline endtabular) The TruthTable object can also be called with multiple propositions. myTableTruthTable (['p', 'q'], ['p or q', 'p and q']) The command above generates one truth table with both propositions side by side: p orq p and q 0 0 0 0 0 When using TruthTable, we need to specify all propositions and propositional variables as Python strings, as illustrated in the examples here. The TruthTable object supports the following logical connectives: or (v), and (A),-(-),-) (-), (). Equivalence in Propositional Logioc As discussed in class, one way to prove whether two propositions are equivalent, is to generate truth tables for both propositions and check to see if all the rows match, in which case the otherwise they are not. are equivalent, In this exercise you are required to write a Python program that asks the user to enter two propositions, where both propositions the variables p and q. Your program s should then determine whether or not the two propositions are equivalent, and report the result | Here is an example of how a user might interact with your program Enter proposition 1: p and q En ter proposition 2: q and The propositions aro oquivalent In order to complete this exercise, you should study the structure of the table property of TruthTablo You can access it by saying print ayTable.table For example, here is the tablo of the proposition pA q CCCO, 0, [0, C0, 1, [0]], C01, ], 0 1, 1], [1]1] As you can see, it is one big list, made up of exactly 4 smaller lists. Each one of the smaller lists represents a row of the truth table. Each row in turn is made up of exactly 2 lists. The first one is a list of truth values of the propositional variables, and the second one is the overall truth value of the proposition(s), given the truth values in the last list If we take the above table and consider the first row, that is the list [rO, 0, [011. Bearing in mind that this is for the formula pA q, the way to interpret this list is that when the variables p and q are 0, 01, respectively, then the proposition pAq has a truth value of [0, or simply O. Remember, this is represented as a list with a single element in it because the truth table may have more than one proposition being This brings us to the following example. Let us generate a single truth table for both pAq andp. Now we have the following table: CECO, 01, 0, 01], [I0, 11, [0, 01], [01, 0], [0, 0]], C01, 1], , 1111 It is still made up of 4 lists, each of which represents a row of the truth table. Each row is still made up of two lists, as before. The first of these lists is still a specific combination of truth values, and the second list is the overall truth value of each proposition. Since we generated a truth table for 2 propositions, the list has 2 results in it. This time, taking the second row, which is C0, 1, [0, 011, we can see that when p-0 and q = l then p^ q evaluates to false, and qAp also evaluates to false, hence both 0 values in the list. Examining the whole table, we can see that the two propositions are indeed equivalent because, for every row, the result part always contains the same two values, meaning that for any possible combination of truth values, both propositions evaluate to the same result
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started