What you will learn
- Define and identify: boolean, expression, composition, True, False
- Evaluate a boolean expression
- Compose boolean expressions using
and,or,not,<,>, and==
Reading
Do Now
Part 1
Open up the console. Type each line of the following code into the interactive editor:
>>> 5 < 3
>>> 5 > 3
>>> type(5 < 3)
>>> type(5 > 3)
>>> my_favorite_animal = "cats"
>>> user_favorite_animal = input("What is your favorite animal? ")
>>> my_favorite_animal == user_favorite_animal
In your Notebook, answer the following
- What does
5 < 3evaluate to? - What is the type of
5 < 3? What does that stand for (should be familiar from Snap!)? - What is the difference between
==and=? - What data type do you think
my_favorite_animal == user_favorite_animalis?
Part 2
Open up the console. Type the following code into the interactive editor.
>>> months_with_driving_permit = 6
>>> age = 16
>>> can_get_license = months_with_driving_permit >= 6 and age >= 16
>>> print(can_get_license)
In your Notebook, add this to your notes in your Notebook
- What does
anddo here? What type do you thinkcan_get_licenseis? - Update the code to fit the new driving law: If you are over the age of 18 you don’t need to have a permit
Part 3
Open up the console. Type the following code into the interactive editor.
>>> animal = 'mouse'
>>> animal == 'cat' or 'dog'
In your Notebook, continue to your answers in your Notebook to the following
- What does = and == do here?
- What is the difference between the to statements?
Part 4
Open up the console. Type the following code into the interactive editor.
>>> animal = 'mouse'
>>> animal == 'cat' or animal == 'dog'
In your Notebook, continue your notes in your Notebook
- What does = and == do here?
- What is the difference between the to statements here and Part 3 statements?
Lab
In your Notebook
Predict if each of the following examples will produce a True or False output. Check your answers in interactive mode.
Example 1
>>> a = 100
>>> b = "science"
>>> a > 75 and b == "science"
| Predicted Output | Actual Output |
|---|---|
Example 2
>>> a = 100
>>> b = "science"
>>> a > 75 and b != "science"
| Predicted Output | Actual Output |
|---|---|
Example 3
>>> a = 100
>>> b = "science"
>>> a > 75 or b != "science"
| Predicted Output | Actual Output |
|---|---|
Example 4
>>> a = 100
>>> b = "science"
>>> c = True
>>> not c and a > 75 and b == "science"
| Predicted Output | Actual Output |
|---|---|
In your Console
Complete the following coding challenge
-
Create a “Can I be President?” program, which determines if the user meets the minimum requirements for becoming the President of the United States. Have the user input the information needed.
The minimum requirements to be president of the United States are:
- Older than 35
- Resident of US for 14 Years
- Natural born citizen
- Print
Trueif the person could be president andFalseif they can’t be president.
-
Create a “I can’t be President?” program. Print
Trueif the user cannot be President andFalseif they can be President. -
Create a “Can I ride the roller coaster?” program. A roller coaster has the rule that a rider has to be over the height of 50 inches. Because of a legal loophole, if you are over the age of 18 you can ride regardless of your height. If you are allowed to ride, the coaster costs 4 quarters (although the operator accepts tips so more money is appreciated).
- Also, the theme park sells frequent rider passes: with a frequent rider pass the roller coaster costs only 2 quarters. Ask the user how tall they are in inches, their age, how many quarters they have, and if they have a frequent rider pass. Print
Trueif the person can ride andFalseif they can’t.
- Also, the theme park sells frequent rider passes: with a frequent rider pass the roller coaster costs only 2 quarters. Ask the user how tall they are in inches, their age, how many quarters they have, and if they have a frequent rider pass. Print
Bonus
Are the following expressions equivalent? Research DeMorgan’s Laws and write why you think they are the same or why they are not the same
not(x or y) == not x and not y
not(x and y) == not x or not y
Check your understanding
Pick an answer for each question. You can try again until you get it right.
1. What is the difference between = and ==?
- They mean the same thing
=compares two values;==stores a value==stores a value in a variable;=is only used in strings=stores a value in a variable;==compares two values
A single = assigns a value, while == asks whether two values are equal and gives a boolean.
2. If x = 8, what does x > 3 and x < 6 evaluate to?
FalseTrue8- An error
With and, both sides must be true, and x < 6 is false, so the whole expression is False.
3. What is the value of not (4 > 9)?
False4True9
The expression 4 > 9 is False, and not flips it to True.