Conditions and Branching Flashcards
Conditional statements - comparison operators
equal: == not equal: != greater than: > less than: < greater than or equal to: >= less than or equal to: <=
Comparison operators example
Use Equality sign to compare the strings
“ACDC”==”Michael Jackson”
“ACDC” == “Michael Jackson”
False
Branching - IF statements
If statement example
age = 19 #age = 18
#expression that can be true or false if age > 18:
#within an indent, we have the expression that is run if the condition is true print("you can enter" )
#The statements after the if statement will run regardless if the condition is true or false print("move on")
Else statements
The else statement runs a block of code if none of the conditions are True before this else statement. # Else statement example age = 18 # age = 19 if age > 18: print("you can enter" ) else: print("go see Meat Loaf" )
print(“move on”)
go see Meat Loaf
move on
elif statement
The elif statement, short for else if, allows us to check additional conditions if the condition statements before it are False. # Elif statment example age = 18 if age > 18: print("you can enter" ) elif age == 18: print("go see Pink Floyd") else: print("go see Meat Loaf" )
print(“move on”)
go see Pink Floyd
move on
Logical operators
Logical operators allow you to combine or modify conditions.
and
or
not
Logical operator - “AND”
album_year = 1980
if(album_year > 1979) and (album_year < 1990):
print (“Album year was in between 1980 and 1989”)
print(“”)
print(“Do Stuff..”)
Album year was in between 1980 and 1989
Do Stuff..
logical operator - “OR”
album_year = 1990
if(album_year < 1980) or (album_year > 1989):
print (“Album was not made in the 1980’s”)
else:
print(“The Album was made in the 1980’s “)
Album was not made in the 1980’s
logical operator - “NOT”
album_year = 1983
if not (album_year == '1984'): print ("Album year is not 1984")
Album year is not 1984