All TalkersCode Topics

Follow TalkersCode On Social Media

Switch Statement In Python

Last Updated : Aug 3, 2023

Switch Statement In Python

In this article we will show you the solution of switch statement in python, a switch case statement is a type of selection control system used in computer programming to allow a variable's value to alter the control flow of a program's execution.

The 'if' statement in any programming language is analogous to the switch case statement.

In any program, the "if..else" statement is replaced by the "switch case" statement when it is known that the options will be integer or character literals.

When programming, there are instances when we must run a certain piece of code based on another circumstance.

A switch statement can be used to test a variable against a range of potential values and then carry out various instructions based on the outcome. Adding a switch statement to a program usually improves it.

We will now discuss the idea of how to switch statements in python with an example.

Step By Step Guide On Switch Statement In Python :-

switch (monthOfYear) {
    case 1:
        printf(“%s”, January);
    case 2:
        printf(“%s”, February);
    case 3:
        printf(“%s”, March);
    case 4:
        printf(“%s”, April);
    case 5:
        printf(“%s”, May);
    case 6:
        printf(“%s”, June);
    case 7:
        printf(“%s”, July);
   case 8:
        printf(“%s”, August);
    case 9:
        printf(“%s”, September);
    case 10:
        printf(“%s”, October);
    case 11:
        printf(“%s”, November);
    case 12:
        printf(“%s”, December);
        printf(“Incorrect month”);
  1. The switch statement in the code above prints the name of the month dependent on the value of the "monthOfYear" variable.
  2. The value of "monthOfYear" is created as the input for a switch statement.
  3. The value 1, which stands for January, is the first case in the switch statement. The string "January" is printed using the printf function.
  4. After the code for each case has been executed, the break statement is used to end the switch statement.
  5. The switch statement's subsequent case is for the value 2, which stands for February. The word "February" is printed using the printf function.
  6. A different case is handled in this manner for every month of the year.
  7. Any values of "monthOfYear" that are not covered by the preceding circumstances are utilized in the default case. The string "Incorrect month" is printed out in this instance using the printf function.
  8. The switch statement is finished, and then the program advances to the following line of code.

Conclusion :-

As a result, we have successfully learned how to switch statements in python with an example.

You have learnt about switch-case statements in this blog, along with their alternatives and how to utilize them.

Since Python lacks an internal switch case method, as previously mentioned, you may always utilize these substitutes to improve the performance and readability of your code.

I hope this article on switch statement in python helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials