Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

case in python

def http_error(status):
    match status:
        case 400:
            return "Bad request"
        case 404:
            return "Not found"
        case 418:
            return "I'm a teapot"
        case _:
            return "Something's wrong with the internet"
Comment

python switch statement

def f(x):
    match x:
        case 'a':
            return 1
        case 'b':
            return 2
        case _:
            return 0   # 0 is the default case if x is not found
Comment

python switch

# Python 3.10.0 +
match subject:
    case <pattern_1>:
        <action_1>
    case <pattern_2>:
        <action_2>
    case <pattern_3>:
        <action_3>
    case _:
        <action_wildcard>
Comment

python switch case

match points:
    case []:
        print("No points in the list.")
    case [Point(0, 0)]:
        print("The origin is the only point in the list.")
    case [Point(x, y)]:
        print(f"A single point {x}, {y} is in the list.")
    case [Point(0, y1), Point(0, y2)]:
        print(f"Two points on the Y axis at {y1}, {y2} are in the list.")
    case _:
        print("Something else is found in the list.")
Comment

python switch

>>> def week(i):
        switcher={
                0:'Sunday',
                1:'Monday',
                2:'Tuesday',
                3:'Wednesday',
                4:'Thursday',
                5:'Friday',
                6:'Saturday'
             }
         return switcher.get(i,"Invalid day of week")<div class="open_grepper_editor" title="Edit & Save To Grepper"></div>
Comment

python switch item

def operations(letter):
    switch={
       'a': "It is a vowel",
       'e': "It is a vowel",
       'i': "It is a vowel",
       'o': "It is a vowel",
       'u': "It is a vowel",
       }
    return switch.get(letter,"It is a not a vowel")		# disregard 2nd parameter
operations('a')
Comment

case python

lang = input("What's the programming language you want to learn? ")

match lang:
    case "JavaScript":
        print("You can become a web developer.")

    case "Python":
        print("You can become a Data Scientist")

    case "PHP":
        print("You can become a backend developer")
    
    case "Solidity":
        print("You can become a Blockchain developer")

    case "Java":
        print("You can become a mobile app developer")
    case _:
        print("The language doesn't matter, what matters is solving problems.")
Comment

Python Switch case statement Using classes

class PythonSwitch:
    def day(self, dayOfWeek):

        default = "Incorrect day"

        return getattr(self, 'case_' + str(dayOfWeek), lambda: default)()

    def case_1(self):
        return "monday"

def case_2(self):
        return "tuesday"

 def case_3(self):
        return "wednesday"

   
def case_4(self):
       return "thursday"

  def case_5(self):
        return "friday"

   def case_7(self):
        return "saturday"
    
    def case_6(self):
        return "sunday"

my_switch = PythonSwitch()

print (my_switch.day(1))

print (my_switch.day(3))
Comment

PREVIOUS NEXT
Code Example
Python :: quote_from_bytes() expected bytes 
Python :: difference between local and global variable in python 
Python :: legend ax matplotlib 
Python :: how to get ping from computer IN PYTHON 
Python :: dense in keras 
Python :: get path and name of file for open() 
Python :: conditional subsetting python 
Python :: append two dfs 
Python :: blender python get current filename 
Python :: 2 plater die game in python 
Python :: debugging python 
Python :: django search pagination 
Python :: get value of bit in integer python 
Python :: how to looks like a hacker 
Python :: how to wait for loading icon to disappear from the page using selenium python 
Python :: slice python 
Python :: python defaultdict default value 
Python :: django count all objects 
Python :: python all option 
Python :: command to install python3.6 on mac os 
Python :: reduce dataframe merge 
Python :: handlebars python 
Python :: decimal hour to hour minute python 
Python :: sum of list of numbers 
Python :: compare string python 
Python :: python menentukan genap ganjil 
Python :: what is an object in python 
Python :: install requests-html with conda 
Python :: python toupper 
Python :: get last x elements of list python 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =