Explanation:
๐น Line 1: Create a List
x = [1, 2, 3]
A list is created and assigned to x.
Current value:
x = [1, 2, 3]
๐น Line 2: Start Pattern Matching
match x:
Python's match-case statement (introduced in Python 3.10) is similar to a switch statement, but much more powerful.
Python now tries to match:
[1, 2, 3]
against the available patterns.
๐น Line 3: Check the Pattern
case [1, *rest]:
This pattern means:
First element must be 1
and
Store all remaining elements in rest
Think of it like:
[1, anything, anything, ...]
๐น Line 4: Match the First Element
List:
[1, 2, 3]
Pattern:
[1, *rest]
Python checks:
1 == 1
✅ Match successful.
๐น Line 5: Capture Remaining Elements
After matching the first element:
1
remaining values are:
[2, 3]
These values are collected into:
rest
So:
rest = [2, 3]
๐น Line 6: Execute Print Statement
print(rest)
becomes:
print([2, 3])
Output:
[2, 3]

0 Comments:
Post a Comment