Learn Python MATCH-CASE STATEMENTS in 5 minutes! 📆

preview_player
Показать описание
A match statement takes an expression and compares its value to successive patterns given as one or more case blocks. Specifically, pattern matching operates by:

1. using data with type and shape (the subject)
2. evaluating the subject in the match statement
3. comparing the subject with each pattern in a case statement from top to bottom until a match is confirmed.
4. executing the action associated with the pattern of the confirmed match

If an exact match is not confirmed, the last case, a wildcard _, if provided, will be used as the matching case. If an exact match is not confirmed and a wildcard case does not exist, the entire match block is a no-op.

# Match-case statement (switch): An alternative to using many 'elif' statements
# Execute some code if a value matches a 'case'
# Benefits: cleaner and syntax is more readable

def is_weekend(day):
match day:
case "Saturday" | "Sunday":
return True
case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
return False
case _:
return False

print(is_weekend("Monday"))
Рекомендации по теме
Комментарии
Автор

# Match-case statement (switch): An alternative to using many 'elif' statements
# Execute some code if a value matches a 'case'
# Benefits: cleaner and syntax is more readable

def is_weekend(day):
match day:
case "Saturday" | "Sunday":
return True
case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
return False
case _:
return False

print(is_weekend("Monday"))

BroCodez
Автор

JavaScript:
switch()
case :

c++:
switch()
case :

Literally every programming language:
switch()
case :

Python: mATcH

Thomas-dpeb
Автор

I just pulled up to say thanks for being the goat, had to say it before u hit 2 mil.
I’ve been using your videos to learn JS and C++, and hopefully some more in the future. 🍕

NatNay
Автор

Your explanations are always so clear and engaging; it's a gift to learn from you. ❤❤

misbahmoulvi
Автор

bro is the real chad
never disappoint us

Rahib-gvgd
Автор

GREAT JOB BRO ALMOST LIKE SQITCH CASE STATEMENTS IN C++ AND JAVA . I donot even aware of this feature in pythpn thanks a lot ...🎉

ashimsarkar
Автор

Wow i just thought it doesnt have a switch case, ... Really amazing

bie
Автор

<*_> This is my seal. I have watched the entire video, understood it, and I can explain it in my own words, thus I have gained knowledge. This is my seal. <_*>

piotrmazgaj
Автор

can we do like if else and write a program on even or odd

ECHOISBACK
Автор

Is it faster than if, elif and else statements?

cookieIg-nb
Автор

it's pretty good but i prefer to use if else, thank you Bro

reincarnationofthelightking
Автор

Bro is the best! please only once reply me!

TorabekRaufov-px
Автор

4:26 can you also you "or" instead of "|"

AmarMustafi-yj