About Lesson
Match Statement
The match
statement, introduced in Python 3.10, is a way to handle different conditions in your code more easily. It lets you check a value (called the “subject”) against several patterns and run the appropriate code when a match is found.
Key Points
- Pattern Matching: You can match simple values like numbers or strings, as well as more complex structures like lists and dictionaries.
- Wildcards: Use an underscore (
_
) to match anything that doesn’t fit other patterns. - Guard Conditions: You can add extra conditions with
if
statements to make your matches more specific.
Example :
Python
num = int(input("Enter a number:"))
match num:
case 0:
print("Zero")
case 1:
print("One")
case 2:
print("Two")
case 3:
print("Three")
case 4:
print("Four")
case 5:
print("Five")
case 6:
print("Six")
case 7:
print("Seven")
case 8:
print("Eight")
case 9:
print("Nine")
Assignment
1.Enter numbers in digits and print it in words -> 4257 = Four Two Five Seven
after that print 512 -> Five hundred twelve