Python's Case of the Cleverly Disguised Switcheroo
Table of contents
A switch case statement is a programming construct found in many programming languages, though notably absent in Python. It provides a way to execute different blocks of code based on the value of a certain expression or variable. The expression is evaluated, and then the program "switches" to the corresponding block of code associated with the value of that expression.
Before Python 3.10, Python never had a switch or case statement like other programming languages such as C++ or Java so users used to use multiple if statements. The basic structure of a switch or case statement in Python can be seen in the below image.
Example
Declaring "It's hero time!" with a touch of nostalgia, let's revisit a childhood favorite: Ben 10. Now, we'll step into the realm of Python programming, employing switch and case statements to craft a basic Omnitrix-inspired structure.
The first line of the program, alien_name = input("What alien do you want to turn into? ")
, prompts the user to enter the name of an alien. The user's input is stored in the alien_name
variable.
The next line, match alien_name:
starts a switch case statement. The match
keyword takes the alien_name
variable as its argument. The case
statements then compare the value of alien_name
to the values specified in the case
statements. If the value of alien_name
matches the value of a case
statement, the code block associated with that case
statement is executed.
The case
statements in this program are for the ten original Ben 10 aliens: Heatblast, Wildmutt, Diamondhead, Four Arms, XLR8, Stinkfly, Grey Matter, Ripjaws, Ghostfreak, and Upgrade. If the user enters the name of one of these aliens, the corresponding case
statement will be executed and the power of that alien will be printed.
The case
statement for _
is a catch-all case. This case is executed if the value of alien_name
does not match any of the other case
statements. In this case, the message "I don't know that alien." is printed.
Try typing XLR8 and you will get the below result:
Concluding our exploration of switch and case statements, I extend my apologies for any discrepancy you might have observed in the appearance of the code editor. It came to my attention that my Microsoft Visual Studio instance lacked the required update to accommodate Python 3.10. As a result, I resorted to leveraging an online Python compiler known as Programiz. Should you wish to explore this tool, you can access it via the following link: https://www.programiz.com/python-programming/online-compiler/
In our forthcoming article, we shall delve into the realm of while loops.
Subscribe to my newsletter
Read articles from JMN directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
JMN
JMN
I'm JMN (Jaffery Mwangi Ndisho), and I'm thrilled to share my passion for data science and programming with you. With a background in business and IT, I've always been fascinated by the power of algorithms and their ability to transform data into insights. Through this blog, I hope to share my learning journey and practical knowledge with you, as well as explore new techniques and applications in the field. Join me on this journey and let's discover the world of data science and programming together!