How To Construct Truth Tables Using Python

Video Series: Logic and Mathematical Proof (In-Depth Tutorials for Beginners)

(D4M) — Here is the video transcript for this video.

[Music] hi everyone welcome back in this episode we’re going to learn how
to construct truth tables truth tables using python now before we get started i
wanted to mention that this video is part of the series logic and
mathematical proofs in-depth tutorials for beginners
so let’s go ahead and get started in today’s episode we’re going to cover um
propositional variables and then we’re going to talk about connectives and
building truth tables and uh tautologies and contrapositive now
you know if you’ve been watching the series we’ve already covered each of
those topics and you know we did so uh mathematically and
we didn’t uh do anything uh on computer so today we’re going to do all this
again but we’re going to see how easy it is to do using a

programming language like python and at the end we’re going to also talk about
logical equivalence so let’s go ahead and get started
and to get started now we’re going to first talk about
how to declare variables uh using uh this python
code that we have and so let’s go ahead and get started on that so
what i’m going to do is i’m going to bring in this python notebook right here
and so first off let’s see how to set this up
and so we’re going to need to execute this cell right here import logic using
this import command right here now if you’re not sure how to uh follow along
with this python um you’ve never set it up before well rest assured
there are thousands of videos out there on how to set up python to extremely
popular programming language you can set it up on your computer on
your machine whether you’re using windows or mac

or you can just use uh google collab which is a free notebook
and you can get started i’m using anaconda on my mac
and so we can we can just follow along if you if you don’t even want to get get
all that set up but we’ll see how easy it is so this is
also a good review in terms of what we’ve already covered all right so to
execute that cell i’m going to hit shift enter
and now i’m going to set up some declare
some variables so here’s some cells here we can execute so this cell right here
is going to declare p q r s and t as variables so
hit shift enter to execute that now just to double check that
this is actually working i’m going to just type in p and q and execute that
and so it works it tells me that p is a proposition and that user proposition we
could have checked all five but you know just to you know see what it looks like
to make sure that um the notebook is recognizing

um this package right here this logic code right here
all right so now what i’m going to do is actually we can also
instead of just using pq you know these letters here we can also enumerate
um and so you know if i execute that cell right here
so now we have set up variables p1 p2 and so on
and you can set up as many as you like however keep in mind that this code may
not scale to obviously hundreds of variables
that’s an interesting question i haven’t
tested out how many very how far you can go with it but for this um video here
we’re just going to work with five variables or or less so we can
execute the cell right here p1 it recognizes that p1 is a propositional
variable here all right so what connectives are we going to cover um in
previous videos we covered several seven different types of connectives
so in this video here we’re just going to talk about these connectives here so

we’re going to have the negation which we defined in the previous video the or
the and the implication and the biconditional right here so these are
the same truth tables that we talked about before
um these are the same definitions we talked about before now to do them in
python well this symbol right here which
we used in a previous video the negation of p
in python we’re going to use this symbol right here
so let’s execute this cell to see it so that means not p it recognizes that
not p is a proposition now this is really nice and fun if you
want to print out a truth table we’re going to use this method right here
print underscore truth underscore table parentheses let’s execute that and you
see we get this true table that we get over here now let’s do the or
so the or right here mathematically or if we want to type it up on computer
we’ll just use this vertical bar here execute that cell
let’s go ahead and print out a true table for the or
there we go so it matches the same column right here

and let’s do the same for the and so we have p and q here we’re using the
ampersand for an and or mathematically we use the
uh this wedge symbol here so here we go we’re going to use p and q
and then let’s print out the truth table for it very good so implication
so we’ll use this one we’ll use the symbol right here twice so execute
shift enter for execute and then we’ll go ahead and print the truth table out
here and this is for implication as you can see here we get the same table that
we have here for the definition of implication
and then now to do uh the biconditional we can do p implies q and q implies p
and to see that that’s the same that we usually know is uh true false false true
now a caveat here make sure you have parentheses around the whole expression

right here so i have these outer parenthesis and then the dot all right
or if you want we can use this um here we can say p dot
if and only if that’s the same thing as the
by conditional here so if and only if and then q with parentheses in it
so there’s another way to denote the if and only if right there
so just to check that out let’s also print out the true table for that
as you can see we get the same true table either way
so this represents the p if and only if q right here
all right so there’s the connectives that we’re going to work with
and so now let’s do some um truth table examples here
so uh how to construct a true table well
we’re going to use the print truth table which we’ve been doing so far
we’re going to do some more examples now so now i’m going to um declare some
formulas here so i’m going to just type in the word

formula equals and then i’m going to type in this formula right here using
all the connectives that we just discussed and so let’s shift enter that let’s
execute that and then i’ll check that it’s a proposition
so we have this right here proposition and then we have you know the p or q and
the negation of p and uh all that implies q
all right so good so let’s execute that and now let’s go ahead and print out a
truth table for it so it says construct a true table for
this statement right here and that’s exactly what we have right
over here as we can see the column is true true true and true now
that’s a really nice quick way of finding the truth table out on the other hand
typically if you want to find the truth table for this

for example if you want to do this by hand what you would often do is you would
have something like like let’s see here we would have something like
p and then a q and then we would have p or q and then
you know we might even put not p first and then p or q
and then we would have the and between those so p and p or q and then the and
and then the not p we would have that column
and then we would have the column for the whole formula
so we would make all these columns here one two three four columns
and also with the p and q so six columns total so we could fill all that out and

what this code right here doing is just giving us the last column though it’s
skipping these three columns right here and it’s not finding them now we can
we can find these three columns here by doing the following method though so
in case you want to do that you don’t have to but we could say
here’s our first formula f1 which i’m going to say is not p
and then f2 is p or q which is this part right here
and then we can do the and between them so f2 and f1
and then we can do the implication so this is f3 which is all of this right here
implies the q um and so that would be the f4 statement there
so let’s go ahead and execute that so we’ve just you know declared four
formulas right there and we can check that it understood that

just execute that cell right there it jumped on me but here it is right here
and then we can print truth tables out for each and every one of those so i’m
going to print a truth table out of course f4 is the one that we’re
interested in so let’s just print those true tables
out here’s the truth table for f1 for f2 and so that would give us the columns
the missing columns if we wanted to go find those
and then here’s the final last one here is the column right here
you know the true table for or the true values for the whole statement right
there so let’s do that again now but this time
let’s look at one here that has five variables um so we have five variables here
we’re going to be looking at um this you know now we need five formulas we’re
gonna say g one g two g three g four g five and so let’s build this up
here so here’s g one right here here’s g two right here s and t

and then we have the uh g three would be the q or r and then we have the g4
which is supplies q and then we can piece them all together
so we have g1 implies g2 and and then we have g3 implies g4 and so then we can
just list them all right there let’s go ahead and execute this cell right here
so what we just declared is let’s go ahead and execute this cell right here
um it kind of jumps on me when i do that but when i execute the cell right here
it’s going to get this out right here which means it recognizes this all of
these right here is propositions if you don’t want to do that you could just hit
like that right there and then it just kind of doesn’t even give you any output
in any case once we once we define these variables here now we can

um print a true table out for the one that we’re interested in g5 which is the
whole statement here and when we execute that then we’re going to get this
truth table here so let’s see if we can bring that into focus here so here we go
so there are the five variables right there and then here’s the whole
statement right here and there’s all the truth values for it so
very nice and quick and executes it very quickly all right so now let’s look at
tautologies how do we check for tautologies here so
you know let’s check that f4 is a tautology so remember f4 was the um
the statement in the last one right here so f4 was this one right here
and how do we check if this is a tautology right here so there’s a method
called is tautology and so that’s we’re saying right here is
to check for tautology all you got to do is check this method right here is

tautology so f4 our formula our fourth formula is the tautology and it turns
out to be true and we saw that above because
the table for f4 if you had noticed um right here is all true so it’s a
tautology right here but you don’t have to make out the whole
truth table you could just run this method right here and it’ll tell you yes
all the true right here means it is a tautology which means all the columns
are in the last column all the values are true
and what about g5 well g5 was this statement right here and as you can see
it’s not a tautology there are some falses here so this returns right here
false this right here is not a tautology so let’s go ahead and execute those two
right there and then now if f4 is a tautology that it’s negation
should be a contradiction so if i negate f4 and remember these parentheses right

here are important and so i can check right here is negation and then it is
contradiction and then that’s true and then one last thing is
it’s not a tautology well if it’s false that could mean it’s a contradiction or
it’s a contingency so in fact there’s actually a method to do to check if it’s
a contingency so executing that you should get true
all right so now let’s go on to contrapositive and the converse here and
so when we do that what we’re going to see is that there’s actually a way to do
or to switch uh the arrows here so this right here is the proposition that q
implies p so this is double left as opposed to
double right so if we execute that then it recognizes that q implies p
and we can print the true table for this right here
so this is q implies p so true implies true is true

and these falses right here um both lead to truths and then true implies a false
is a false okay so that will give us the converse and then
now what about the contrapositive and the converse well let’s just do this
example over here so we’re going to construct choose table
truth tables for this whole statement and it’s converse and it’s contra
positive now there is a converse in contrapositive because the whole
statement here is an implication so i have all of this right here implies all
of this right here so it’s an implication so we can find the converse and
contrapositive of that so i’m going to build it up into pieces i’m going to say
h1 is the not p and then we have the s and t
and then we have the uh h1 and h2 here so that’s h3 right here h1 implies h2 so
this whole side right here is h3 and so let’s just you know put that down

right here this right here is h3 and so all of that
and then we have q or r so that right there is h4 and then we have
s or q which is h5 and so then we have h4 and h5 so we have
here h4 and h5 and then all of that right there is
um h6 right there so that that right there is h6
so we have an implication in between those so that’s the statement our given
statement is h3 implies h6 and our converse is
um h3 and then we’re going to reverse this so we’re using a
converse right here so h3 and h6 and then we have the contrapositive so
remember the contrapositive is the negation here and then implies and then
the negation here so for this given implication right here
the statement we have the converse and contrapositive

so i’m going to go ahead and execute this cell right here and
what that did is executing this cell right here it recognized all these
propositions here that were working with and we have our statement our converse
and our contrapositive here and then now let’s go ahead and make the
truth table out for this original statement right here this whole
statement right here with five variables let’s go ahead and print out the truth
table and as you can see it does it really
fast here let’s move me out of the way and so as you can see right here here’s
the whole h three implies h6 there’s the original statement right there
and there’s all the truth values right there
and the con converse statement which we defined right here let’s go ahead and
print that truth table out right here there we go there’s the converse right
there and here is the contrapositive right here
so it prints out that contrapositive right there

and there’s all the truth values for it and in fact if you look at these right
here it looks very familiar if you had looked through this one right
here it looks almost the same right there
in fact it looks exactly the same if you start looking at it up close so
this leads us to the next topic here which is the logical equivalence
so logical equivalence here let’s just recall that two statements p
q are logically equivalent means that the biconditional is a tautology
so let’s go and see that let’s look at how to do that in python so again i just
wanted to mention that you know i did a whole video whole
previous episode over logical equivalence i hope you check that out
and all right so here here are three ways of checking for
logical equivalence so the first way is to look at the statement and it’s

contrapositive and then we can say if and only if so
remember this is how we did a biconditional so i’m going to execute
that command right there and so this is going to say okay i recognize the
the propositional statement now i’m going to execute the truth table for it
and so what we notice here is that we can’t see it because it’s off to the
side so let’s bring that into focus there there it goes um so
we have the pqrst and then we have the whole statement and then if as you
notice right here to be a logical equivalent we need this to be a
tautology so as we scroll through the values over there notice every every
value is true in the last column there so that means it’s the tautology
so that’s one way of doing that we can just say statement if and only if
contrapositive print out a truth table go through every value and check that

it’s true another way is to just ask is it a tautology
so i have a statement if and only if the contra positive so i’m checking the
statement and the contrapositive and i’m checking to see if the if
they’re equivalent or not is it is all trues
so again remember we need parentheses around here and here before we use the
dot before we use this method anyways i’m going to execute that command right
there and it’s going to say true and what that true represents is that
all of these call all these values in the last column are true
now a third way um is just uh just do it straight up just say
all right this is a statement this is a contrapositive are they logical
equivalent and then we can just execute that and
we’ll see it’s true this is obviously the faster this is
the better method right here even though this is the method right

here in any case um so let’s look at some examples now
so these are some um logical equivalences that we talked about
discussed in a previous video and i explained you know what logical
equivalence is and how you check it how you verify it just like we did and saw
here in python however i left it as a something for you
to go check is to make all the true tables for all these here well we’re
going to do that real quick right now using this python right here
so i’m going to go over here to and execute each one of these so this is p
and q is it logically equivalent to q and p that’s the first statement right
here um p or q is it logically equivalent to
q or p so we’ll execute both of those and both of those are true so
we can you know carry on we can say and and and and in other words we can move
the parentheses this is the associative property for and and this is the

associative property for or so we can um just evaluate those cells right there
and as you can see they’re logical equivalences they’re coming back true
and then we have the distributive properties here we can distribute the
and over the or and we can distribute the or over the and
and we can check that both of these are logical equivalences
so let’s execute those cells right there all right and then
p or q is equivalent to p and p and p is equivalent to p
let’s check both of those and then finally we have de morgan’s law here the
negation of an and is the or of the negations
and the negation of the or is the and of the negations so let’s check those
and there we go all right so we checked all these
logical equivalences it didn’t take long to work them out and

um so that’s how you do truth tables um and using python um i hope you like this
video if you did like and subscribe and i’ll see you in the next episode
if you like this video please press this
button and subscribe to my channel now i want to turn it over to you math can be
difficult because it requires time and energy to become skills i want you to
tell everyone what you do to succeed in your studies either way let us know what
you think in the comments

About The Author
Dave White Background Blue Shirt Squiggles Smile

David A. Smith (Dave)

Mathematics Educator

David A. Smith is the CEO and founder of Dave4Math. His background is in mathematics (B.S. & M.S. in Mathematics), computer science, and undergraduate teaching (15+ years). With extensive experience in higher education and a passion for learning, his professional and academic careers revolve around advancing knowledge for himself and others. His work helps others learn about subjects that can help them in their personal and professional lives.

Let's do some math together, today!

Contact us today.



About Us


© 2022

Privacy Policy | Terms of Service