Why there is no switch statement in python? - tccicomputercoaching.com

Page 1

Why there is no switch statement in python? tccicomputercoaching.com Switch case is a powerful decision-making construct commonly used in modular programming. Unlike other programming languages, Python doesn’t provide a switch case instrument over the self.

However, it has many other constructs like a dictionary, lambda function, and classes to write a custom implementation of the Python switch case statement. One example of Switch in Python : Here, we define a function week() to tell us which day a certain day of the week is. A switcher is a dictionary that performs this mapping.

As you can see, for values other than the ones we mention in the switcher, it prints out “Invalid day of week”. This is because we tell it to do so using the get() method of a dictionary.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.