This video covers the basics of learning about boolean/binary/discrete/True-False/1-0 logic statements in Python! We show how the AND function in python check for two conditions to both be met, while the OR function only requires one conditon to be true, and we introduce the NOT operator which essentially negates any condition to check for the opposite.
With this video as well as the full python for beginners series on our channel, you can go from absolute rookie to experienced programmer in just a few hours with tons of content including practice problems, graphical user interface tutorials for apps, widgets and games and tons of advanced practice projects and programs for coders and programmers of any skill level to find something that interests them!
Hopefully you find this tutorial useful and engaging, and be sure to let us know if there is something specific you'd like to see in future videos and don't forget to like the videos and subscribe to our channel!
what's going on you guys welcome back to
the channel and today i've got a great
introduction python tutorial for you um
we're going to cover the and or and not
conditions in python and talk kind of
about
all the different boolean logic stuff
that you can do so for those of you who
don't know a boolean
is essentially like program or slang for
true or false okay it's a basically a
bit which can equal zero or one
right so um these are one of the most
common variable types in python it
essentially means you're dealing with a
statement that is it can only be in two
states it's
thing one or thing two so um essentially
if you have two variables and i'll just
call them variable one
and we'll say
the second one is equal to
variable two
okay and you give them true or false as
a starting parameter with capital t and
a capital f and now you've defined a
boolean variable and so essentially
the only thing that can happen to those
variables is they're true or they're
false they're equal to one or they're
equal to zero and so that's the kind of
the purpose of the and statement or the
or statement or the not statement um
to check the states of various
conditions so uh we won't just deal in
booleans as the video goes on but we
will start there so we have these two
variables and we're going to combo it
with if statements to kind of show how
we use them so the first thing is we're
going to check if both variables are
true so what we can do is start with if
and then say if variable one
is true
and
variable two
is true okay and if both those things
are true when we run this program we
just want the text
both
variables
are true to pop up on the screen okay so
right now i'm setting variable 1 equal
to true and variable 2 equal to false so
we're going to run this and
when i do oh
i should give it a better name
sort of list okay
when we run this it's um
it's printing
nothing because it's not true so what
you could do is you could combine this
if with an else
and print
one or both
variables are
false okay
so now one of these two things is going
to happen because of the if else
one or both variables are false all
right well if our code is working if i
change variable 2 to be equal to true
and i run it now it says both variables
are equal to true so you can kind of
look at this code and see it it's
making a decision here based on the
state of those two booleans that we set
in this and operator so if i were to
remove the and
it's only checking the state of one
variable so a lot of times you need to
check that something is running and
something else is running like in the
state of a video game you might need to
check what level you're on and so you
might need to see that a game over
variable is false but you also might
need to see that a score is below a
certain threshold or time is over a
certain threshold
so comboing those with the and can be
really useful but let's say you want to
swap that out for an ore and now you're
saying like to move on to the next level
uh you know five minutes could have
passed or the score got above a certain
uh variable
or to turn lights on you have two
different switches and if either of them
are up then you want to move on so
now we'll say for the or it's only
checking that one of the two conditions
is true
that one of the two conditions is
valid so we'll say one of the variables
are true
actually we're going to say one of or
both because as long as
one of them
is true it's going to work but if
they're both true
it's still going to work so the only
condition where we get false here is if
neither are true
all right so i'm going to go ahead and
run this
and you'll see one of or both the
variables are true okay so i'm going to
go ahead and change both of the
variables in the beginning to false
and see what we get
all right
so now we get neither are true but let's
change just variable 2 to true and take
a look at that
okay so one of or both the variables are
true that's the beauty of the or
statement
it only needs one of these two
conditions to be met and another thing i
should have touched on a little bit
earlier even you can have
ores and ands together and you can chain
them all together so if you were going
to add a
variable 3 to this that is an integer
let's say 1000 you can take this first
statement and say variable 1 or variable
2
and
variable 3 and now we have our first
non-boolean variable it's an integer and
we could say variable 3 has to be
greater than 500 so no matter what
one of them has to be true and our other
variable has to be greater than 500 so
even if these are both true or one of
these is true we still need to meet the
conditions of the and statement so
it's it gets a little bit messy here
when you start introducing a whole bunch
of parameters so you can break it down
into even more layers and you can add if
statements inside of other if statements
we'll cover that in a separate video
but for this kind of condition i'll go
ahead and run it here
one or both of the variables are true
and
variable 3 is over 500
neither are true
or
variable three is less than
500 okay so let's go ahead and take a
look at this
one of or both the variables are true
and variable three is over five hundred
so those are the conditions that are
going to make this statement true let's
bump variable 3 down to 100 and i'm
going to set both of the other ones
equal to true
this or statement is clearly
should be getting through because both
variables are true now but variable 3 is
under 500 so
we'll get neither are true or variable 3
is less than 500. so right there you can
see combining the ands and the ors
allows for more detailed
boolean logic combining with combining
with integer logic
so we're going to do some examples at
the end of the video but the next thing
i want to talk about is a
special thing that python has for
simplifying this kind of logic and you
can kind of see actually it's trying to
tell me because i'm using pi pi charm as
my ide
and it does some suggestions for
simplifying your code
when you have a boolean variable like
variable one and variable two here that
are either true false
saying if or i'm gonna get rid of
everything uh
let me get rid of the variable three for
now and say one of or both variables are
true we'll cut it down to that and we'll
cut down to that so
you can simplify this statement just by
saying if variable one or variable two
so think of this in terms of a zero and
one one being true zero being false
this statement if variable one
or variable two
is equivalent to the statement we just
deleted which is if variable one equals
true
or
variable 2 equals true any condition
where you're checking true false
essentially on a boolean variable you
don't need the equals true or the equals
false you can just say if
and then the variable name and if what
you're trying to check is this if
variable one equals false
then you can
and let's go ahead and actually test
this one and say
variable one is
false
and then we'll say
true otherwise so if what you're trying
to say is variable one equals false and
let's make that
happen here
all right you can see right now variable
one is equal to false but this is
actually going to work even if i say
and actually let's go ahead and do this
this is checking the inverse
all right
if i put if not variable 1 that is the
same as saying if equals false so that
is the function of the not statement is
it basically negates and does the
inverse of whatever you said
so
if not a true false statement is the
same as saying if the variable is equal
to false and that's why we're getting
variable as false
now not can be used with
integer and float values as well so
let's go ahead and say if not
variable 3 greater than 500 now this is
a stupid way of doing it but you would
be saying if the variable is not greater
than 500
print variable less than 500
and so that's actually gonna and we'll
say over 500 down here
so if you look at what we have right now
variable 3 is equal to 100 which is not
greater than variable
which is not greater than 500 so this
statement is actually true because we're
saying
if variable 3 is less than 500 but
instead of saying less than or equal to
500 we just said if not greater than
500. so
you probably wouldn't use this as much
for analogs as you would for boolean
values but it does pop up now and then
and so being familiar with it can be
really useful there so let's go ahead
and take a look at just one quick
example that i think will highlight um
kind of a lot of boolean logic here and
can help you out and there's also a list
of python beginner like practice
problems practice interview problems or
quiz problems on this channel i
recommend you check that out if you want
a whole bunch of logic and reasoning
with booleans
that kind of practice can be super
helpful but so let's say for
example
you
want
an alarm
to go off
if it is a weak day
and you are
not on
vacation
okay so
what we're saying is if it's a monday
through friday
and you're not on vacation you didn't
take the day off you want your alarm on
so you could do this
in a kind of a complicated way but i
want to keep it simple just so you get
the practice here essentially you have
two variables you have
weekday
which is going to be true or false if
it's monday through friday it's a true
if it's saturday sunday it's false and
then you have
vacation which we're gonna say is false
so
what your like calendar would be doing
or your watch would be doing would be
checking like if day is equal to
um
you know
monday tuesday wednesday thursday friday
it would be doing something like that
but for this example we're just going to
take a look at the actual weekday and
vacation statuses
so let's say it's a tuesday so weekday
is true and you didn't take vacation um
so the uh the
vacation parameter is going to be false
well we're going to say if weekday
and not
vacation
print well we'll say alarm equals true
and then we'll print
alarm and so
to start this you have to
initialize your parameter i'm going to
say alarm equals none just so we don't
put any default variable in when we call
it
and then we're going to print the state
of alarm so
uh essentially what what we're doing
here is we're checking if it's a weekday
and that we're not on vacation and if
those are both true right those are the
conditions in the uh example we're going
to set the alarm on so let's go ahead
and run this
all right and we get true because we
said it is a weekday and we're not on
vacation well now let's say it's a
weekend and let's set weekday equal to
false
okay and we run it we get none um which
is basically saying there's nothing in
there what we could do is say else
alarm
equals false okay right so if it is the
weekend or we did take vacation then we
want the alarm to turn off so
we run it and we see we get false
because we said it's not a weekday now
let's say it is a weekday
true
oh boy i'm having trouble typing
okay but let's say you took a vacation
so it's a wednesday but you did you
requested vacation um
earlier so we run it and we get false
and essentially that's all there is to
it um you know this is a way of using
not and and um another way you could
write this just to show it's equally
valid and use an or statement
would be
if not weekday
or vacation right so if not weekday or
vacation we want alarm to be equal to
false else alarm is equal to true that's
actually exactly equivalent to what we
just had written so i'll type those both
next to each other just try not to
get too confused here
if weekday and not
vacation alarm equals true
else
alarm
equals
false so i'm going to explain just
briefly why the two things we're looking
at here
are exactly the same in terms of what
they'll output
but i wanted to show not
or and and all in this example
what we're saying here is if it is a
monday through friday and you didn't
take vacation turn the alarm on and in
any other scenario which is basically
checking to see if it's not a weekday or
you're not on vacation turn it off
so what we did here is we just did the
opposite we started by checking if it's
not a weekday or if you're on vacation
and there's no reason that you would
need both of these in your programming
and there's no reason one is more
correct than the other one
so that right there is a great example
of how there are multiple ways to code
almost every scenario in almost every
problem you're going to come across so
essentially just being familiar with the
basic tools of python and how to combine
different instructions is going to lead
you to writing code that works for you
and as long as it works that's the most
important part so hopefully you found
this useful there's a wide variety of
content on the channel from introductory
videos like this to game and app
tutorials in using different graphical
user interfaces using pygame kivi kinter
so check all of those out if you're
curious if you found this or anything
else on the channel useful i really
appreciate a like on the video subscribe
to the channel it helps me out a ton and
if there's anything in particular you
want to see in a future video just let
me know about in the comments below and
if you have any questions about what you
saw here today let me know about the
comments below as well and as always
good luck with your code and thanks for
watching thanks bye