• 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

EXERSICE: Use a boolean expression to determine if the average grade is above an 80 and print the result (True or False).

allGrades = [90, 65, 60, 75, 95]

# used the sum and length functions, divided them
allGradesAve = sum(allGrades)/len(allGrades)

# if and else statements to determine if average is less than or greater than 80
if allGradesAve > 80: 
    print ("True. The average grade is above an 80.")
else: 
    print ("False. The average grade is below an 80.")
False. The average grade is below an 80.

Relational operators Example

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

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

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

num1 = 1
num2 = 2
sum = num1 + num2

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

Analyzing Code

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: || = 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 = 30
sal = ""
experienced = True
# using operators to determine the salary
if (hours >= 10):
    sal = "150k"
elif (hours >= 8):
    sal = "90k"
else:
    sal = "50k"
    experienced = False
# printing the final statement 
print ('The person has a salary of:' , sal,'and has experience:' , experienced)
The person has a salary of: 150k and has experience: True

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"
expired = False
cost = 50

if(expired):
    print('this product is no good')
else:
    if (cost > 50):
        print('this product is too expensive')
    elif (cost < 25):
        print('this is a cheap product')
    else:
        print('this is a regular 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

Book Knowlege Quiz

# used a dictionary with a question as the key and a list of multiple choice as values for the quiz
quiz = {"Who is the author of It Ends With Us":["a. Holly Jackson","b. Colleen Hoover", "c. Lisa Jewell", "d. Jennifer Barnes"], "Who is the author of Percy Jackson":["a. JK Rowling", "b. Holly Jackson","c. Rick Riordan", "d. Colleen Hoover"]
, "When was a Good Girl's Guide to Murder Published":["a. 2019","b. 2018", "c. 2020", "d. 2021"], "When was It Starts with Us Published":["a. 2019","b. 2020", "c. 2021", "d. 2022"]}

# for the answers, used the question as the key and the multiple choice letter as the value 
answers = {"Who is the author of It Ends With Us":"b", "Who is the author of Percy Jackson":"c", "When was a Good Girl's Guide to Murder Published":"a", "When was It Starts with Us Published":"d"}
count = 0

# iterating through the dictonary's keys and values 
for k,v in quiz.items():

# printing the key and it's values 
    print(k)
    print(*v, sep = '\n')

# taking the user input for the answer, entering ab
    user_input = input("Enter Your Answer")

# comparing that with the answer's value 
    if(answers.get(k)==user_input):

        # incrementing the count 
     count = count +1

# printing the user's score
print('You got', count, 'correct answers')    
Who is the author of It Ends With Us
a. Holly Jackson
b. Colleen Hoover
c. Lisa Jewell
d. Jennifer Barnes
Who is the author of Percy Jackson
a. JK Rowling
b. Holly Jackson
c. Rick Riordan
d. Colleen Hoover
When was a Good Girl's Guide to Murder Published
a. 2019
b. 2018
c. 2020
d. 2021
When was It Starts with Us Published
a. 2019
b. 2020
c. 2021
d. 2022
You got 4 correct answers