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

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
def Average(lst):
    return sum(lst) / len(lst)

lst = [90, 65, 60, 75, 95]
average = Average(lst)
  
print("Average of the list =", round(average, 2))
Average of the list = 77.0

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!

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

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

Lesson Overview: 3.6 - Conditionals

Focusing on Selection

Selection: uses a condition that evaluates to true or false

Selection determines which part of an algorithm are executed based on a condition being true or false

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

Conditional Statements

Also known as "if statements"

Can be seen as if statements or if blocks

Can also be seen as if else statements or if else-blocks

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 = 400
sum = num1 + num2
if sum == 200:
    print("yea 200")
else:
    print(sum)
500

Lesson Overview - 3.7 Nested Conditionals

  • 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
time = 3
score = 1000
lives = 5
if time < 3 and score > 1000:
    lives = lives + 3
else:
    if time < 3 or score > 1000:
        lives = lives + 1
    else:
        lives = lives - 1

print(lives)
4
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!")
    }
}

Output: Please come to retake up to a 90 next week at tutorial!

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!!!")
    }
    }
}

Output: Looks great but lets see if we can cut down on sugar, we don't want diabetes!

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
meHours = 9

if 10 > meHours > 8:
    print("experienced with 90k")
else:
    if 10 < meHours:
        print("experienced with 150k")
    else:
        print("inexperienced with 50k")
inexperienced with 50k

Hacks Assignments:

cost = 30
stat = 1       
if stat < 1:
    print("this product is no good") 
else:
    if cost > 50 and stat == 1:
        print("this product is too expensive") 
    else:
        if 50 > cost > 25 and stat == 1:
            print("this is a regular product")
        else:
            print("this is a cheap product")
this is a regular product
questions = {
    "Which movie is a comedy?":["a. The King's Man","b. What's eating Gilbert Grape", "c. About Time", "d. Batman"], 
    "Which movie features 50 Cent?":["a. Shaft", "b. 8 Mile","c. Southpaw", "d. Batman"], 
    "Who is the best Batman?":["a. Christian Bale","b. Ben Affleck", "c. Robert Pattinson", "d. Kevin Conroy"]
    }

answers = {
    "Which movie is a comedy?":"c", 
    "Which movie features 50 Cent?":"c", 
    "Who is the best Batman?":"d"
    }

score = 0

print("Ready for a movie quiz?")

for q,a in questions.items():

    print(q)
    print(*a)

    inp = input("Enter Your Answer")

    if(answers.get(q)==inp):
        score = score +1
    else:
        print("SYNTAX ERROR...jk try again")
        break

print('Final Score:', score)  
Ready for a movie quiz?
Which movie is a comedy?
a. The King's Man b. What's eating Gilbert Grape c. About Time d. Batman
Which movie features 50 Cent?
a. Shaft b. 8 Mile c. Southpaw d. Batman
SYNTAX ERROR...jk try again
Final Score: 1
questions = {
    "Which movie is a comedy?":["a. The King's Man","b. What's eating Gilbert Grape", "c. About Time", "d. Batman"], 
    "Which movie features 50 Cent?":["a. Shaft", "b. 8 Mile","c. Southpaw", "d. Batman"], 
    "Who is the best Batman?":["a. Christian Bale","b. Ben Affleck", "c. Robert Pattinson", "d. Kevin Conroy"]
    }

answers = {
    "Which movie is a comedy?":"c", 
    "Which movie features 50 Cent?":"c", 
    "Who is the best Batman?":"d"
    }

score = 0

print("Ready for a movie quiz?")

for q,a in questions.items():

    print(q)
    print(*a)

    inp = input("Enter Your Answer")

    if(answers.get(q)==inp):
        score = score +1
    else:
        print("SYNTAX ERROR...jk try again")
        break

print('Final Score:', score)  
Ready for a movie quiz?
Which movie is a comedy?
a. The King's Man b. What's eating Gilbert Grape c. About Time d. Batman
Which movie features 50 Cent?
a. Shaft b. 8 Mile c. Southpaw d. Batman
Who is the best Batman?
a. Christian Bale b. Ben Affleck c. Robert Pattinson d. Kevin Conroy
Final Score: 3