Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

You may recall how to use the if else and else if statements to test

a variable against a few conditions. But on some occasions you will have
to test a variable against many conditions to deal with this. You can use something
called a match
case statement in this video you'll learn how to use a match statement as
an alternative to an if statement. Now let's consider an example to compare
the match statement to the if statement say you want to write code to print http
error messages according to error codes. To do this with the if statement,
you would have to write the if condition, all the alternative if else conditions
and
finally an else condition conditional statements like if and else work well
over a small number of conditions but over a large number of conditions. Your code
can get large complex and
messy fortunately there is a cleaner way to achieve the same result
using the match statement. The match statement In Python was
introduced in version 3.10 using the match statement you can achieve
cleaner more readable code that allows all the same functionality as
the if controlled statement. When using match statements, there are a
few things to remember you can combine several conditions by using the or
operator in the conditional statement. The default is essentially the final
outcome if nothing is found in the case checks it's the equivalent to the else
in the if blocks let me demonstrate this example ow using V S code. Okay, so I've
written a simple
if statement that checks for an http status code if the value of
the variable http status Matches one of the conditions,
it will print out the equivalent message. I'm now going to add a match
statement below the if statement for a clear comparison I will test the same
variable against the same values I type match and then the variable http
status and a colon on the next line. I type case which is
the equivalent of the word if and the value of 200 on another line I repeat
the action using the if statement for 200 which is to print the word of success. So
in other words the variable is
matched against the value of 200 and the if values are equal it will
print out the word success. Notice that the value of HTTP status is
indeed 200 at the moment so let's run the code to test how the if and match
statements are processed in the terminal. The word success is printed twice because
the value of http status is matched twice in my code run once for the if statement
and once for the match statement. Now let's change the value of http
status to 201 and run the code again in this case success is only printed
once why do you think that happens? Because there is an all condition for
the value of 201 in the if statement but none in the match statement to do
the equivalent in the match statement. You use the or operator so
I place my cursor in between 200 and the colon and add an all character and
the value of 200 and one I clean my screen by using C L S and
then click on run again. Now success is printed twice again so in the match
statement the pipe
command is shorthand for if or the great thing is that you can add many
case statements in a match statement. But what if none of the values
match the variables value now let's change the value of HTTP status
to the value of say 550 and explore what happens, click on run and
this time the word unknown is printed. You may be wondering why that is because
the L statement is like a catch all if the value does not match anything
within the if or the L if statements. The default will be the else statement
which in this case has a print function for the word unknown. Well the match
statement
also has a default class and you add it by typing the word case
underscore colon and on the next line. Print unknown let's run
the code again great the output is unknown which means that
the default statement in both the if as well as the match
statement was action. My match statement is coming along well
but it still needs a few tweaks to make it act exactly like the given
if statement to do that. I'll add a few more case
statements that will test for the same values as the L if statements,
type case 400 colon and then I add a print command
with the words bad request, I add another case and the value of 500. And I also
need to test for
501 like in the L if statement above so once again I add an all character and
type 501 on the next line I add the error message that I want
to print which is server error. The match saves a bit of space by
combining the your statement so you don't have to do a comparison
against a variable each time. Like in the if statement let's
change the value of http status one more time to 500 and
one I just clear the screen again and click on run and server error
is printed for both statements. Now you know that there are some
differences between the two but the match statement does exactly
the same as the statement in summary. The match statement compares a value to
several different conditions until one of these conditions is met. So you now know
how to use the match
statement as an alternative to the if statement to test a variable
against many possible values. The match statement Is relatively
new to Python prior to version 3.10, developers had to get creative and code
to their own solutions you'll learn more about those alternative
methods later in this lesson.

You might also like