avatarVinicius Monteiro

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3271

Abstract

/code> block. It executes something when no other case satisfies. It’s like the <code>else</code> block in a <code>if</code> condition.</p><p id="ea42">A single case block also accepts the combination of multiple values. See the line in bold in the snippet of code below.</p><div id="703a"><pre>def day_of_week (day): <span class="hljs-keyword">match</span> day: <span class="hljs-built_in">case</span> <span class="hljs-number">1</span>: <span class="hljs-keyword">return</span> <span class="hljs-string">"Sunday"</span> <span class="hljs-built_in">case</span> <span class="hljs-number">2</span>: <span class="hljs-keyword">return</span> <span class="hljs-string">"Monday"</span> <span class="hljs-built_in">case</span> <span class="hljs-number">3</span>: <span class="hljs-keyword">return</span> <span class="hljs-string">"Tuesday"</span> <span class="hljs-built_in">case</span> <span class="hljs-number">4</span> | <span class="hljs-type">5</span> | <span class="hljs-type">6</span>: <span class="hljs-keyword">return</span> <span class="hljs-string">"Another day of the week"</span> <span class="hljs-built_in">case</span> <span class="hljs-keyword">_</span>: <span class="hljs-keyword">return</span> <span class="hljs-string">"Invalid day"</span></pre></div><div id="69f8"><pre><span class="hljs-function"><span class="hljs-title">print</span>(<span class="hljs-title">day_of_week</span>(<span class="hljs-number">2</span>))</span></pre></div><p id="02f4">Result,</p><div id="9c56"><pre><span class="hljs-attribute">Monday</span></pre></div><h1 id="e7c7">Pattern matching on other types of expressions</h1><p id="8a6e">Pattern matching in Python 3.10 accepts things such as lists and tuples as well, not only a single value expression.</p><p id="19c0">You can match the general pattern of the list. For instance, <i>the expression is a single item list or a two-item list</i>.</p><p id="7223">You can also match the general pattern and the list's values. For instance, <i>the expression is a two-item list, and the second item is the number two</i>.</p><p id="33fd">You can even use <code>if</code> statements and pattern match to a list of any number of elements. See the two cases in bold in the code below.</p><div id="9abd"><pre><span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> [[<span class="hljs-string">"A"</span>,<span class="hljs-string">"B"</span>,<span class="hljs-string">"C"</span>],[<span class="hljs-number">11</span>],[<span class="hljs-number">45</span>,<span class="hljs-number">45</span>],[<span class="hljs-number">7</span>,<span class="hljs-number">8</span>,<span class="hljs-number">9</span>,<span class="hljs-number">7</span>],[<span class="hljs-string">"Sunday"</span>], [<span class="hljs-string">"Sunday"</span>, <span class="hljs-string">"Monday"</span>],<span class="hljs-string">"string element"</span>]: <span class="hljs-keyword">match</span> element: <span class="hljs-keyword">case</span> [x]: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"List with one item: <span class="hljs-subst">{x}</span>"</span>) <span class="hljs-keyword">case</spa

Options

n> [x,y] <span class="hljs-keyword">if</span> x == y: <span class="hljs-built_in">print</span>(<span class="hljs-string">"List with two items of same value"</span>)
<span class="hljs-keyword">case</span> [x,<span class="hljs-string">"Monday"</span>]: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"List with two items: <span class="hljs-subst">{x}</span> and Monday"</span>) <span class="hljs-keyword">case</span> [x,<span class="hljs-string">"Tuesday"</span>]: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"List with two items: <span class="hljs-subst">{x}</span> and Tuesday"</span>)
<span class="hljs-keyword">case</span> [x,y,z]: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"List with three items: <span class="hljs-subst">{x}</span>, <span class="hljs-subst">{y}</span> and <span class="hljs-subst">{z}</span>"</span>) <span class="hljs-keyword">case</span> [*x]: <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> x: <span class="hljs-built_in">print</span>(i)
<span class="hljs-keyword">case</span> _: <span class="hljs-built_in">print</span>(<span class="hljs-string">"Case not handled"</span>)</pre></div><p id="569e">Result,</p><div id="3fd2"><pre>List <span class="hljs-keyword">with</span> <span class="hljs-literal">three</span> <span class="hljs-keyword">items</span>: A, B <span class="hljs-keyword">and</span> C List <span class="hljs-keyword">with</span> <span class="hljs-literal">one</span> <span class="hljs-keyword">item</span>: <span class="hljs-number">11</span> List <span class="hljs-keyword">with</span> <span class="hljs-literal">two</span> <span class="hljs-keyword">items</span> <span class="hljs-keyword">of</span> same <span class="hljs-built_in">value</span> <span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">7</span> List <span class="hljs-keyword">with</span> <span class="hljs-literal">one</span> <span class="hljs-keyword">item</span>: Sunday List <span class="hljs-keyword">with</span> <span class="hljs-literal">two</span> <span class="hljs-keyword">items</span>: Sunday <span class="hljs-keyword">and</span> Monday Case <span class="hljs-keyword">not</span> handled</pre></div><h1 id="f716">Reference:</h1><p id="bd7f"><a href="https://docs.python.org/3/tutorial/controlflow.html">Python 3.10.2 documentation</a></p><p id="72df">Thanks for reading. <b><i>More on Python:</i></b></p><div id="f3ef" class="link-block"> <a href="https://readmedium.com/python-for-beginners-conditions-and-loops-syntax-ce6aeb05c8a5"> <div> <div> <h2>Python For Beginners: Conditions and Loops Syntax</h2> <div><h3>if, for and while statements syntax in Python</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*W9QEHIvP4LJnwruo)"></div> </div> </div> </a> </div></article></body>

Python For Beginners: match Statement in Python 3.10.2

Introduced in Python 3.10, it’s the equivalent of the switch statement found in other programming languages

Photo by Nikolay Trebukhin on Unsplash

If you’re familiar with programming languages such as Java or JavaScript, you may know about the switch statement. It can replace if else blocks. Here’s an example of the switch statement in Java,

int letter = 1;
switch (letter) {
  case 1:
    System.out.println("A");
    break;
  case 2:
    System.out.println("B");
    break;
  case 3:
    System.out.println("C");
    break;
  default:
    System.out.println("D");
}

Similar to the switch above, from Python version 3.10, you can use the match statement.

Basic syntax

It takes an expression value and compares it with patterns in case blocks.

def day_of_week (day):
    match day:
        case 1:
            return "Sunday"
        case 2:
            return "Monday"
        case 3:
            return "Tuesday"
        case _:
            return "Invalid day"
print(day_of_week(3))

Comparing with the switch statement above, the _ at the end of the matchis equivalent to the default block. It executes something when no other case satisfies. It’s like the else block in a if condition.

A single case block also accepts the combination of multiple values. See the line in bold in the snippet of code below.

def day_of_week (day):
    match day:
        case 1:
            return "Sunday"
        case 2:
            return "Monday"
        case 3:
            return "Tuesday"
        case 4 | 5 | 6:
            return "Another day of the week"
        case _:
            return "Invalid day"
print(day_of_week(2))

Result,

Monday

Pattern matching on other types of expressions

Pattern matching in Python 3.10 accepts things such as lists and tuples as well, not only a single value expression.

You can match the general pattern of the list. For instance, the expression is a single item list or a two-item list.

You can also match the general pattern and the list's values. For instance, the expression is a two-item list, and the second item is the number two.

You can even use if statements and pattern match to a list of any number of elements. See the two cases in bold in the code below.

for element in [["A","B","C"],[11],[45,45],[7,8,9,7],["Sunday"], ["Sunday", "Monday"],"string element"]:
    match element:
        case [x]:
            print(f"List with one item: {x}")
        case [x,y] if x == y:
            print("List with two items of same value")    
        case [x,"Monday"]:
            print(f"List with two items: {x} and Monday")
        case [x,"Tuesday"]:
            print(f"List with two items: {x} and Tuesday")    
        case [x,y,z]:
            print(f"List with three items: {x}, {y} and {z}")
        case [*x]:
            for i in x:
                print(i)       
        case _:
            print("Case not handled")

Result,

List with three items: A, B and C
List with one item: 11
List with two items of same value
7
8
9
7
List with one item: Sunday
List with two items: Sunday and Monday
Case not handled

Reference:

Python 3.10.2 documentation

Thanks for reading. More on Python:

Python
Programming
Coding
Software Development
Python For Beginners
Recommended from ReadMedium