Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

match case in Python

>>> command = 'Hello, World!'
>>> match command:
...     case 'Hello, World!':
...         print('Hello to you too!')
...     case 'Goodbye, World!':
...         print('See you later')
...     case other:
...         print('No match found')
 
Hello to you too!
Comment

python match case example

match subject:
    case <pattern_1>:
        <action_1>
    case <pattern_2>:
        <action_2>
    case <pattern_3>:
        <action_3>
    case _:
        <action_wildcard>
Comment

python match case

x = 4

# x is the variable to match
match x:

    # if x is 0
    case 0:
        print("x is zero")

    # case with if-condition
    case 4 if x % 2 == 0:
        print("x % 2 == 0 and case is 4")

    # Empty case with if-condition
    case _ if x < 10:
        print("x is < 10")

    # default case(will only be matched if the above cases were not matched)
    # so it is basically just an else:
    case _:
        print(x)



# WHAT THE CODE WOULD LOOK LIKE IF IT DIDN'T USE MATCH/CASE

x = 4

if x == 0:
	print("x is zero")
elif x == 4 and x % 2 == 0:
	print("x % 2 == 0 and case is 4")
elif x < 10:
	print("x is < 10")
else:
	print(x)
Comment

PREVIOUS NEXT
Code Example
Python :: pca in python 
Python :: fast api template syntax 
Python :: sanke in python 
Python :: python qr scanner 
Python :: count TRUE in DF 
Python :: alphabet 
Python :: net way to print 2d array 
Python :: python string contains substring ignore case 
Python :: unicodedata no accent 
Python :: python singleton class 
Python :: django login required as admin 
Python :: remove rows from dataframe 
Python :: discord python application bot 
Python :: python print bytes 
Python :: pandas python3 only 
Python :: get ip address 
Python :: pandas use dict to transform entries 
Python :: How to clone or copy a list in python 
Python :: stemming words python 
Python :: sum up list python 
Python :: refer dataframe with row number and column name 
Python :: datetime convert python 
Python :: check for null values in rows pyspark 
Python :: private key 
Python :: Python list function tutorial 
Python :: scrapy with selenium 
Python :: Python RegEx Subn – re.subn() Syntax 
Python :: convert 12 hour into 24 hour time 
Python :: handling exceptions 
Python :: python print new line 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =