Home
Q&A
Education
Technology
Credit
General
Health
Insurance
Questions
Ask a Question
Replacements for switch statement in Python?
Home
Education
Replacements for switch statement in Python?
asked
Jan 24, 2022
in
Education
by
JackTerrance
I want to write a program which returns different values for different input,it can be easily done using switch case but Python doesn’t seem to have switch cases so, can anyone suggest me some alternative? Select the correct answer from above options
switch-statement
python-faq
python
1
Answer
0
votes
answered
Jan 24, 2022
by
JackTerrance
Best answer
@Anvi, To replace a switch statement in Python use the following code: def f(x): return { 'p': 9, 'q': 5, }[x] Alternatively, you can also use: def f(x): return { 'p': 9, 'q': 5 }.get(x, 0)
Related questions
0
votes
Q: What's the canonical way to check for type in Python?
How can I check a given object is of a given type? How can I check that a object inherit a given type? There's ... that it's a string? Select the correct answer from above options...
asked
Jan 22, 2022
in
Education
by
JackTerrance
string
python
python-faq
0
votes
Q: How do I lowercase a string in Python?
How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
python
python-faq
string
uppercase
0
votes
Q: How do I get the number of elements in a list in Python?
How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
python
python-faq
list
0
votes
Q: Understanding Python super() with __init__() methods
I am a learner and just want to know the basic concept of super(). Here in the given code I want to know ... () ChildOne() ChildTwo() Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
python
python-faq
oop
super
0
votes
Q: How do I use raw_input in Python 3
I want to freeze the dos pop-up, I already tried raw-input as mentioned in my study material of Python 2.6 and ... (3**200) raw_input() Select the correct answer from above options...
asked
Jan 23, 2022
in
Education
by
JackTerrance
python-faq
python
raw_input
0
votes
Q: Python split list into n chunks
In a list with range(1,16) and I am trying to divide this list in some fixed numbe n . Let's assume n=7 ... uneven, How can I do that? Select the correct answer from above options...
asked
Jan 23, 2022
in
Education
by
JackTerrance
python
python-faq
chunks
0
votes
Q: How do I connect to a MySQL Database in Python?
I want to use python to connect to a MySQL database, how can I do that? Select the correct answer from above options...
asked
Jan 23, 2022
in
Education
by
JackTerrance
mysql
python
python-faq
0
votes
Q: Extracting extension from filename in Python
I want to extract an extension from a filename in Python, How can I do that? Select the correct answer from above options...
asked
Jan 23, 2022
in
Education
by
JackTerrance
filenames
python-faq
python
extansion
0
votes
Q: How do I trim whitespace from a Python string?
In Python, How to remove trailing and leading whitespace from str ? For instance check this: " Hey" --> "Hey" " ... "Harsh has a dog" Select the correct answer from above options...
asked
Jan 23, 2022
in
Education
by
JackTerrance
python-faq
python
trim
0
votes
Q: Find all files in a directory with extension .txt in Python
Can someone tell me how to find all the files in directory with .txt extension in Python? Select the correct answer from above options...
asked
Jan 23, 2022
in
Education
by
JackTerrance
file-io
python-faq
python
txt
0
votes
Q: How do I check what version of Python is running my script?
How to check which Python version is interpreting my script? Select the correct answer from above options...
asked
Jan 23, 2022
in
Education
by
JackTerrance
python-faq
python
interpreter
script
0
votes
Q: What is the Python 3 equivalent of “python -m SimpleHTTPServer”
Can someone tell me a python -m SimpleHTTPServer equivalent Python 3? Select the correct answer from above options...
asked
Jan 23, 2022
in
Education
by
JackTerrance
httpserver
python
python-3
python-faq
sipplehttpserver
0
votes
Q: null object in Python?
Can someone tell me how to refer a null object in Python? Select the correct answer from above options...
asked
Jan 22, 2022
in
Education
by
JackTerrance
null
python
python-faq
0
votes
Q: How can I represent an 'Enum' in Python?
Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked
Jan 22, 2022
in
Education
by
JackTerrance
python
python-faq
python-3
x
enums
0
votes
Q: Converting integer to string in Python?
In Python how can I convert an integer to a string? I am trying this: r = 16 r.str() I am trying to ... but there's nothing like that. Select the correct answer from above options...
asked
Jan 22, 2022
in
Education
by
JackTerrance
string
python
python-faq
integer
...