Logical Statements

Logical Statements

We just learned that we can compare other types of variables like strings or ints, but what if we could compare booleans? ​

Using a regular comparison only checks if the booleans are the same. So if you have two false booleans, False == False will return True.But what if you wanted to know if two values were both True? There are some special types of comparisons that that let you do this.

AND Statements:

What if we wanted a comparison that told us if both variables were true? We can use an “and” statement to do this:

An AND statement is set up just like a comparison, but with the keyword “and”. It will only return True if the expression on its left and on its right are both True.

OR Statements:

An OR statement is a different type of comparison that checks if either of the two booleans are true.

OR statements use the keyword “or”. They will only return False if both of the booleans are False; if either one of the two booleans is True, it will return True.

NOT Statements:

Another useful tool to be able to pull out of your programmer’s toolkit is the NOT statement. Just like != returned the opposite of ==, not will return the opposite of a boolean.

This can be used to modify the traditional AND, OR and comparison statements.

Play around with some expressions of your own. Logical Statements can be very useful when designing your own program, and you can always test out your expression in a console before putting it in your code.