Lesson Overview: 3.5 - Boolean Expressions

  • Boolean: A data type with two possible values: true or false

Boolean and Binary

  • Boolean math and binary notation both use the same two ciphers: 1 and 0
  • Differences:
    • Boolean quantities are one singlular bit (can only be either 1, or 0)
    • Binary numbers can be many bits

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...

Relational Operators in action

  • How could you use operators to determine if the average of 5 grades is greater than 80?
  • With the grades below, use a boolean expression 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
gradeAvg = (grade1 + grade2 + grade3 + grade4 + grade5)/5
print(gradeAvg>80)
False

The versatility of relational operators:

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])
100 == 100: True
Hello == Adios: False
Hello != Adios: True
Hello == Hola: True
5>=4: True

['a','b','c'] > ['x','y','z']: False
[1,2,3,5] > [1,2,3,4]: True
[1,2,3,5] < [1,2,3,4]: False
[1,2,3,5] == [1,2,3,4]: False

Logical Operators!

Works 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

Turn the following arithmetic phrases into either True or False statements as indicated USING LOGICAL OPERATORS

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 10>20)
1 > 2 or 5 < 12: True
24 > 8: False
10 > 20: False

Lesson Overview: 3.6 - Conditionals

Selection: uses a condition that evaluates to true or false, determines which part of code is executed

Algorithm is a finite set of instructions that accomplish a specific task

Conditional Statements

Also known as "if statements"

  • if statement: executes a code if a certain parameter of code is met
  • if else statement: similar to an if statement but executes a different line of code if the "if" statement is not met(this is done by the "else" statement)
x = 20
y = 10
if x > y:
    print("x is greater than y")
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")
x is greater than y

Participation

-Calculate the total sum of two numbers, if it is equal to 200, print 200, if otherwise, print the sum.

num1 = 100
num2 = 100
sum = num1 + num2

if sum == 200:
    print("sum = 200")
else:
    print(sum)
sum = 200

Lesson Overview - 3.7 Nested Conditionals

  • Nested conditional statements are if else statements inside more if else 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!!!")
    }
    }
}

Writing Nested Code Activity

  1. 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

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"
experience = 10
salary = 0
if experience>=8: 
    print("experienced")
    salary = 90000
    if experience>=10:
        salary = 150000
    print("Salary: ", salary)
else: 
    print("not experienced")
    salary = 50000
    print("Salary: ", salary)
experienced
Salary:  150000
status = "not expired"
cost = 28

if status == "not expired":
    if cost>=50:
        print("This product is too expensive")
    elif cost>25 & cost<50:
        print("This is a regular product")
    elif cost<=25:
        print("This is a cheap product")
elif status == "expired":
    print("This product is no good")
This is a regular 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
question1 = ["How are you doing in CSP?", "- Very good :D", "- Good :)", "- Mid", "- Bad :("]
question2 = ["What is a boolean expression?", "- Bits", "- A single bit that returns true or false", "- Two bits that returns true or false", "- A single bit that returns a decimal number"]
question3 = ["What is binary 1011 equal to in decimal?", "- 22", "- 20", "- 16", "- 21"]
question4 = ["What is binary 111 equal to in decimal?", "- 16", "- 20", "- 14", "- 8"]


qAns = ["Very good :D", "A single bit that returns true or false", "22", "14"]

score = 0
doubleOrNothing = 0
qAmount1 = 2
qAmount2 = 4
qNum = 0
i = 1
j = 1
# questionDisplay = 0
print("Welcome! Put in your answer according to the given choices below the question. Please type in the exact answer provided from one of the four choices.")
while qNum < qAmount1:
    print("Question #", str(qNum + 1), ":")
    if i==1:
        print(*question1)
    elif i==2:
        print(*question2)
    answer = input()
    if answer == qAns[qNum]:
        score += 1
        print(answer, "is correct!")
    else:
        print(answer, "is incorrect!")
    qNum += 1
    i += 1

print("The next two questions are double or nothing!")
while qNum>=qAmount1 and qNum<qAmount2:
    print("Question #", str(qNum + 1), ":")
    if j==1:
        print(*question3)
    if j==2:
        print(*question4)
    answer = input()
    if answer == qAns[qNum]:
        doubleOrNothing += 1
        print(answer, "is correct!")
    else:
        print(answer, "is incorrect!")
    qNum += 1
    j += 1


if doubleOrNothing == 2:
    score += 4
print("Final score: ", score, "/4")
Welcome! Put in your answer according to the given choices below the question. Please type in the exact answer provided from one of the four choices.
Question # 1 :
How are you doing in CSP? - Very good :D - Good :) - Mid - Bad :(
Very good :D is correct!
Question # 2 :
What is a boolean expression? - Bits - A single bit that returns true or false - Two bits that returns true or false - A single bit that returns a decimal number
testing is incorrect!
The next two questions are double or nothing!
Question # 3 :
What is binary 1011 equal to in decimal? - 22 - 20 - 16 - 21
22 is correct!
Question # 4 :
What is binary 111 equal to in decimal? - 16 - 20 - 14 - 8
14 is correct!
Final score:  5 /4