
Syntax: And Operator
Checking if multiple things are true with the logical "and" operator.
The Boolean "AND" Operator in Python
In some instances when creating conditional statements, you will need to check if two expressions are true at the same time.
- An example of this would be if you needed to check that a numeric value was both a positive integer and that its value is less than 10.
In Python, creating a conditional statement that requires at least two expressions to be true at the same time is done with the and operator.
Note the syntax examples below:
packages = 4
print(packages >= 1 and packages <= 6)
> True
desired_hotdogs = 9
hotdogs_in_pack = 10
buns_in_pack = 8
enough_hotdogs_to_eat = desired_hotdogs <= hotdogs_in_pack
enough_buns_to_eat = desired_hotdogs <= buns_in_pack
print(enough_hotdogs_to_eat and enough_buns_to_eat)
> False
*Note: the second example is False due to enough_buns_to_eat being False, even though enough_hotdogs_to_eat is True. With and, both booleans need to be True for the output to be True.
Looking at the first example above, note that both sides of the and operator require a complete expression. Let's look at another example:
value = 5
print(value < 10 and value > 3)
Contrast that with this example that would not work:
value = 5
print(value < 10 and > 3)
If you tried to run the above code, you will get a syntax error. Remember that both sides of the and operator must contain full expressions!
It's also important to know that the and operator can be used more than once to connect expressions when you have multiple expressions that must be true at the same time:
full_name = "Franklin Delano Roosevelt"
print(full_name.startswith("Franklin") and full_name.endswith("Roosevelt") and full_name.istitle())
> True
Finally, its important to know that when checking expressions which use the and operator, no conditions will be checked after a false one is found.
For Example:
value = 15
print(value < 10 and value > 3)
Since value is 15 in this instance, the expression value < 10 will be false and the expression value > 3 will not be evaluated at all since the first expression was already found false.
Practice: AND Operations
Based on the information above. Complete the following steps:
-
Create a variable named low_price and assign it a value of 15.99.
-
Create a variable named high_price and assign it a value of 26.99.
-
Create a variable named test_price and assign it a value greater than high_price.
-
Create a variable named test_1 and assign it the result of an
andexpression checking if the test price is greater than the low_price value and less than the high_price value. -
Print the value of the test_1 variable.
-
Update the value of the test_price variable to a value between the low_price and high_price values.
-
Create a variable named test_2 and assign it the result of an
andexpression checking if the test price is greater than the low_price value and less than the high_price value. -
Print the value of the test_2 variable.
HINT*: Click here for a hint
> False
> True
SOLUTION*: Click here for an example solution
low_price = 15.99 high_price = 26.99 test_price = 28.98 test_1 = test_price > low_price and test_price < high_price print(test_1) test_price = 18.99 test_2 = test_price > low_price and test_price < high_price print(test_2)
When the above is completed and you are getting the proper output, you are ready to move onto the next exercise!