Skip to content

Instantly share code, notes, and snippets.

@funkycoder
Created December 11, 2020 08:05
Show Gist options
  • Save funkycoder/0a3a14d3bf2629642bacf244a91fecd2 to your computer and use it in GitHub Desktop.
Save funkycoder/0a3a14d3bf2629642bacf244a91fecd2 to your computer and use it in GitHub Desktop.
Switch statement in python
# Python doesnt have switch case statement. Hereis the workaround
def grading(score):
switcher = {
0: "Zero",
1: "One",
2: "Two",
3: "Three",
4: "Four",
}
# get() method of dictionary data type returns
# value of passed argument if it is present
# in dictionary otherwise second argument will
# be assigned as default value of passed argument
return switcher.get(score, "Five and above")
print(grading(5))
>>>Five and above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment