06python_control
06python_control
of Flow
if Statements
if x == 3:
print “X equals 3.”
elif x == 2:
print “X equals 2.”
else:
print “X equals something else.”
print “This is outside the ‘if’.”
>>>
break and continue
• You can use the keyword break inside a
loop to leave the while loop entirely.
assert(number_of_players < 5)
For Loops
For Loops / List Comprehensions
• Python’s list comprehensions provide a
natural idiom that usually requires a for-loop in
other programming languages.
• As a result, Python code uses many fewer
for-loops
• Nevertheless, it’s important to learn about
for-loops.