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

  • Perl – Perl 5 vs. Perl 6

    Perl 6 gives the Perl 5 syntax a new paint job and introduces a genuine object system. Mike gives it a hands-on test based on a re-implementation of the old blackjack game.

  • CardStock

    CardStock provides a simple development environment for building a Python graphical application.

  • Hidden Meaning: Working with Microformats

    Programs aren’t as smart as humans when it comes to interpreting the meaning of web information. If you want to maximize your search rank, you might want to dress up your HTML documents with microformats and microdata.

  • 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.

  • Treasure Hunt

    A geolocation guessing game based on the popular Wordle evaluates a player's guesses based on the distance from and direction to the target location. Mike Schilli turns this concept into a desktop game in Go using the photos from his private collection.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News