How to Use Python Match Case Statements

LeMaster Tech
183 followers
1 Video View·Apr 20, 2025
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! https://www.patreon.com/LeMasterTech
what's going on you guys welcome back to
the channel and in today's video I'm
going to show you how to use match case
statements in Python
[Music]
okay so this is going to be a short and
sweet tutorial essentially a match case
statement is a newer feature of python
something that they put in I think in
Python 3.10 and people with a bit of a
background in c-based programming
languages will probably be used to this
because it's very similar but
essentially let's say you have a
function I have a very Sim simple
function going on behind you here as
long as it's a main file which this is
then it's going to constantly take an
input from the user and we're going to
ask them for the date one through seven
and then based on that input one through
seven I use a series of if else if
statements to print out the date and
basically this works fine this stacking
of ifs and L ifs if I were to run this
it's going to work okay but the thing
that makes this less than optimal is
that I'm using this L if date is equal
to a numerical value over and over and
over again and if it was a string it
would be the same thing I have to type
in lot of stuff here and there's a
simpler way to do it when we're talking
about single variable or otherwise
simple comparisons that we're checking
for a whole bunch of cases on the same
value so if it was like if date is equal
to one and some other value and some
other thing it makes sense to use ifs
and L ifs but when you get these long
lists of if L is L if L if L if else it
makes more sense to do something else
that we can call
um match case so basically the way we do
this and for now I'm going to kind of
leave this on but I'm going to put it in
a block of comments so let's look at the
great alternative to these if else
statements which is match cases okay so
we can type in match and then the value
we want to match on which is the date
variable and then we start defining the
cases okay so in the case of one let's
print out instead of me typing it is
whatever whatever I'll just print the
day and I'm going to say the day the
week starts on Monday Sunday because
that's when I have to go to work
and then let's just go ahead and say for
each case then we're going to print out
the different day okay so we're gonna go
case one case two case three and uh for
now
pan come on trying to go too fast
case four then we will print
Thursday and hopefully what you can tell
is that even though it's a minor
difference and to be clear both of these
are valid ways of writing code case one
case two is a little bit faster than
saying L if date is equal to one it's
totally a preference thing and if one
makes more sense to you than the other
or is easier to read than the other then
feel free to continue using that one but
match cases are still a valuable tool to
be able to quickly cycle through a lot
of Opera options and Alternatives off of
one variable in Python it's a little bit
faster and cleaner than a bunch of
nested if L ifs for simple variables and
then just like
um if else you can throw down an else at
the end to catch the rest we can say
case other which means something that we
haven't covered yet and we'll just say
in valid so you entered something that
wasn't a one through seven we'll just
print out invalid okay so if you look at
the if else's that I have commented out
here the code is going to do the exact
same thing as this match case but
hopefully you can tell just from looking
at it like I'll take the comment block
out for a second look how just clean and
streamlined and easy to read this is and
there's just a lot more stuff in this
one it's not bad and it's not wrong and
if you prefer to use that then you
definitely can but check out what now
when I run match case just to prove it
works for you guys let's go ahead and
enter every day so one Monday two
Tuesday three I guess I didn't
capitalize the T in Tuesday that's going
to drive some of you guys nuts five six
seven and let's put in a zero
invalid but then that's also how we exit
Okay so I have this just to talk about
the outside structure real quick I just
have this set up to where if it's not
um the main.pi file this is just
something where I recently learned it's
good practice to put this in there in
case you ever want to call a function
inside of this file from another
function as if it was a library you can
kind of ignore that and focus on the
match case stuff but basically we're
saying until the user enters a zero
we're just going to keep prompting them
to enter a date and then tell them what
date that corresponds with and so this
match casing is basically just a clean
smooth alternative to if L if else
Stacks so I hope this video was clear I
hope it was what you were looking for if
you have any questions about how to use
this or would like to see anything else
on the channel in the future just let me
know about in the comments below as
always thank you so much for watching
until next time bye
