About 99,100 results
Open links in new tab
  1. What is the Python equivalent for a case/switch statement?

    Jul 14, 2012 · Python 3.10.0 provides an official syntactic equivalent, making the submitted answers not the optimal solutions anymore! In this SO post I try to cover everything you might …

  2. Replacements for switch statement in Python? - Stack Overflow

    Sep 13, 2008 · Python 3.10 (2021) introduced the match-case statement, which provides a first-class implementation of a "switch" for Python. For example: For example: def f(x): match x: …

  3. Using a dictionary as a switch statement in Python

    Feb 23, 2014 · I'm trying to make a simple calculator in Python, using a dictionary. Here's my code: def default(): print "Incorrect input!" def add(a, b): print a+b def sub(a, b): print a-b def m...

  4. How to write a nested switch case in python? - Stack Overflow

    Nov 21, 2019 · I am working on the python project in which I needed to implement nested switch. This answer helped me a lot. Here is working example based on that answer. The example is …

  5. how can I combine a switch-case and regex in Python

    Feb 14, 2011 · This will take a string with a number and a time unit in it, e.g. "15d" is 15 days. The regex splits the 15 and the d up, then a switch statement is performed on the units. This code …

  6. python - How to create a switch case with the cases being intervals ...

    In Python 3.10 an explicit switch statement was introduced - match.Although it doesn't support direct containment checking, so we'll have to exploit the guard feature:

  7. How can I write a switch case statement with functions in Python ...

    Jan 29, 2020 · Python function are object that can be passed as parameter as long as you don't put parenthesis after their name (it would make them function call). You can then call the …

  8. menu - Switch-case statement in Python - Stack Overflow

    Mar 15, 2021 · x = 3 for case in switch(x): if case(1): # do something break if case(2,4): # do some other thing break if case(3): # do something else break else: # deal with other values of x Or …

  9. What is the syntactical equivalent to switch/case in Python?

    Mar 30, 2021 · In this example, the first two cases would be executed because the first case falls through. If there was no break statement inside the second case, all cases, including the …

  10. How to use switch in Python? - Stack Overflow

    Sep 20, 2015 · A second, and much better approach would be to make use of Python's dictionaries as Shrutarshi Basu wrote about on his website. He makes use of Python's …

Refresh