Python Codes

‘switch’ Case in Python

[Note: This tutorials is for beginners only]

Once you start programming in Python, very soon you encounter with the scenario that there is no direct support of switch case. The community has various alternatives for the purpose namely,

  • Using dictionaries
  • Using lambdas
  • Using Exceptions
  • and many others!

For a beginner it is faster and easier to go with if-else construct to get the switch case functionality. Below code rests the case. Syntax adheres to Python 3.

# Dummy function_one
def function_one():
    print("You called Function One")
# Dummy function_two
def function_two():
    print("You called Function Two")

# Defining the switch case equivalent in an infinite while loop,
# until the user decides to break
def main():
    while True:
    print("Enter your choice\n")
    choice = input()

    if choice == '1':
    elif choice == '2':
        print("Program Terminates")

# Call the main()


The code can also be obtained from github from the following link:
switch Case in Python Using if-else


Let me Know What you Think!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s