0:00
Hey guys, welcome back to another
0:02
interesting video. So it is all about if
0:05
you have tired of those long if l if and
0:09
else they just keep growing and growing
0:11
like seriously where does it end? If
0:14
you're in that kind of notion then
0:15
you're going to love what we are going
0:17
to talk about today. It's all about the
0:19
match case statement and it's Python
0:22
modern way of doing pattern matching.
0:24
Like think of it as Python's version of
0:27
switch case but a smarter. So let's dive
0:30
into what is it, how it works, and how
0:33
you can use it in your own projects to
0:35
write cleaner and cooler code.
0:44
So it's all about match case in
0:52
Okay. So what is match case?
0:56
It was introduced in Python 3.10
0:59
version. So if you're using an older
1:01
version, just update it. Okay. Think of
1:04
match case like a decision maker. It
1:06
looks at a value and matches it against
1:09
different patterns. If it finds a match,
1:12
it runs the code inside that block.
1:15
Sounds a bit like if, l if, and else,
1:18
right? But wait, it's more powerful. It
1:21
can match complex patterns like it could
1:23
be list, dictionaries and even class
1:25
instances. It's like pattern matching
1:31
Okay, let's start simple and you can
1:33
check this out here or we can even do
1:36
this. I want you to type the code with
1:39
me so that you will learn practically
1:42
rather than theoretically.
1:45
So that is the reason.
1:48
So type with me. So just create this
1:54
and start with match and then case
1:59
and then if the case is 10 then print
2:54
Yep. So here's the function. It is
2:56
actually checking the value of x. So if
2:59
the x is 10, then it's 10. That's the
3:02
output. If x is 20, then it'll be 20. If
3:06
nothing else matched, it will just do
3:07
this. like it will say it's neither 20
3:10
nor neither 10 nor 20. So here the
3:13
function this is what it's going on. If
3:16
you want I will call this function
3:24
and give value 10 here or 20.
3:35
See it's 10. It's showing the output.
3:37
What if it's 30? It's not 10 and it's
3:43
So then it will show you the output like
3:45
it's neither 10 nor 20. Okay,
3:50
it's pretty neat, right? So now if your
3:54
app is like for example if there is an
3:56
application and you want to welcome your
3:59
users and maybe this can help you in
4:02
giving a clean and more readable logic
4:06
like you don't need 10
4:09
and you just have to give
4:54
Here you should give Anna.
4:58
And here you should give John.
5:04
You just have to print
5:17
And if the case is nothing
5:36
and that's it. Now when you call this
5:49
And you have to give it in strings.
6:00
So it will show you hello Anna. It's
6:06
And here it'll be name. Actually my bad.
6:10
I made something wrong. Okay, let's try
6:13
again. And if you give the name Anna,
6:17
it says hello Anna. And when you give
6:32
It says hello John. And when you give
6:41
see hello, it's nothing.
6:48
So if your app is welcoming users then
6:51
this could be a clean readable logic.
6:53
You don't need 10 and then you just have
6:55
to give if name equal to X and you have
6:59
to write all those lines and you don't
7:01
have to do that anymore. It's clean.
7:06
So this is what we are talking about.
7:07
And now let's talk about the conditions
7:13
So for this all you have to do is
7:21
create function or check number.
7:26
I want you to do this with me because
7:28
when you do then only you will remember
7:29
what you what you did rather than just
7:32
looking at the screen. I hope you're
7:34
doing with me. So match number and then
7:38
if the case is it could be either 10 or
7:41
it could be 20 or it could be 30
7:45
whatever the number it have to print
8:00
I want you to use f strings as well and
8:03
we have already made how to use f
8:05
strings and how to represent strings
8:11
So value 10 recognized or value 20
8:14
recognized value 30 recognized.
8:21
And if there's nothing
8:36
and now when you call this so here we
8:40
are using or operator so this is the
8:42
pipe symbol that we use for or operator
8:45
it'll be right besides the brackets I
8:48
could say maybe above the enter button.
8:51
So you just have to shift and press that
8:53
button and then you'll get this or
8:57
So see this pipe symbol. This is like
8:59
saying if number or if it is 10 or 20 or
9:05
30. It's like grouping multiple values
9:08
together. It's really very useful when
9:11
you have multiple conditions and if you
9:13
want any one of those conditions should
9:16
be positive. So at the time this will be
9:18
the best operator to use when you're
9:20
checking the conditions. Run this
9:22
function. So check number and what if 10
9:33
And when you give 50 which is none of
9:43
So this is how we'll use our operator
9:45
with the match case. And now
9:50
we can also in fact add conditions with
9:58
like if you see here so we can do this.
10:09
or you can just copy paste instead of
10:14
And here you're going to check check
10:21
and match number and if the case is then
10:25
and here in just give if
10:32
mod two if number is divided by two and
10:37
when you get the remainder zero then
10:40
that at that time we'll say the number
10:42
is even. So if the number is divisible
10:44
by two then we will say it's even. So
10:46
that's what we are checking here. So we
10:49
are checking if number mod 2 equal to
10:52
zero and you can just print the answer
11:03
Okay. And if you just want to keep case
11:06
10 then here it will print
11:31
And then if there's none of it
11:50
So you can add if condition inside a
11:53
case. Yeah, we can do that. It makes so
11:55
much more expressive and you can check
11:57
the values and add custom logic inside
12:00
it. Like for example, if we run this
12:25
or when you give any other number it
12:35
let's try with matching lists and tpples
12:44
def we are creating the function and let
12:47
me just clear this out
12:50
process data and we are sending the data
13:02
Let's create two parameters
13:13
So here we are actually created we gave
13:16
the list to the case and you just have
13:47
And you can do the same thing
14:10
There's nothing print
14:27
So now when you give
14:34
and when you give X and Y
14:45
Maybe two and three.
14:55
So list with two elements two and three
14:57
that's what it will give. Like for
14:59
example when you give four or six or
15:10
then it will show invalid format.
15:24
so now let's see for dictionaries. So
15:28
all you have to do is div describe
15:36
You just have to give person here
15:42
and all you have to do is match person
15:50
we are create we are giving a
15:57
so this is a dictionary. So name will be
16:00
the key and this name will be the value
16:04
and then age will be the key and this
16:08
age will be the value
16:14
and then you just have to print
17:07
just this have to give f
17:33
So here you basically created a person.
17:37
So you're trying to get the data from
17:40
the person's data. It could be name and
17:41
age according to this case. And you're
17:44
checking if you have name and age then
17:48
you're printing that that person is this
17:51
much years old. And if you just have the
17:53
name then you're just you know saying
17:56
that that person's age is unknown. And
17:58
if you don't have name and age and if
18:00
you're just calling just by giving any
18:03
random data then it will say invalid
18:09
like for example describe person when
18:11
you call this man and you want name to
18:25
lucky name is the key and value is lucky
18:35
So this will be the key. So when you
18:38
just give this and run so it will show
18:43
lucky is 21 years old and when you just
18:46
give name and no age.
18:54
So Ly's age is unknown. So that is what
19:00
So Python checks keys and even extracts
19:03
values just for you. And you don't need
19:05
to write if name in person. You don't
19:08
have to write all the syntax. Just
19:13
Now let's try with matching class
19:27
So here all you have to do is just
19:35
And we'll just try to pass for now. And
19:39
we'll just create one more class circle.
19:44
And we'll use this shape here.
19:49
And let's initialize it. So def in it.
19:56
And we don't need this.
20:26
Now let's try class.
20:29
Let's create rectangle
20:33
and we will use that shape
20:49
and for rectangle gra width and height.
21:33
actually we use it when we are writing
21:35
multiple lines in the one in the single
21:38
line multiple lines of code in a single
21:40
line and we don't need it here. So I
21:43
thought of writing everything in one
21:44
line but it's okay. Let's make it simple
21:46
even for you to understand what's going
21:52
Okay. Now let's create a function
22:00
and we'll give shape
23:33
And when there's nothing,
23:49
So now this is powerful. You can match
23:54
class types and pull out attributes all
24:00
using this in a game or maybe a GUI
24:03
application or when processing API
24:05
responses. It's super handy.
24:08
So what we actually did is we created a
24:12
basic class base class which is shape
24:15
and we created circle and we used this
24:18
shape here and for circle all we need is
24:21
radius. So we just give radius here and
24:24
for rectangle we need width and height.
24:27
So we just captured width and height
24:29
here and now we created a function
24:32
called describe shape and if it's circle
24:36
if the if user gives you radius then
24:39
it'll be a circle. So it will print
24:41
circle with radius whatever the
24:43
centimeters that you give and if it's
24:46
rectangle then it will print rectangle
24:48
width into height and then it will
24:51
basically give you unknown shape when
24:53
user gives something else. Let's just
25:04
So let's create objects and run this
25:06
function. Like for example, circle equal
25:09
to call circle circle class with maybe
25:19
And let's also create rectangle.
25:33
maybe four and six as parameters
25:42
will be the shape itself.
25:48
And now when you call this describe
25:54
describe shape right
26:06
describe shape of rectangle
26:10
and describe shape of
26:25
And we have to give this outside of the
26:40
So now when you run this
26:50
Because here you give class. Let's just
26:53
give R. Let's retry.
26:59
So So here if you check when we have run
27:02
this function. So, so all we did is we
27:06
created a class shape which is the base
27:09
and then we created the circle and we
27:11
have inherited shape here and for circle
27:14
we need radius. I'm just trying to
27:16
explain it once again so it'll be easy
27:18
for you to understand. And then for
27:20
rectangle we need width and height. So
27:22
we have inherited shape again. So we
27:25
created width and height. And then now
27:28
we created a class called describe
27:30
shape. And if it is circle then it will
27:35
print the radius R. If it is rectangle
27:38
then it will print rectangle width into
27:40
height. So now we just created the
27:43
objects for circle with the circle class
27:47
rectangle with rectangle class. For
27:49
rectangle we need width and height. For
27:50
circle we need radius. And unknown is
27:53
just the actual shape itself.
27:56
So when you called this describe shape
27:59
now describe shape will go here and then
28:02
it will go here and it will see that it
28:06
needs circle. So then it will jump here
28:08
and it will check the shape and it is
28:11
circle and then that's how it will print
28:14
the circle with radius something like
28:18
So this is how everything exactly works
28:22
and save this code and you can also you
28:25
know create a file named shapes py and
28:28
you can just run it and then that's how
28:30
you can do it. So this is why you should
28:33
use match case statements because it is
28:36
really powerful here and you can match
28:38
class types and pull out the attributes
28:40
all in one go and you can also do this
28:43
imagine you can do this using a game and
28:45
you can work on a ji application or when
28:48
processing API responses which is super
28:50
handy I could say. So why you should use
28:53
match case because it has cleaner
28:55
syntax. It is more readable than long if
28:58
chains and it also supports data
29:00
structures like list, dictionaries and
29:03
even classes and it will also helps you
29:05
avoid the bugs when you forget to handle
29:08
a case. So if you see here we have a
29:12
quick FAQs which are frequently asked
29:14
questions. You can just go through there
29:16
and you can also go through this blog
29:18
and it's really handy for you guys to
29:20
even revise and go through the code. I
29:24
will I will also keep this thing in the
29:25
description. All right, that's our crash
29:29
course on match case and Python. If this
29:31
helped clear things up, don't forget to
29:33
like the video, subscribe for more
29:34
Python tips, and comment down below if
29:36
you have used match case before or if
29:38
you're planning to do it now. And I'll
29:41
see you in the next one. Until then,
29:43
keep coding and keep learning. Bye.