Sections 3.5 to 3.7
Lesson Overview: 3.5 - Boolean Expressions
- Here we will focus on:
- basics of Booleans
- its relationship with binary
- relational operators
- Logical Operators
What is a boolean?
- A data type with two possible values: true or false
Boolean and Binary
So similar yet so different.
- Boolean math and binary notation both use the same two ciphers: 1 and 0.
- However, please note that Boolean quantities are restricted to a singlular bit (can only be either 1, or 0)
- On the otherhand, binary numbers may be composed of many bits adding up in place-weighted form to any finite value, or size
Must Knows
- A Boolean value is either TRUE or FALSE
- The AP Exam will provide you with a reference sheet with the operators below.
- A few ways these operators could be used...
- With the grades below, use boolean expressions to determine if the average grade is above an 80 and print the result (True or False)
- Try it in as few steps as possible!
- Be creative! There are obviously TONS of different practical solutions
grade1 = 90
grade2 = 65
grade3 = 60
grade4 = 75
grade5 = 95
result = (grade1 + grade2 + grade3 + grade4 + grade5)/5
if result > 80:
print("true")
else: print ("false")
print("100 == 100:",100==100)
print("Hello == Adios:","greeting"=="farewell")
print("Hello != Adios:","greeting"!="farewell")
print("Hello == Hola:","greeting"=="greeting")
print("5>=4:", 5>=4)
print ('')
# Notice that relational operators can even work on lists!
# For lists, the relational operator compares each respective component until an answer is derived
print("['a','b','c'] > ['x','y','z']:", ['a','b','c'] > ['x','y','z'])
print("[1,2,3,5] > [1,2,3,4]:", [1,2,3,5] > [1,2,3,4])
print("[1,2,3,5] < [1,2,3,4]:", [1,2,3,5] < [1,2,3,4])
print("[1,2,3,5] == [1,2,3,4]:", [1,2,3,5] == [1,2,3,4])
Logical Operators!
These types of operators don't necessarily deal with equivalent/non-equivalent values, but they rather work on operands to produce a singular boolean result
- AND : returns TRUE if the operands around it are TRUE
- OR : returns TRUE if at least one operand is TRUE
- NOT : returns TRUE if the following boolean is FALSE
print("1 > 2 or 5 < 12:", 1 > 2 or 5 < 12)
# Output TRUE using OR ^
# Output FALSE using NOT
print("24 > 8:", not 24 > 8,)
# Output FALSE using AND
print("10 > 20:", 10 > 20 and false)
x = 20
y = 10
if x > y:
print("x is greater than y")
x = 20
y = 10
if x > y:
print("x is greater than y")
else:
print("x is not greater than y")
num1 = 78
num2 = 106
sum = num1 + num2
if sum == 200:
print("the sum is 200")
else: print("the sum is not 200. Sum: "), print(sum)
- Nested conditional statements consist of conditional statements within other conditional statements
- Utilizes "if else" statements within "if else" statements
- Basics of a nested conditional:
- Block Coding Visual of Nested Conditionals:
- Example Psuedocode of Nested Conditional Statements
Analyzing Code Walkthrough
- Psuedocode to the left, block code to the right
Approach the problem by going through each condition one at a time
- Decide which ones are false to skip and which ones are true to execute
You Try:
score = 82
if (score >= 90)
{
console.log("You got an A, congrats!")
}
else
{
if (score >= 75)
{
console.log("Please come to retake up to a 90 next week at tutorial!")
}
else
{
console.log("You have detention!")
}
}
protein = 25
carbs = 36
sugar = 11
if (carbs >= 55 || protein <= 20 || sugar >= 15)
{
console.log("Your lunch is too unhealthy, please pick a new one")
}
else
{
if (carbs < 35 || protein < 25)
{
console.log ("This lunch is alright but try to add some more carbs or protein")
}
else
{
if (sugar >= 11)
{
console.log ("Looks great but lets see if we can cut down on sugar, we don't want diabetes!")
}
else
{
console.log ("Amazing, you created a healthy lunch!!!")
}
}
}
note: the double lines || means OR
Writing Nested Code Activity
- Write a program that fits these conditions using nested conditionals:
- If a person has at least 8 hours, they are experienced
- If a person is experienced their salary is 90k, if they have ten hours or above their salary 150k
- If a person is inexperienced their salary is always 50k
- print the salary of the person at the end and whether they are experienced or not
hours= 7
if (hours < 8)
{
console.log ("This person is not experienced :(. Your salary is 50K")
}
else (hours >= 8 && hours < 10)
{
console.log ("This person is experienced, they have a salary of 90K")
}
else (hours >= 10)
{
console.log ("This person is very experiences, they have a salary of 150K")
}
Hacks Assignments:
Conditionals:
- Write a program that fits these conditions using nested conditionals:
- If the product is expired, print "this product is no good"
- If the cost is above 50 dollars, and the product isn't expired, print "this product is too expensive"
- If the cost is 25 dollars but under 50, and the product isn't expired, print "this is a regular product"
- If the cost is under 25 dollars, print "this is a cheap product"
# Here is a python template for you to use.
#if the age of the product is under 1 month, the product is not expired
age = 1;
cost = 37
print ("how long the product has been on the shelf (it expires after 2 months): ", age, "months")
print ("cost: $", cost,)
if (age > 2):
print("this product is no good");
elif (age <= 2 and cost > 50):
print("this product is too expensive");
elif (age <= 2 and cost >= 25 and cost < 50):
print("this is a regular product");
elif (cost < 25):
print ("this is a cheap product");
Boolean/Conditionals:
- Create a multiple choice quiz that ...
- uses Boolean expressions
- uses Logical operators
- uses Conditional statements
- prompts quiz-taker with multiple options (only one can be right)
- has at least 3 questions
- Points will be awarded for creativity, intricacy, and how well Boolean/Binary concepts have been intertwined
print("this is my multiple choice quiz on frogs:")
print(" ")
score = 0
#question 1:
print("Question 1: How many toes does a frog have on each foot?")
q1_answers = {"a": "3",
"b": "4",
"c": "5",
"d" : "7"}
print("")
for n in q1_answers:
print(n, ":", q1_answers[n])
def users_pick(prompt):
msg = input()
return msg
rsp = users_pick("")
if rsp == "b":
print("your response was:", rsp)
print("correct")
score = score + 1
else:
print("your response was:", rsp)
print("incorrect, a frog has 4 toes")
#question 2:
print("Question 1: What is the life span of a poison dart frog?")
q1_answers = {"a": "2 - 3 years",
"b": "4 - 7 months",
"c": "10 - 15 years",
"d" : "30 - 35 years"}
print("")
for n in q1_answers:
print(n, ":", q1_answers[n])
def users_pick(prompt):
msg = input()
return msg
rsp = users_pick("")
if rsp == "c":
print("your response was:", rsp)
print("correct")
score = score + 1
else:
print("your response was:", rsp)
print("incorrect, the average life span of a poison dart frog is 10 - 15 years")
#question 3:
print("Question 1: How much does the largest frog weigh?")
q1_answers = {"a": "3 pounds",
"b": "10 pounds",
"c": "5 pounds",
"d" : "7 pounds"}
print("")
for n in q1_answers:
print(n, ":", q1_answers[n])
def users_pick(prompt):
msg = input()
return msg
rsp = users_pick("")
if rsp == "c":
print("your response was:", rsp)
print("correct")
score = score + 1
else:
print("your response was:", rsp)
print("incorrect, the largest frog weighs 7 pounds (as much as a newborn baby")
print("thanks for taking the quiz.. your score was: ", score, "/ 3")