Python

Switch in Python

There is no switch statement in Python, but there are equivalents depending on what you want to achieve. The best-known use of switch is to evaluate a value and avoid nesting multiple if statements.

Today I will explain it: how to make an equivalent switch in Python.

When you want to evaluate a sentence with multiple conditions

You can observe the most common case when you want to compare the value of an element with multiple conditions.

For example, give the user the option they want, and evaluate depending on it:

menu = """Welcome to the store
1 - New product
2 - See all
3 - Update
4 - Delete
5 - Exit
"""
choose = int(input("Choose an option:"))
switch choose:
 case 1:
  print("Insert")
  break
 case 2:
  print("See")
  break

That is a switch in a universe where Python incorporates it, however, it does not currently exist. Therefore, the code should be as follows:

menu = """Welcome to the store
1 - New product
2 - See all
3 - Update
4 - Delete
5 - Exit
"""
choose = int(input("Choose an option: "))
if choose is 1:
 print("Insert")
elif choose is 2:
 print("See all")
elif choose is 3:
 print("Update")
elif choose is 4:
 print("Delete")
elif choose is 5:
 print("Exit")
else:
 print("Default option here...")

Maybe at the beginning it is a bit cumbersome, but the equivalent to switch in Python are many if statements, with elif (which means else if) and else, which is the default case.

Use switch to get a value

Another use of switch in Python is when you want to get a value of a function from an argument that can have multiple values. For example, get the name of the day from its number; taking Sunday as 0, Monday as 1 and finally Saturday as 6.

For this, the first thing you think is to write a function as follows:

def get_day_name(number_of_day):
 if number_of_day is 0:
  return "Sunday"
 elif number_of_day is 1:
  return "Monday"
 # Here more ifs...
 else:
  return "Unknow"

It’s okay, and it uses the switch equivalent in Python, but it can be written in a better way:

def get_day_name(number_of_day):
 days = {
  0: "Sunday",
  1: "Monday",
  #Here the other days...
 }
 return days.get(number_of_day, "Unknow")

In this way, a dictionary and its get method are used, which receives the key of the value to be obtained (something like the index) and the default value in case the key does not exist.

Conclusion

In conclusion there is no switch statement in Python, but there are equivalents for all cases. In fact, you could say that switch is syntactic sugar for if.


I am available for hiring if you need help! I can help you with your project or homework feel free to contact me.
If you liked the post, show your appreciation by sharing it, or making a donation

parzibyte

Freelancer programmer ready to work with you. Web, mobile and desktop applications. PHP, Java, Go, Python, JavaScript, Kotlin and more :) https://parzibyte.me/

Entradas recientes

Receipt designer for thermal printers – Free and open source

In the last months I have been working on a ticket designer to print on…

12 months hace

JavaScript: store and read files with the Origin Private File System

In this post you will learn how to use the Origin Private File System with…

1 year hace

JavaScript: download file with fetch

In this post you will learn how to download a file in the background using…

1 year hace

SQLite3 with vanilla JavaScript and OPFS – Hello world

In this post I will show you how to use SQLite3 directly in the web…

1 year hace

Python Thermal Printing: A Comprehensive Guide for Printing on Thermal Printers

In this tutorial, we'll explore how to effortlessly print receipts, invoices, and tickets on a…

1 year hace

Image printing on Thermal printer

When printing receipts on thermal printers (ESC POS) sometimes it is needed to print images…

1 year hace

Esta web usa cookies.