3.5-3.7 Hacks
logical operators, conditionals
- not
- Shows/does the opposite of the data
- mostly used for true false
- ex. if you assign red to false, and then you have a variablle called result that you assign the value not(red) to, then it will output true
- code example:
isRed = False
green = not(isRed)
print(green)
- and
- evaluates two conditions together and if both conditions are met and only if both conditions are met, will the final result be shown
- ex. if flower = red and flower = green
print("christmas flower")
the flower has to be both red and green for the algorithm to print chirstmas flower, if only red, christmas flower will not be printed - code example:
flower = 50
if flower >= 1 and flower <= 100:
print("bouquet")
if flower >= 100:
print("garden")
- or
- only one of the two conditons has to be met
- ex. if temp >=100 or temp <= 20
print ("stay home") - code example:
temp = 101
if temp >= 100 or temp <= 20:
print("stay home")
evaluates actions in the program (true or false)
Selection: the result of this block of a selection depends on if a condition is true or false
Algorithm: instructions/steps to accomplish a certain task, or solve a different problem
Conditional Statement: executes certain statements depending on if it is true or false
print("please enter a number")
x = int(input())
if x % 2 == 0:
# if x is divisible by 2 with no remainder
print(x, "is even")
else:
# if x is not divisible by 2 with no remainder
print(x, "is odd")
print("are you happy?")
reply = input("yes or no")
if reply == "no":
print("why be sad when you can be glad")
if reply == "yes":
print("good for you")
print("what holiday do you celebrate?")
reply = input("Christmas or Hanukkah")
if reply == "Hanukkah":
print("Happy hanukkah!")
elif reply == "Christmas":
print("Merry Chirstmas!")
else:
print("Happy Holidays!")
print("are you a swiftie?")
reply = input("yes or no")
if reply == "yes":
print("slay!")
else:
print("oh.")
print("Have you watched criminal minds?")
reply1 = input("yes or no?")
if reply1 == "yes":
print("Who's your favorite character")
reply2 = input("favorite character")
if reply2 == "Emily":
print("yay")
else:
print("oh")
else:
print("you should")
Stem or no stem?
print("are you interested in stem?")
reply1 = input("yes or no")
if reply1 == "yes":
list1 = ["APCSP", "AP Biology", "AP Statistics"]
print("you should take", list1)
else:
list2 = ["AP Human Geography", "AP World", "AP English Language"]
print("you should take", list2)