Notes

  • Boolean is a denoting system of algebraic notation to represent logical propositions
    • Booleans are only either true or false
  • Relational operators is the mathematical relationship between two variable
    • Determines output by whether statement is true
  • Logical operators include: not, and, or
  • NOT displays opposite of data
    • does not affect the variablre
  • AND evaluates two conditions together and determines if both conditions are met
  • OR allows function to run only if one of the conditions is met
  • Conditionals allow for the expression of algorithms that utilize selection without a programming language
  • Conditional Statement / If-Statement: A statement that affects the sequence of control by executing certain statements depending on the value of a boolean
  • Nested conditional statements consist of conditional statements within conditional statements
    • i.e. An else if inside of another else if
  • The first if statement can represent if two coditions are true. The first else if can represent if only one or the other is true. The last else if represents if neither of the conditions are true

Hacks

Logical operators

Define and exemplify

NOT:returns the opposite of the inputted value; true displays false, false displays true

print("It is cold:")

isCold = True
result = not(isCold)
print(result)

# as data, isCold is True, but prints as False because of  not() 
It is cold:
False

AND: returns true only if both conditions of the statement are met

temp = 58
rain = True
if temp <= 60 and rain == True:
    print("Bad weather")
    
# both of the conditions are met so the code runs. if only one condition was met, the code woud not run.
Bad weather

OR: returns true if at least one of the conditions are met

rainy = False
cold = True
if rainy == True or cold == True:
    print("Bad weather")

# one of the conditions is met, so the code runs. if both conditions were met, the code would also run.
Bad weather

Conditionals

Definitions with examples

Selection:the section of code only runs if a condition is met

temp = 70

if temp < 60:
    print("brrr it's cold")
elif temp > 80:
    print("whew it's hot")
else:
    print("good weather!")

# code runs based on the condition that is met
good weather!

Algorithm: a set of steps that completes a task

x = 5
y = x + 2
result = x + y
print(result)

# steps are carried out in order for the code to print the result 
12

Conditional Statement / If-Statement: program executing depending on whether a condition is true or false

x = 30
if x < 50:
    print ("Condition is true")

# code runs if condition is met
Condition is true

Binary conditional logic

coffee = input("Would you like coffee?")
if coffee == "yes":
    coffee = True
elif coffee == "no":
    coffee == False
else: 
    print("Sorry, we can't serve you")

sugar = input("Would you like sugar?")
if sugar == "yes":
    sugar = True
elif sugar == "no":
    sugar == False
else: 
    print("Sorry, we can't serve you")

if coffee == True:
    print("Here's your coffee")
elif sugar == True:
    print("Here is a sugar packet. Are you sure you don't want coffee?")
elif coffee == True & sugar == True:
    print("Here's your coffee")
else:
    print("Sorry, we can't serve you")
Here is a sugar packet. Are you sure you don't want coffee?

Nested Conditionals

Flowcharts and thier resulting code

#1

temp = 70

if temp < 60:
    print("Brrr it's cold, stay warm!")
else:
    if temp > 80:
        print("It's hot out, let's go to the beach!")
    else:
        print("Good weather! Go enjoy the day!")
Good weather! Go enjoy the day!

#2

print("Is x or y greater?")
x = int(input("Enter integer for x"))
y = int(input("Enter integer for y"))
if x==y:
    print("x (",x,") and y(",y,") are equal")
else:
    if x > y:
        print("x(",x,") is greater")
    elif y > x:
        print("y(",y,") is greater")
Is x or y greater?
y( 2 ) is greater

#3

print("How much does this item cost?")
item = 10
sale = True
halfoff = False

if sale == True:
    if halfoff == True: 
        item *= 0.5
        print(item)
    else: 
        item *= 0.75
        print(item)
else:
    print(item)
How much does this item cost?
7.5

Code with 4 conditional statements

print("The parking rate is as follows: \n Less than one hour: Free \n 1-2 hours: $5 \n 2-3 hours: $8 \n 3-4 hours: $10 \n 4+ hours: $12")

time = float(input("How many hours have you parked at this garage?"))
print("How many hours have you parked at this garage?")
print(time, "hours costs:")

if time < 1 :
    print("Free")
else:
    if time >= 1 and time < 2 :
        print("$5")
    elif time >= 2 and time < 3 :
        print("$8")
    elif time >= 3 and time < 4 :
        print("$10")
    else:
        print("$12")

print("Have a good day!")
The parking rate is as follows: 
 Less than one hour: Free 
 1-2 hours: $5 
 2-3 hours: $8 
 3-4 hours: $10 
 4+ hours: $12
How many hours have you parked at this garage?
4.0 hours costs:
$12
Have a good day!

Code for recommending 3 classes with 2 conditions

print("What classes should you take?")

stem = input("Do you enjoy STEM?")
if stem == "yes":
    mathsci = input("Cool! Are you more into the math or science side of STEM?")
    if mathsci == "math":
        print("You should take algebra, geometry, and calculus!")
    elif mathsci== "science":
        print("You should take biology, chemistry, and physics!")
elif stem == "no":
    hmnts = input("Do you enjoy humanities?")
    if hmnts == "yes":
        print("You should take history, english, and an art course!")
    elif hmnts == "no":
        print("You should just try PE, I guess!")
What classes should you take?
You should take history, english, and an art course!