• Boolean: a data type. it has two possible values.
    • TRUE
    • FALSE
    • can only be 1 or 0
  • Operators
    • a = b - equal to
    • a (not equal to symbol) b - not equal to
    • a > b - greater than
    • a < b - less than
    • a (is greater than or equal to symbol) b - greater than or equal to
    • a (is less than or equal to symbol) b - less than or equal to EXERCISE: Use a boolean expression to determine if the average grade is above an 80 and print the result (True or False).
grade1 = 90
grade2 = 65
grade3 = 60
grade4 = 75
grade5 = 95
averagegrade = (grade1 + grade2 + grade3 + grade4 + grade5)/5
print(averagegrade >= 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!

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

  • Selection: uses a condition
    • result - TRUE or FALSE
  • Algorithm: set of instructions that accomplish a task
x = 20
y = 10
if x > y:
    print("x is greater than y")
x is greater than y
x = 9
y = 10
if x > y:
    print("x is greater than y")
else:
    print("x is not greater than y")
x is not 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(200)
else:
    print(sum)
200

Lesson Overview - 3.7 Nested Conditionals

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!")
    }
}
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!!!")
    }
    }
}
Looks great but lets see if we can cut down on sugar, we don't want diabetes!

NOTE: || means or in javascript

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
hours = 10
if (hours >= 8)
{
    if (hours < 10)
    {
        console.log ("Your salary is", 90000)
        console.log ("You are experienced!")
    }
    if (hours >= 10)
    {
        console.log ("Your salary is", 150000)
        console.log ("You are super experienced!")
    }
}
else
{
    console.log ("Your salary is", 50000)
    console.log ("You are not experienced!")
}
Your salary is 15000
You are super experienced!

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 above 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"
expiry = "not expired"
cost = 25
if (expiry == "expired")
{
    console.log ("This product is no good!")
}
else
{
    if (cost >= 50)
    {
        console.log ("This product is too expensive!")
    }
    if (cost >= 25 && cost < 50)
    {
        console.log ("This is a regular product!")
    }
    if (cost < 25)
    {
        console.log ("This is a cheap product!")
    }
}
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
import getpass, sys
    
def question_with_response(prompt):
    print("Question: " + prompt)
    msg = input(prompt)
    return msg

questions = 5
correct = 0

print('Hello, ' + getpass.getuser())
print("You will be asked " + str(questions) + " questions.")
print("Are you ready for this Basketball Quiz? Let's start!")

rsp = question_with_response("Who is the leader for all time points scored in the NBA? A. John Stockton B. LeBron James C. Kareem Abdul Jabbar D. Michael Jordan")
if rsp == "Kareem Abdul Jabbar" or rsp == "C":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Who is the leader for all time 3-pointers made in the NBA? A. Stephen Curry B. Shaquille O'Neal C. Ray Allen D. Klay Thompson")
if rsp == "Stephen Curry" or rsp == "A":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Who is the leader for all time steals in the NBA? A. Damian Lillard B. Chris Paul C. LeBron James D. John Stockton")
if rsp == "John Stockton" or rsp == "D":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Who is the leader for all time assists in the NBA? A. Chris Paul B. John Stockton C. Michael Jordan D. Kobe Bryant")
if rsp == "John Stockton" or rsp == "B":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Who is the leader for all time rebounds in the NBA? A. Kobe Bryant B. Wilt Chanberlain C. Draymond Green D. Shaquille O'Neal")
if rsp == "Wilt Chamberlain" or rsp == "B":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))
Hello, soham
You will be asked 5 questions.
Are you ready for this Basketball Quiz? Let's start!
Question: Who is the leader for all time points scored in the NBA? A. John Stockton B. LeBron James C. Kareem Abdul Jabbar D. Michael Jordan
Kareem Abdul Jabbar is correct!
Question: Who is the leader for all time 3-pointers made in the NBA? A. Stephen Curry B. Shaquille O'Neal C. Ray Allen D. Klay Thompson
A is correct!
Question: Who is the leader for all time steals in the NBA? A. Damian Lillard B. Chris Paul C. LeBron James D. John Stockton
C is incorrect!
Question: Who is the leader for all time assists in the NBA? A. Chris Paul B. John Stockton C. Michael Jordan D. Kobe Bryant
Michael Jordan is incorrect!
Question: Who is the leader for all time rebounds in the NBA? A. Kobe Bryant B. Wilt Chanberlain C. Draymond Green D. Shaquille O'Neal
B is correct!
soham you scored 3/5