Combine Boolean Expressions

In the last lesson I talked about the basics of a boolean expression. In essence, a boolean expression is some form or comparison that yields either a True or a False. These expressions are especially useful when working with if-statements. If statements are used to make choices, and we generally make choices based on these kinds of expressions.

A few examples are shown below:

age = int(input('Enter age: '))
if age > 21:
    print('You may drink')
money = float(input('Money in account: '))
if money > 1500.0:
    print('You can afford to pay for dinner')

Both in real-life and in programming, there are often choices that depend on multiple different true / false conditions, rather than just one. For example, suppose that there is a ride at a theme park that has both a minimum height of 40 inches and a maximum height of 70 inches in order to ride. There are now two things that need to be checked in order to allow a person to ride, rather than just one. We could write a program to tell the user if he can ride the ride or not using separate if statements to determine this:

1 height = int(input('Height in inches: '))
2 if height >= 40:
3     if height <= 70: 
4         print('You may ride')
5 if height < 40:
6     print('You may not ride')
7 if height > 70:
8     print('You may not ride')

Technically, this program would work just fine, and it would give the user the correct answer. The 'You may ride' print on line 4 will only get run if the condition in the if statements on both lines 2 and 3 are true. After getting the input on line 1, the if-statement on line 2 will run. If the height is greater than or equal to 40, we still need to ensure that the user is not too tall. Thus, within that if, we put another if to check the max height. Then, if that is true, we can tell the user he is able to ride. After, there are two other if-statements to handle being too tall or too short. We could diagram this program to see the flow as follows:

graph LR classDef blue fill:#cef,stroke:#059; A[lines 1,2] --> |True| B[line 3]; B --> |True| C[line 4]; C --> D[line 5] B --> |False| D[line 5]; A --> |False| D[line 5]; D --> |True| E[line 6]; D --> |False| F[line 7]; E --> F[line 7] F --> |True| G[line 8]; F --> |False| END[End]; G --> END[End]; class A blue; class B blue; class C blue; class D blue; class E blue; class F blue; class G blue; class END blue;

Though it works, this is unnecessarily complex! One thing that would help us significantly simplify the code are the and and or operators. You can use the and keyword within a boolean expression in python. If you put a True or False value on either side of it, it will combine the two together! If both sides are true, it will yield True, otherwise it will yield false.

For example, the if condition if 4 < 5 and 3 > 2: would be True because 4 < 5 is True, and 3 > 2 is rue. However, if we changed it to this instead: if 4 < 5 and 3 < 2: then the if statement condition would be false, because one side (3 < 2) is not true anymore.

The or operator can be used in a similar way, except it will result in True if either side (or both) is True. It will only result in False if both sides of the or are false. For example, the if condition With an or, this would result in a True if condition since only one side is False: if 4 < 5 and 3 < 2:. With an or, However, if both sides were false, this would result in a False: if 4 > 5 and 3 < 2:.

How could we use these two operators to our advantage in the amusement park ride example? Well, we know that a person has to be BOTH greater than or equal to 40 inches tall AND less than or equal to 70 inches to ride the ride. We also know that if the user is EITHER shorter than 40 inches OR taller than 70 inches, he cannot get on the ride. Thus, we could reqrite the code with less lines:

1 height = int(input('Height in inches: '))
2 if height >= 40 and height <= 70: 
3     print('You may ride')
4 if height < 40 or height > 70:
5     print('You may not ride')

We were able to eliminate 3 lines of code! Also, the flow diagram can be simplified too:

graph LR classDef blue fill:#cef,stroke:#059; A[lines 1,2] --> |True| B[line 3]; A --> |False| C[line 4]; B --> C[line 4] C --> |True| D[line 5] C --> |False| END[End]; D --> END[End]; class A blue; class B blue; class C blue; class D blue; class END blue;

Going forward, if you find yourself in a situation in code where you have to make a choice about wether or not to run some lines, but that choice depends on multiple conditions, consider combining them with and / or.

There’s another thing about this code that could be improved upon still. If you think about it, it is kind of a ways to have to compare the users height against the numbers 40 and 70 twice throughout the code. If the first if-statements condition is false, we should be able to infer that the user MUST not be in the correct height range. Therefore, the second if-statement check is kind of excessive and wasteful. Howe could we eliminate this? In the next lessions, I will show how this can be done using the else keyword.



PyFlo Home Complete Bookmark Next