Table of contents
Introduction
Hello beautiful people and welcome to my 16th blog post. A little update, I was sick for the past 2-3 days and hence could not share my learning of the python coding journey. So today I learned a new feature of python that got introduced in python 3.10 i.e Match Case statement. It is similar to the "Switch case" statement but without a break. Now let's dive deep into the details
So let's get started......
Match Case Statement
The match case statement compares a variable with the pattern in the match case statement until it finds the perfect fit. So a match case statement compares the variable value with the different shapes also called as patterns.
The match case statement consists of 3 components:
The "match" keyword
One or more case clauses i.e options available for patterns
Expression for each case
The case clause consists of patterns to be matched to the variable, a condition that needs to be matched with the variable and then a set of statements to be executed if the pattern matches.
Syntax -
match variable_name:
case 0: #pattern 1
print("abc") #statement1
case 1 if (variable_name == 1): #here we have applied a condition
print("xyz") #statement 2
case 2: #pattern 3
print("lmno") #statement3
case _: #default case
print("aasad")
.
.
.
.
.
.
So I wrote a bunch of code as explained in the tutorial for a better understanding of "match case statement". A default case will be executed if none of the cases fulfills the conditions.
Example -
x = 4
# x is the variable to match. you can also ask user to enter the varibale using input() function
#x = input("Enter the variable")
match x:
# if x is 0
case 0:
print("x is zero")
# case with if-condition
case 4 if x % 2 == 0: #this case is executed as condition is TRUE
print("x % 2 == 0 and case is 4")
# Empty case with if-condition
case _ if x < 10:
print("x is < 10")
# default case(will only be matched if the above cases were not matched)
# so it is basically just an else:
case _:
print(x)
x % 2 == 0 and case is 4
Resources Used
You can watch the video of Day#16 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. Hope you find this post insightful in case you are also a beginner like me and helped you learn something new.
I hope you have joined me and are enjoying my magical journey of learning python coding. This is it for Day #16
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.