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

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

PREVIOUS NEXT
Code Example
Python :: get column number in dataframe pandas 
Python :: jupyter upload folder 
Python :: pytube progress bar example 
Python :: left click pyautogui 
Python :: frequency spectrum signal python 
Python :: make averages on python 
Python :: pd count how many item occurs in another column 
Python :: python ascii code to string 
Python :: pathlib get extension 
Python :: python bold text in terminal 
Python :: python get os 
Python :: how do i print a list line by line in python 
Python :: reverse geocode python 
Python :: concatenate dataframes 
Python :: how to convert gb to mb in python 
Python :: flatten a 2d list 
Python :: remove spaces from string python 
Python :: copy a dict in python 
Python :: save a torch tensor 
Python :: python numpy array replace nan with string 
Python :: numpy random.permutation 
Python :: pycairo 
Python :: ImportError: No module named colored 
Python :: python timestamp to datetime 
Python :: how to read xlsx file in jupyter notebook 
Python :: How to copy any text using python 
Python :: python: measure time code 
Python :: python append n numbers to list 
Python :: pi python 
Python :: read excel file in python 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =