Ad space (banner)
🐍Python Lessons
Lesson 15 / 69

The match Statement (Python's switch)

This lesson covers the basics of the match statement, available since Python 3.10, so you can write branching logic similar to a switch statement. It's written for anyone searching "Python match statement tutorial" or "Python switch statement".

The match statement, introduced in Python 3.10, branches based on a value — similar to a switch statement in other languages. You write match value: followed by a series of case pattern: blocks, and case _: means "everything else."

The sample code checks the value of day against case 1: through case 3:, falling through to the case _: block if none of them match. Earlier versions of Python had no dedicated branching syntax like this — chaining if/elif was the standard approach — but once you have many possible cases, a match statement is often clearer and easier to follow.

A common beginner mistake is not realizing match is a relatively new feature, only available from Python 3.10 onward. It may not work on older Python versions or in some execution environments like Pyodide, so check your Python version if it doesn't behave as expected.

Patterns can also include additional conditions, allowing more flexible branching than simple value matching. It's a syntax that feels familiar if you've used a switch statement in another language, and a good option whenever you want to tidy up a complex chain of conditions.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)