👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
Unlike Java or C# , Python does not have a built-in switch statement. This means you can not evaluate a switch expression without having to write your own code that mimics a statement "switch ... case" .
In this guide we discuss how to write a "switch ... case" in Python and follow two examples of a bogus switch statement so you can learn how to write one one in your code.
Switch statements: an update
A switch statement allows you to select one of a series of blocks of code to execute. They are a way of evaluating multiple expressions in a program.
A switch statement works by evaluating a switch statement and comparing the result of this instruction with the values of instructions "box" . If a match is found, the respective code block is executed. Otherwise, nothing will happen.
Optionally, a "default" keyword is used to execute a block of code if no statement matching a particular expression is found.
Python Switch Statement: Return individual values ​​
Let’s write a program that converts a numeric value to a day of the week.
To begin with, ask a user to enter a number that they want to convert to a day written:
We convert the value the user enters in our program to an integer. We’ll see why this is necessary later in the tutorial.