Control Flow Flashcards
Comparison Operators
==
!=
<
>
<=
>=
Boolean Operators
or - +
and - *
not # not True
if Statement
name = “Antony”
if name == “Debora”:
print(“Hi Debora!”)
elif name == “George”:
print(“Hi George!”)
else:
print(“Who are you?”)
Ternary Conditional Operator
<expression1> if <condition> else <expression2>
'kid' if age < 18 else 'adult'
</expression2></condition></expression1>
Switch Case (3.10 and above)
response_code = 502
match response_code:
case 200 | 201:
print(“OK”)
case 500 | 502:
print(“Internal Server Error”)
case str():
print(“Code is a string”)
case _:
print(“Invalid Code”)
While Loop and control loop Statements
spam = 0
while spam < 5:
spam = spam + 1
if spam == 3:
continue
elif spam == 5
break
print(“Hello, world.”)
For loop and range
pets = [“bella,” ‘milo”, “loki”]
for pet in pets:
print(pet)
for i in range(0, 10, 2): # range(start, stop, step)
print(i)
for i in range(5, -1, -1):
print(i) # 5 4 3 2 1 0
For Else statement
else выполняется только тогда, когда весь цикл был выполнен
for in [1, 2, 3, 4, 5]:
if i == 3:
break
else:
print(“only executed when no item is equal to 3”)
Ending a Program
import sys
sys.exit()