How to Use Python Match Case Statements
ฝัง
- เผยแพร่เมื่อ 4 มิ.ย. 2023
- A 'match case' statement in Python allows you to check a variable or function value and perform different things based on the result!
It is a good replacement for applications that use a lot of repetitive if-else statements and is similar to match case syntax of other C-based programming languages!
Feel free to leave a comment with any questions or to say what you'd like to see on the channel next, and don't forget to leave a like on the video and subscribe to the channell!
Thanks for watching and goodluck with your code!
Check out my Patreon if you'd like to become a super supporter of the channel! / lemastertech - วิทยาศาสตร์และเทคโนโลยี
Looks to be a more advanced version of the "switch" statement from the C languages.
Wonder why it took them so long to add it in but I'm glad it's available - much more intuitive.
Agreed! That was my first impression too and I sometimes forget to use it because I’ve gotten so used to the if elif chains but it’s a nice hack for sure 😎
Very well done tutorial! Did you say, however, that this is only available in Python 3.10 or later? I just upgraded to Python 3.12, so wanting to make sure I can use it.
Hello and thank you! Yes according to pythons official release notes this is a new feature as of python 3.10!! And it is available in all versions going forward now :)
Can we use match case statement for matching a string in python , I am struggling with this in python ❤.
Hi it is not really meant for that it’s really for numerical comparison, there are other efficient ways to check for string matching!
Haha, the thing is, I was wondering how to do this and you made a tutorial on it. Now that is luck.🎮
Very cool!! Glad it could be helpful! :)
Thanks forms india❤
You’re very welcome!! Thanks for watching
how about something like this
switch (DrawingMode)
{
case Drawing.TurtleSnowflake:
drawable = new TurtleSnowflake();
break;
case Drawing.TurtleGosper:
drawable = new TurtleGosper();
break;
Hi, all I have seen the case switching for in Python is off of numerical values - this looks like you want to switch off a variables status so I would suggest actually just using if else if statements for this one!!
Thanks from Pakistan
You’re welcome thanks for watching!!