The boolean and function


#1



:grin: Hello I am new to codecademy and I am really struggling to understand the point of booleans. I write the code correctly etc and pass each lesson but I am failing to understand the point of the 'and' function. For instance, does bool_1, which I have defined as False and False, then correspond to bool_2, which I have defined as two incorrect mathematical equations?
I hope this makes sense and I thank you for your help :blush: :blush:



#2

Some extra reading may be in order. Study up on logical AND, logical OR, and the other related logical operations.

Briefly,

A and B

will evaluate to True if, and only if both A and B are True, or truthy. This means,

False and False

will evaluate to False, as will,

True and False

and,

False and True

Examining expressions,

9 == 2 * 5 - 1
8 == 2 ** 3

The above identity comparisons both evaluate to True, so,

9 == 2 * 5 - 1 and 8 == 2 ** 3

will evaluate to True.

Any number that is non-zero, any string that is not empty, and any expression that yields a non-zero or non-empty string value will evaluate to True. They are described as truthy.

Zero, "", '', and None are described as falsy.

0 and 0   => False
1 and 0   => False
0 and 1   => False
1 and 1   => True

#3

Thank you - I understand the majority of that now! :slight_smile: However I have one more question: how do you evaluate expressions? Is it through the print function? Is it even necessary to manually evaluate it?


#4

Normally our program will do that; however, in order to write code that we can be confident in, we should be fluent at manual evaluation. It's the only way we can train our brain. Practice is the best way to learn.

In code, the interpreter does it when parsing. We see 4 * 6, the computer evaluates this to 24.

When we print, all that is printed is what the computer sees.

A = 2 * 5 - 1   => computer sees 9
B = 2 ** 3      => computer sees 8

A and B         => 9 and 8 => True and True => True

Read up on precedence so you learn and memorize the correct order to evaluate complex expressions. This is discussed in the lessons, but needs reinforcement on our own. This unit is one of the most important modules in the entire course and applies to all languages, not just Python. What you learn will be useful across the board.


#5

Cool - I will do! Thank you for clearing this up so quickly & thoroughly :relaxed:


#6

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