Exercise: 9. This and That (or This, But Not That!)


#1

https://www.codecademy.com/courses/learn-python/lessons/conditionals--control-flow/exercises/this-and-that-or-this-but-not-that?action=resume_content_item
This exercise became really confusing. Even though the instructions do say to refer to the “Stuck? Get a Hint”, I was still lost :confused:. And still am. The Hint says “True or not False and False. not gets evaluated first, so we have True or True and False. and goes next, so we get True or False. As we’ve seen, True or False is True, so the value finally returned is True!” All I see is “True, False, True, Not…” So on. Can someone clarify in more simpler terms? :slightly_smiling_face:


#2

Precedence

not
and
or

in that order. not applies to the value immediately to its right.


#3

I’m still confused, but I’ll try to read the previous exercise to understand. Why does coding get so complicated??? Quick question though; Can you give me an example other than the one one the exercise? Because I think maybe listening the someone else response can give me another viewpoint on this situation.


#4

Like most things, it gets easier as you go, once the concepts are clear.

Consider the basic truth tables.

A AND B
   T  F
T  T  F
F  F  F


A OR B
   T  F
T  T  T
F  T  F

So,

True and True   => True
True and False  => False
False and True  => False
False and False => False

True or True    => True
True or False   => True
False or True   => True
False or False  => False

As we can see, only when both operands are True is A and B True, otherwise False; and, only when both operands are False is A or B False, otherwise True. This is known as short-circuiting. Any operand that is False in an AND expression will short-circuit to False as the result. Any operand that is True in an OR expression will short-circuit to True as a result.

A or B or C or D

will be True if any one of the operands is True.

A and B and C and D

will be False if any one of the operands is False.

This gets a little more complicated when we consider casting which is when a value or expression is evaluated as a boolean. Truthy values will cast to True, and Falsy values will cast to False.

0, '', "", None

are all Falsy, so cast to False in an expression:

0 or '' or "" or None     => False

0 and '' and "" and None  => False

Everything else is Truthy.

1 and 'a' and "A" and not None  => True

See where we snuck in the not? That negates a boolean, so if a value is True, it becomes False, and if it is False, it becomes True.

Let’s use the bool() class to test the above.

>>> bool(0)
False
>>> bool('')
False
>>> bool("")
False
>>> bool(None)
False
>>> 

Above we used the bool function to convert (cast) the values to their boolean equivalent.

>>> bool(1)
True
>>> bool('a')
True
>>> bool("A")
True
>>> not None
True
>>> 

Notice how not behaves just like the bool function? That’s right, we can use not to coerce (force cast) a boolean from an expression or value.

>>> not 1
False
>>> not 0
True
>>> not 'real'
False
>>> not not 'real'
True
>>> 

This takes a little while to get straight so work on lots of exercises to cement the ideas in your head. It may not seem important but it is at the very core of logic and is vital to the cause of programming. Programs make all their decisions using IF, AND, OR and NOT conditional expressions.

I’ll use another post to go over the lesson material with you.


#5

Set bool_one equal to the result of

False or not True and True

Set bool_two equal to the result of

False and not True or True

Set bool_three equal to the result of

True and not (False or False)

Set bool_four equal to the result of

not not True or False and not True

Set bool_five equal to the result of

False or not (True and True)

bool_one

False or not True and True

Start with NOT, not True => False

Rewriting gives,

False or False and True

Next work with the AND, False and True => False

Rewriting gives,

False or False

which we know to be False.

bool_two

False and not True or True

Start with the NOT,

False and False or True,

Next the AND,

False or True

which we know to be True.

bool_three

True and not (False or False)

Brackets always come first so,

True and not False

then NOT,

True and True

which we know to be True.

bool_four

not not True or False and not True

First the NOT,

True or False and False

then the AND,

True or False

which we know to be True.

bool_five

False or not (True and True)

Brackets come first,

False or not True

Then the NOT,

False or False

which we know to be False.


#6

Wow, so much more simple. Thanks a lot Mr. Roy. This helped very much. :grinning:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.