Control flow in Python refers to the order in which the program's code executes. The control flow of a program is regulated by conditional statements, loops, and function calls. Python provides various constructs to manage the flow, making the code flexible and adaptable to different scenarios and logic.
Conditional statements allow the program to execute certain pieces of code depending on whether a specific condition is true or false.
- The
if
statement is used to test a condition and execute a block of code if the condition is true.
x = 10
if x > 5:
print("x is greater than 5")
elif
andelse
Statementselif
(short for else if) is used to test additional conditions if the previous conditions were false.else
provides a block of code that runs if all previous conditions are false.
if x < 5:
print("x is less than 5")
elif x == 5:
print("x is 5")
else:
print("x is greater than 5")
The match
statement introduced in Python 3.10 provides functionality similar to the switch/case statements found in other languages but with more advanced pattern matching capabilities. It allows matching not only single values but also complex data structures, class types, and more.
The match
statement allows you to compare a value against several patterns and execute the block of code corresponding to the first matching pattern.
Example:
def http_status(status):
match status:
case 200:
return "OK"
case 404:
return "Not Found"
case _:
return "Other"
You can match multiple values using the |
operator (which signifies "or
") or by matching against an iterable like a list.
match status:
case 200 | 201:
return "Success"
case 404:
return "Not Found"
case _:
return "Other"
match status:
case [200, 201]:
return "Success"
case [404]:
return "Not Found"
case _:
return "Other"
The wildcard _
is used as a default case to match any value. It's similar to the default case in other languages' switch/case constructs.
Example:
match status:
case 200:
return "OK"
case 404:
return "Not Found"
case _:
return "Other" # Default case
match works well with Enums, providing a clean way to handle enumerated values.
from enum import Enum, auto
class Color(Enum):
RED = auto()
GREEN = auto()
BLUE = auto()
color = Color.RED
match color:
case Color.RED:
print("Red")
case Color.GREEN:
print("Green")
case Color.BLUE:
print("Blue")
The match statement supports deep matching within data structures, allowing you to match patterns within nested sequences or objects.
Example:
match point:
case (x, y) if x == y:
return "Point is on the y = x line"
case (x, y):
return f"Point({x}, {y})"
The match statement can also be used to match types of objects, which is particularly useful in dealing with different classes or data structures.
response_code = "300"
match response_code:
case int():
print('Code is a number')
case str():
print('Code is a string')
case _:
print('Code is neither a string nor a number')
class Error:
code: int
message: str
match response:
case Error(code=404, message=msg):
return f"Not Found: {msg}"
case Error(code=code, message=_):
return f"Error {code}"
case _:
return "Success"
When using pattern matching with custom classes, you can match based on just one attribute of the object. You don't have to match against all attributes of the object.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
match point:
case Point(x=0, y=_):
return "Point is on the Y-axis"
case Point(x=x, y=_):
return f"Point is at x={x}"
case _:
return "Unknown"