0:00
a new statement called may statement has been introduced in Python 3.10 so if you don't
0:14
know about it this video is definitely gonna work for you I'm gonna teach you about may
0:19
statement in this video if you're coming from cc++ background probably you know about switch
0:25
statement well this one is just similar to that one but anyways doesn't matter you're
0:29
gonna learn it right here so bear with me I'm gonna teach you this with a very simple
0:34
example so I've simply initialized a variable with its value 5 and I'm gonna simply use
0:41
the match keyword and pass the expression here followed by a colon and inside of that
0:46
we're gonna define some cases each case will hold a value and expression will match with
0:51
each case so whenever the expression matches with any of the case then that corresponding
1:00
case will be executed its body basically will be executed so suppose the first case right
1:04
here is 10 and I simply want to print 10 alphabetically so that's it this is your first case you can
1:11
have a single case or multiple case it doesn't matter so suppose I have a next case and it's
1:17
holding a one I just want to print out one here so this is the second case right here
1:23
then I have a third case and simply it's holding suppose 50 it's it's holding 50 and I just
1:32
want to print out 50 that's it so this is it all the cases will be matched with the
1:39
given expression and whenever whenever a case matches with the expression then that
1:47
case would come into play and that that case will be executed and if none of the cages
1:53
if none of the cases matches then simply we can have a default case which is simply represented
2:02
by an underscore and simply you can type something like this is a default case right
2:11
this is default case so that's it so if none of the case matches then this default case
2:19
must be executed it's somewhat like a fail structure if you know about it so it's basically
2:24
like that but let's run this code I want to keep this its value 10 so I would like to
2:31
run this code and see what I'm going to get in output so guys as you can see down here
2:36
I got 10 because the case value is 10 so basically the expression matches with this case so that's
2:44
why this case was executed and the rest of the cases were being skipped because the value
2:52
because you know the expression doesn't match it match with other cases so that's why it
2:59
picked up this case the first case so if I replace its value with one so it's gonna print
3:06
sorry I didn't save it so it's gonna print something else yes one so now the second case
3:14
just came into play because the expression is holding one right here it's one right here and
3:21
the case is also holding one so that's why it printed out one here it executed this case and
3:27
rest of the cases were being skipped so let's keep it 50 and let me run it again and I got
3:36
this guess right here right now so this is how it works so if you keep x value something else
3:43
that is not defined inside of those cases suppose hundred if you keep x value something else except
3:50
10 150 then it's actually going to get into the default case so if I run it I get this one going
4:01
do some practice if you got any questions you can ask me in the comment section and that was
4:06
all for now see you next time