Match Case Statements in Python | Python Tutorial - Day #16

preview_player
Показать описание
Python is one of the most demanded programming languages in the job market. Surprisingly, it is equally easy to learn and master Python. This python tutorial for absolute beginners in Hindi series will focus on teaching you python concepts from the ground up.

python, C, C++, Java, JavaScript and Other Cheetsheets [++]:

►Learn in One Video[++]:

►Complete course [playlist]:

Follow Me On Social Media
Comment "#HarryBhai" if you read this 😉😉
Рекомендации по теме
Комментарии
Автор

@CodeWithHarry I think if/else statements and Match case statements are totally same because I made a simple calculator using both but found Match case to be more convenient to use..

# Using Match Case Statements

''' Simple Calculator '''

input1 = int(input("Enter first no. : "))
input2 = int(input("Enter second no. :"))

op = input("Enter operator (+, -, *, /) : ")

match op:

case '+':
print(input1+input2)
case '-':
print(input1+input2)
case '*':
print(input1*input2)
case '/':
if input2 == 0:
print("Number cannot be divide by 0")
else:
print(input1/input2)



# Using If/Else Statements

int1 = int(input("Enter first no. : "))
int2 = int(input("Enter second no. :"))

op1 = input("Enter operator (+, -, *, /) : ")

if op1 == '+':
print(int1+int2)
elif op1 == '-':
print(int1-int2)
elif op1 == '*':
print(int1*int2)
elif op1 == '/':
if int2 == 0:
print("Number cannot be divide by 0")
else:
print(int1/int2)

else:
print("No other operators are allowed")

abraar
Автор

Bhai mera ek simple request hai. At the end of every new session give us some questions based on what we have learned so far.

techodisha
Автор

Such a Hardworking Mentor in coding courses in YouTube ever...Respect Sir❤

uddiponneog
Автор

No one is like mr. harry. I have recently seen one of the youtube channel charging aroun 7K for 45days of python training. but here harry bhai giving 100days of trainning for free. some people are so stupid they are ready to pay for only 45days for code in python. harry bhai you're awesome. that is the reason this channel growth rate is so high as compare to other youtube channel. and harry bhai teaching style is fire. thank you for bringing awesome course harry bhai. after this soon please bring some cloud technology like DevOps tools. thank you so much. that youtube channel name is #WsCube Tech which has half subscriber than this. please dont be fool guy. get good and awesome content with codewithharry. thank you.

rayganmudberry
Автор

a = int(input("Enter a Valid Month Number: "))
match a:
case 1:
print("January")
case 2:
print("February")
case 3:
print("March")
case 4:
print("April")
case 5:
print("May")
case 6:
print("June")
case 7:
print("July")
case 8:
print("August")
case 9:
print("September")
case 10:
print("Octuber")
case 11:
print("November")
case 12:
print("December")
case _:
print("Please Enter a Valid Month Number")

MuhammadIhsan-guks
Автор

match case is available from python 3.10 onwards. _ is used for default. break is not required in python.
case default can also have if statements.
Syntax

match (original argument)
case condition:
case _:

sohailnawaz
Автор

Day 4 of #100DaysOfCode. Today in this video 16, I learned about match case statements in Python, which provide switch-case-like functionality. I discovered how to compare a variable's value to different patterns using the match keyword, case clauses, and expressions. This will enable me to create more efficient and readable code when handling multiple conditions or patterns.
Thank You Harry Bhaiya.

debasishbesra
Автор

# * match case is basically switch case you'd know if you come from different coding language
age = int(input("Enter your age: "))
match age:
case _ if age < 18:
print("Not Legal to drive")
case _ if age >= 18 and age < 60:
print("Legal to drive")
case _ if age >= 60 and age < 80:
print("Legal to drive, but be cautious")
case _ if age >= 80:
print("Illegal to drive")

user
Автор

print("\tWelcome to the Voting")
x = int(input("Do you want to vote?\n Enter 0 for Yes and 1 for No:"))
match x:
case 0:
print("You want to vote!")
age = int(input("Enter your age: "))
if (age >= 18):
print("You can vote.")
name = int(input("Whom do you want to vote: \n 1. John\n 2.Adam\n 3.Ash\n"))
if (name == 1):
print("Your vote is registered for John!")
elif (name == 2):
print("Your vote is registered for Adam!")
elif (name == 3):
print("Your name is registered for Ash!")
else:
print("Invalid Entry! Try Again.")
else:
print("You cannot vote as your age is below 18.")

case 1:
print("As you wish")

muskanoad
Автор

# Match case of python
print('USE 1 FOR "YES" AND 2 FOR "NO"')
print("Do You Want To Cast Your Vote ?")
x = int(input())
match x:
case 1:
print("So you want to cast your vote")
y = int(input("Enter your age : "))
if(y>=18):
print("You are eligible to vote")
else:
print("You are under age you can not cast your vote")
case 2:
print("As your wish")

xSaranshx
Автор

an example from my side, hope this will help also:

a=int(input("enter the marks:"))
match a:
case _ if a>=0 and a<=34:
print("failed!")
case _ if a>=35 and a<=49:
print("You have got a C")
case _ if a>=50 and a<=59:
print("You have got a C+")
case _ if a>=60 and a<=69:
print("You have got a B")
case _ if a>=70 and a<=79:
print("You have got a B+")
case _ if a>=80 and a<=89:
print("You have got an A")
case _ if a>=90 and a<=99:
print("You have got an A+")
case _ if a==100:
print("You have got an AA")

bhaskarroy
Автор

a = int(input("Enter the first number: "))
b = int(input("Enter the Second number: "))

x = int(input("""1. Enter 1 for add
2. Enter 2 for Sub
3. Enter 3 for Div
4. Enter 4 for Mul\n"""))

match x:
case 1:
c = a + b
print ("Add is: ", c)
case 2:
c = a - b
print ("Sub is: ", c)
case 3:
c = a / b
print ("Div is: ", c)
case 4:
c = a * b
print ("Mul is: ", c)
case _:
print("Enter the correct option")

amitkhare
Автор

# Practice
print('''Who would you like to vote
Number 1 : PTI
Number 2 : PMLN
Number 3 : PPP
Number 4 : TLP
''')
x = int(input("Enter Your Vote-Casting Number Here: "))
match x:
case 1:
print("You Have Voted PTI")
case 2:
print("You Have Voted PMLN")
case 3:
print("You Have Voted PPP")
case 4:
print("You Have Voted TLP")
case _:
print("Invalid Vote-Casting Number")

Just for practice

hazby
Автор

I am so proud that I can write something like this 😁 Thanks Harry!

x= int(input("Please enter your age: "))
match x:
case _ if x <= 13:
print ("Yo! Gen Alpha")
case _ if x <=29 and x >13:
print ("Hi! Gen Z")
case _ if x <=44 and x >29:
print ("Hello! Millennials")
case _ if x > 44:
print ("Greetings!")

cfjorwk
Автор

App hamre liye bahut mehanat kar rahe ho harry bhai apko dil ❤se thanku mene coding ki duniya me apki ungali pakad kar kadam rakha he thanku so much

ravindraprajapati
Автор

My semester exam has scheduled in the first week of january... But i'm addicted to your's tutorial. So i can't delay to watch your any video.

StreamingLivehere
Автор

Harry bhai course bahut achhi hai or ye course karne me v achaa lag raha hai
But aap theroy v saath me de dete to note v ban jata Harry bhai 👍

coding_ascii_
Автор

for anime fans

x = int(input("choose number between 1 - 5 to know ur favourite anime: ", ) )

match x:
case 1:
print("demon slayer")

case 2:
print("naruto")

case 3:
print("tokyo revangers ")

case 4:
print("jjk")

case 5:
print("one piece")


x = input("did you now know your favourie anime ?")

if x.lower() == "yes":
print("thats good")

else:
print("sorry")

pratyus
Автор

Practice exercise for match Statement:

1 : Image File type classifier ( HINT: Enter the full path of the image and use match statement to find out the file type i.e .png, .jpg, etc)

FaceSenseAustralia
Автор

import time
# times = time.strftime('%H:%M:%S')
# print(times)
hour= int(time.strftime('%H'))
if(hour>=5 and hour<=12):
print("Good Morning")
elif(hour>=12 and hour<=6):
print("Good Afternoon and Good Evening")
else:
print("Good Night")

indianriddle