Lecture 1 - Conditionals Flashcards
Allows the programmer, to allow your program to make decisions based upon certain conditions.
Conditionals
Used to ask mathematical questions
Operators
Denotes “greater than or equal to.”
> =
Denotes “less than or equal to.”
<=
Denotes “equals”, used to compare variables.
==
Denotes “not equal to.
!=
This kind of statement uses bool or boolean values (true or false) to decide whether or not to execute.
If
This kind of statement allows the program to make fewer decisions.
First, the if statement is evaluated. If this statement is found to be true, all the ____ statements will not be run at all.
Elif
Elif Syntax
if x < y:
print(“x is less than y”)
elif x > y:
print(“x is greater than y”)
elif x == y:
print(“x is equal to y”)
Statements that catches anything which isn’t caught by the preceding conditions.
Else
Else Syntax
if b > a:
print(“b is greater than a”)
elif a == b:
print(“a and b are equal”)
else:
print(“a is greater than b”)
This conditional allows your program to decide between one or more alternatives.
Or
Or Syntax
if x < y or x > y:
print(“x is not equal to y”)
else:
print(“x is equal to y”)
Is a logical operator, and is used to combine conditional statements
And
Statements that allows you to more easily control the flow of your programs by executing certain parts of code if conditions (or cases) are met.
Match Case
Match Case Syntax
> > > command = ‘Hello, World!’
> > > match command:
… case ‘Hello, World!’:
… print(‘Hello to you too!’)
… case ‘Goodbye, World!’:
… print(‘See you later’)
… case other:
… print(‘No match found’)
Hello to you too!
This will match with any input, resulting in similar behavior as an else statement.
_ Symbol
_ Symbol Syntax
match name:
case “Harry”:
print(“Gryffindor”)
case “Hermione”:
print(“Gryffindor”)
case “Ron”:
print(“Gryffindor”)
case “Draco”:
print(“Slytherin”)
case _:
print(“Who?”)
A match statement compares the value following the match keyword with each of the values following the ____ keywords
Case
Case Syntax
match name:
case “Harry” | “Hermione” | “Ron”:
print(“Gryffindor”)
case “Draco”:
print(“Slytherin”)
case _:
print(“Who?”)
Much like the or keyword, this allows us to check for multiple values in the same case statement.
Single | bar
Single | Syntax
match name:
case “Harry” | “Hermione” | “Ron”:
print(“Gryffindor”)