hey everyone I talked about using
boolean in Python in a few of my
previous videos but I decided to make a
whole new video about it because it's an
important topic
now boolean is just a type of data just
like strings and integers that we've
seen now examples of a string might be
something like Microsoft Apple and
banana and usually strings in Python are
enclosed in either double quotes or
single quotes examples of an integer
might be something like 3 4 2 and - 100
- and what about boolean well for
boolean there are only two possible
values and there are true and false and
a boolean is basically used to express
if a certain logical condition is true
or false now in Python there's a useful
function called the type which we can
use to find type of any value so for
example if you write type of double
quotes Microsoft then this gives us STR
which stands for string and if we write
type of let's say for this will let us
know that this is an integer because it
says int and if you write type of true
note that true here is capitalized this
gives us bull which stands for boolean
and let's now see how we can use boolean
berries actually you've already seen one
use case of a boolean value in my
if-else videos so if you write for
example a goes 3 and B equals 1 and then
if a is greater than B colon 4 spaces
prints a is greater than B then this
Clause right here a greater than B is
evaluated to be either true or false and
in this particular case a which is 3 is
greater than B which is 1 so this is
evaluated to be true so it's sort of
like saying if true then print a squared
of B in this particular case a squared
of B is true so that's printed right
here
so actually we
get the same result by writing if true :
and again note that sure here is
capitalized print a is greater than B
once you run this cell you see that a
squared of B is printed and actually you
could also store the result of this
condition a greater than B in a variable
so let's call that variable boolean
value or boolean and the score value and
then we're gonna write this equals a
greater than B and when I run this cell
a greater than B is evaluated and that
returns true and then this variable
boolean value will have that value true
so if you print boolean value we should
see true and then after that you can
write if boolean value : four spaces
print a is greater than B and when we
run the cell we get the same result
because boolean value is true so when
should you use boolean values well
they're basically useful when you want
to do some complex logical operations so
let's say you want to write a function
called are you sad which is gonna take
two boolean arguments is rainy and has
umbrella and let's say from this
function you want to return true and
indicate that you're sad if it's raining
and if you don't have an umbrella so you
want to return true only when is raining
it's true and how umbrella is false so
you could write this function this way
if is rainy equals true and has umbrella
echoes that's double cosine false column
then return true and else return false
so this says you're only sad or you're
going to only return true when is rainy
is true and house umbrella is false and
if that's not the case then return false
so this function works just fine but you
can simplify this a little bit more by
writing if is rainy
not has umbrella then returned true so
this is exactly the same as saying if is
rainy is true and house
umbrella is not true or has umbrella is
false then return true and basically
this whole expression is rainy and not
has umbrella will be evaluated to be
either true or false and if this whole
expression will be evaluated to be true
then we're going to return true and if
the whole expression becomes false then
we go to the else clause and then we
return false
from our function and that actually
suggests that we can further simplify
this by writing return is rainy and not
has umbrella and that's actually all we
need so this Clause says we're going to
return the result of this whole
expression is rainy and not has umbrella
and this whole expression will become
true only when is rainy is true and has
umbrella is not true or has umbrella is
false
so let's now run this cell and let's
test this function if you right are you
sad of true and false so if it's rainy
and if you don't have an umbrella you
should be sad so this should return true
and it does and if you right are you sad
of true and true so if it's rainy and if
you do have an umbrella you shouldn't be
sad so this function should return false
and it does and if you want you can test
it for all the other cases too and that
will be false true and false false and
either way this function should return
false okay and here's a little exercise
problem for you can you write a function
called C greater than D plus E which is
going to take three integer arguments C
D and E and this function should return
true only when C is greater than D plus
E and otherwise it should return false
so maybe pause the video right here if
you want to try solving this problem
yourself okay here's my solution here
you could write if C greater than D plus
e column then return true and else :
return false and this is just fine but
you could also write it as return C
greater than D plus E and this way if C
is greater than D plus C this whole
expression becomes true so we'll return
true in that case and if that's not the
case then this whole expression becomes
false so we'll return that so let's just
run this cell and test it I'm gonna
write C greater than D plus e 3 1 1 C
which is 3 is greater than 1 plus 1 so
this should return true and it does and
I'm gonna write C greater than and by
the way you can just here press tab to
automatically complete the rest of the
function and then 3 1 2 3 is not greater
than 1 plus 2 which is 3 so this should
return false and it does ok and that's
it for this video but if there is any
topic I should cover in future videos or
if anything was unclear in this video
just let me know in a comment and as
usual thanks for watching and I'll see
you in the next video