Structural Pattern Matching in Python: Not Your Average Switch-Case

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn about the python feature introduced in Python 3.10 called structural pattern matching so let us get right into [Music] ited all right so structural pattern matching is not very new it was introduced in Python 3.10 I think the current version is python 3.12 so it's already two versions old and a lot of you guys might already know this feature and might have already used this feature but I also think that a lot of you guys don't really know what this feature is about because a lot of people use it as a switch case in Python and it can be used like that and it does work like that a little bit but it has another uh purpose and another use case which we're going to talk about in this video today the feature we're talking about is the match case features so what you can do in Python is you can have some value and the value can be for example a string let's say it's hello and then I can use something uh called structural pattern matching I can say match and then I can say I want to match the value and I want to have certain cases for this value so I can say uh the value can be uh hello for example this is one value that the variable value can have and in this case I want to do something so I want to print for example uh the message hello world or whatever then I might have another case if this value has the value uh exit for example I can react to it by exiting this the script and you can see that this works in a very similar way to just saying okay if value equals hello do something so pass L if if the value is something else uh so exit for example do something else and then I can have a default case uh where I do something else and I can do this here as well by just saying case and then underscore or some variable name it doesn't really matter you can call this uh whatever uh and then you can do something else here uh usually what you do is you use an underscore to indicate that this is the default case if nothing um none of these works here none of these works here so we're going to just go to the default case so um that works pretty similar to a basic if else statement and I can run this here now and you can see it says hello world and I can uh change this to exit and you will see it just exits the script and I can change this to uh come on to something else and when I run this H okay I'm passing so let's print unknown command in this case it prints unknown command so what's so fancy about structural pattern matching now it gets interesting when you're actually doing pattern matching right now we're just asking for values but we can also ask for patterns so let's say I have a different use case now let's say I pass a tuple or a list containing division values so let's say I want to pass a list of numbers or a tuple of numbers and I want to divide them what I can do is I can say division numbers is equal and I can define a tuple here with values like five and two and then I want to take these values and perform to division five divided by two now this is a trivial use case so this is not necessarily the most useful uh thing to do here but what we can do now is we can just say match division numbers and I can Define the case that I have some values X and Y and the values X and Y if I get them what I want to do is I want to print um x / Y is equal to x / y so I can do it just like that and if I get something else I can just Define a default case uh and I can say something like invalid input or something like that so when I run this right now I get 2.5 as a result now if I do something else if I for example just pass 10 I'm going going to get invalid input because that's not a tall of X and Y also if I say 5 2 and three I will get invalid input because that would be a tuple with three values in it um so that right here now is one specific case now I'm want I might want to Define another case where I say okay if Y is zero I want to handle it differently so what I can do is I can define a case up here and say okay I can have a tuple X and the second value is zero in this case I want say cannot divide by Zer so you can see I just defined a pattern that I want to match x and z um in that case divide by zero you can see um oh I still have the invalid input so now if I just run the ordinary Division I get 2.5 if I set this to zero I get cannot divide by zero if I do that without this case I'm just going to get the exception because it's going to recognize this is X and Y but Y is zero so you cannot divide by zero and you're going to get the exception or the error zero division error so that's a very simple thing what we can also do is we can Define and by the way this is one thing that's important it's important which case comes first so the value or or the the combination here 5 Z matches both cases so 5 is X and Zer but 5 Z is also X and Y which means that if this case comes first it's going to recognize it first as a match uh as a pattern match because it is a pattern match zero is also Y and now I'm going to get the zero division error so you want to have these um cases before that uh but what I can do now also is I can say what if I enter more numbers and I can define a case and I can say okay if I have x y and other values and how I do that is I say asterisk other for example uh what I want to do here now is I want to say okay I'm going to say division result is going to be equal to x / Y and then what I want to do is I want to say for uh number in other what I want to do is I want to say division result divided equals uh number and then I want to just print the result so division result I can do that and now what I can do is I can say 522 for example and I will get uh 1.25 and I can continue to do that with more values and you can see that the division keeps going now the problem is again I can now introduce a zero here and I will get an exception so what I can do is I can add conditions I can say XY other if0 not in other and Y is not equal to zero that is a pattern match right here I have two values and then some more values and if zero is not an other I can actually also Define this just as X and other but yeah we can do it like this as well just so you see it works but you can also just say x and ask to risk other and then you can say if zero not and other that would also be fine and then you just divide uh X by all the values this also possibility but this is also something that you can do you can add conditions now I can copy this and I can say okay if I have this pattern match um and I can say Y is equal to zero um or zero is part in other then I can also again just print cannot divide by zero and then you can see cannot divide by zero but if I change this to two there you go we get to division so that is the power of pattern matching and the good thing is you can also uh actually this is this is probably um this is important here in this case at least you can use an underscore to basically indicate that it doesn't really matter what x is because I don't need to call this x here if I'm not using X I can just say this is um some value because we're not using it anywhere uh if there there's some value and then I have y and other and these conditions are met then do this um in this case here I'm using X and in this case here I'm using X as well so I'm going to keep them um but yeah this is this is how you do the pattern matching in this case now one thing that you can also do with that is you can check for types now I'm just showing a couple of use cases here if I have some value and the value is let's say a string hello um what I can do is I can say match value and then I can use types to match the type of the value this can be useful if you have an input to a function or a parameter to a function and since python is dynamically typed you don't know what the type is going to be so you can check for the different types and how you do that is you can say case string and you need to call the string uh function so the type casting function and then you can just say okay uh it's a string and the string is whatever the value is and then you can say something like uh what if it's an integer and you can use a pipe symbol to say or a float so if it's a numeric value what you can do is you can just for example take the value and square it this is one thing that you can do with that [Music] um yeah so so that would work and what we can do now is we can go or actually we don't have to do anything we can just uh run this and you can see string hello if I change this to 20 it's going to be 400 if I change it to 90.3 it's going to be that now if I change this now to if I add a case for default I can say unsupported type or something like this uh and then if I change this for example to a list it's going to say unsupported type so this is also something that you can do now finally I want to show you a use case that is quite interesting we can have a command line interface so I can say the command is the user input so I can just ask for a command um and then what I can do is I can allow for different patterns of command so I can say Okay simple case is I have the uh I match the command now but I want to split the command why do I want to split the command to allow for different positional arguments so I split the command on white spaces and then what I do is I say Okay match command split and um then I can define a simple case for example the command could just be hello now since we're splitting I need to pass this as a list here I need to Define it as a list so it's going to be the one argument hello and in this case I can print for example hell world but I also want to allow for parameters for certain types of uh commands so I can say for example there is a command say followed by a message and what I want to do in this case is I want to print um I want to print the message that was passed here um yeah so that also works or we can also maybe say asterisk mess messages this would also be um a thing and then we can do something like I have the command add and I take the two values X and Y here and or basically we can do it even more generalized we can say add and then I can say numbers so asterisk numbers and then what I can do is I can say print the sum of the following list comprehension int n for n in numbers then of course uh we need to call this some uh and then I can have a default case which says uh unknown command so then we have this interface now I can say hello I can say hello again I can type something else unknown command I can say add 10 20 30 I can get 60 and then I can say say something for example test and I'm going to say test all right so this is structural pattern matching in Python a very useful feature so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Info
Channel: NeuralNine
Views: 8,670
Rating: undefined out of 5
Keywords: structural pattern matching, match case, switch case, python, python 3.10, python match case, python switch case, python structural pattern matching, python 3.10 match case, python 3.10 structural pattern matching
Id: s2sSsSH2clE
Channel Id: undefined
Length: 12min 32sec (752 seconds)
Published: Sat Dec 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.