Skip to content

Control Flow

Python includes multiple constructs and statements for controlling flow.

if condition

condition = True
if condition is True:
    print("True")
elif condition is False:
    print("False")
else:
    print(condition)
True

result = "True" if condition is True else "False"
print(result)
True

while loop

counter = 0

while counter < 3:
    print(counter)
    counter += 1
0
1
2

for loop

for x in range(3):
    print(x)
0
1
2

for x in range(1,3):
    print(x)
1
2

for x in range(3):
    if x == 1:
        continue
    elif x == 3:
        break
    else:
        print(x)
0
2