The new Python match

Diversions

© Lead Image © saracorso, 123rf.com

© Lead Image © saracorso, 123rf.com

Article from Issue 249/2021
Author(s):

Exploring the new Python match statement, Python's implementation of switch/case.

If you've decided to learn Python [1] and have any experience with other programming languages, you'll quickly notice that the ubiquitous switch statement is nowhere to be found. Soon, though, that will no longer be the case (sort of). Python 3.10 is slated to be released in October 2021 and includes the new match command [2] [3].

Switcheroo

The function of switch is akin to trying to find a particular office in an office building. As you walk down the hallway, you look at each door to see if it displays the number or name in which you are interested. When you find it, you stop searching and go inside. In C (and indeed many other languages) switch allows you to compare a value against a set of others (Listing 1).

The switch starts the compare operation, with the value you want to check passed in. The sets of curly braces then contain your case statements.

[...]

Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Practical Python in Linux

    We’ll introduce you to Python, an easy-to-learn scripting language, and also help you get started creating your own practical Python scripts.

  • Escape Room Puzzle

    A digital puzzle presents a challenge for young people in an escape room.

  • Python 3

    What do Python 2.x programmers need to know about Python 3?

  • Parrot

    Parrot is an all-in-one tool for developing and executing new programming languages. Perl 6 runs on Parrot; chances are your language can run on it, too.

  • A backtracking algorithm tries its hand at the bridges of Königsberg

    Pretty much any computer science lecture about graph theory covers the "Seven Bridges of Königsberg" problem. Mike Schilli puts a Python script to work on a solution, but finds that a new bridge must be built.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News