Control Flow
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. [1]
This page provides syntax for some of the common control flow methods in Python. Each section includes an example to demonstrate the described methods
Use Cases and Syntax
Test if a specified expression is true or false
Short-circuit evaluation
Test if all of the conditions are true
x and y
Test if any of the conditions are true
x or y
Test if a condition is not true
not z
Conditional evaluation
if
statementif-else
if-elif-else
Ternary operator
true_value
if
conditionelse
false_value
Conditional Statements
Input:
x, y, z = 100, 200, 300
print(f"x = {x}, y = {y}, z = {z}")
# Test if x equals 100
if x == 100:
print(f"{x} equals 100")
# Test if y does not equal z
if y != z:
print(f"{y} does not equal {z}")
# Test multiple conditions
if x < y < z:
print(f"{y} is less than {z} and greater than {x}")
# Test multiple conditions using "and"
if x < y and x < z:
print(f"{x} is less than {y} and {z}")
# Test multiple conditions using "or"
if y < x or y < z:
print(f"{y} is less than {x} or {z]")
# if-else statement
if x < 100:
print(f"{x} less than 100")
else:
print(f"{x} is equal to or greater than 100")
# Same logic as above but using the ternary operator
print(f"{x} less than 100 again" if x < 100 else f"{x} equal to or greater than 100 again")
# if-elif-else statement
if y < 100:
print(f"{y} is less than 100")
elif y < 200:
print(f"{y} is less than 200")
elif y < 300:
print(f"{y} is less than 300")
else:
print(f"{y} is greater than or equal to 300")
Output:
x = 100, y = 200, z = 300
100 equals 100
200 does not equal 300
200 is less than 300 and greater than 100
100 is less than 200 and 300
200 is less than 100 or 300
100 is equal to or greater than 100
100 equal to or greater than 100 again
200 is less than 300
Loops
Repeat a block of code a specified number of times or until some condition is met
while
loopfor
loopUse
break
to terminate loop
Input:
# Demonstrates use of loops
i = 1
# while loop for incrementing i by 1 from 1 to 3
while i <= 3:
print(f"while: {i}")
i +=1
# for loop
for j in range(1,4):
print(f"for: {j}")
for j in range(1,4):
print(f"for again: {j}")
# nested for loop
for j in range(1,4):
for k in range(1,4):
print(f"nested for: {j} * {k} = {j*k}")
Output:
while: 1
while: 2
while: 3
for: 1
for: 2
for: 3
for again: 1
for again: 2
for again: 3
nested for: 1 * 1 = 1
nested for: 1 * 2 = 2
nested for: 1 * 3 = 3
nested for: 2 * 1 = 2
nested for: 2 * 2 = 4
nested for: 2 * 3 = 6
nested for: 3 * 1 = 3
nested for: 3 * 2 = 6
nested for: 3 * 3 = 9
Comparison Operators and Functions
Equality
x == y
Inequality
x != y
Less than
x < y
Less than or equal to
x <= y
Greater than
x > y
Greater than or equal to
x >= y
Input:
# Demonstrate comparison operators
# Assign values to variables using parallel assignment
c1, c2, c3, c4 = 25, 50, 75, 50
print(f"c1 = {c1}, c2 = {c2}, c3 = {c3}, c4 = {c4}")
# Output results of different comparison operations
# Testing equality
print(f" c1 = c3 is {c1 == c3}")
print(f" c2 = c4 is {c2 == c4}")
# Changing values using abbreviated assignment operators
c1 *= 3 # shorthand for c1 = c1 * 3
c4 += 1 # shorthand for c4 = c4 + 1
print(f"c1 = {c1}, c2 = {c2}, c3 = {c3}, c4 = {c4}")
# Testing less than and greater than
print(f" c1 < c2 is {c1 < c2}")
print(f" c4 <= c2 is {c4 < c2}")
print(f" c1 > c2 is {c1 > c2}")
print(f" c3 >= c2 is {c3 >= c2}")
Output:
c1 = 25, c2 = 50, c3 = 75, c4 = 50
c1 = c3 is False
c2 = c4 is True
c1 = 75, c2 = 50, c3 = 75, c4 = 51
c1 < c2 is False
c4 <= c2 is False
c1 > c2 is True
c3 >= c2 is True
Resources
Python Documentation: Control Flow
Python Wiki: For Loops
W3 Schools: Python For Loops
W3 Schools: Python Conditionals and If Statements
Last updated