|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python 10 - Structural Pattern Matching\n", |
| 8 | + "### match statements \n", |
| 9 | + "Very similar to switch/case statements in C, Java, and Javascript. \n", |
| 10 | + "Can be used in lieu of if/elif/else blocks. \n", |
| 11 | + "[documentation](https://www.python.org/dev/peps/pep-0622/)" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 1, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [ |
| 19 | + { |
| 20 | + "name": "stdout", |
| 21 | + "output_type": "stream", |
| 22 | + "text": [ |
| 23 | + "large\n" |
| 24 | + ] |
| 25 | + } |
| 26 | + ], |
| 27 | + "source": [ |
| 28 | + "var = 3\n", |
| 29 | + "\n", |
| 30 | + "match var:\n", |
| 31 | + " case 1:\n", |
| 32 | + " print('small')\n", |
| 33 | + " case 2:\n", |
| 34 | + " print('medium')\n", |
| 35 | + " case 3:\n", |
| 36 | + " print('large')" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "#### The Default case _ \n", |
| 44 | + "The default case, using underscore, is optional. " |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 2, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [ |
| 52 | + { |
| 53 | + "name": "stdout", |
| 54 | + "output_type": "stream", |
| 55 | + "text": [ |
| 56 | + "large\n" |
| 57 | + ] |
| 58 | + } |
| 59 | + ], |
| 60 | + "source": [ |
| 61 | + "var = 4\n", |
| 62 | + "\n", |
| 63 | + "match var:\n", |
| 64 | + " case 1:\n", |
| 65 | + " print('small')\n", |
| 66 | + " case 2:\n", |
| 67 | + " print('medium')\n", |
| 68 | + " case _:\n", |
| 69 | + " print('large')" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "markdown", |
| 74 | + "metadata": {}, |
| 75 | + "source": [ |
| 76 | + "#### Conditionals in case \n", |
| 77 | + "if statements and or (using bar) are supported in case statements." |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": 3, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [ |
| 85 | + { |
| 86 | + "name": "stdout", |
| 87 | + "output_type": "stream", |
| 88 | + "text": [ |
| 89 | + "small\n" |
| 90 | + ] |
| 91 | + } |
| 92 | + ], |
| 93 | + "source": [ |
| 94 | + "var = 2\n", |
| 95 | + "\n", |
| 96 | + "match var:\n", |
| 97 | + " case x if x<=3:\n", |
| 98 | + " print('small')\n", |
| 99 | + " case 4 | 5 | 6:\n", |
| 100 | + " print('medium')\n", |
| 101 | + " case _:\n", |
| 102 | + " print('large')" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "#### No breaks needed\n", |
| 110 | + "Note that you do not need break statements. The match block will automatically end execution after one case is executed." |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": 4, |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [ |
| 118 | + { |
| 119 | + "name": "stdout", |
| 120 | + "output_type": "stream", |
| 121 | + "text": [ |
| 122 | + "A\n", |
| 123 | + "F\n" |
| 124 | + ] |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "def print_grade(score):\n", |
| 129 | + " match score:\n", |
| 130 | + " # case score > 90 this does not work!\n", |
| 131 | + " case score if score >= 90:\n", |
| 132 | + " print('A')\n", |
| 133 | + " case score if score >= 80:\n", |
| 134 | + " print('B')\n", |
| 135 | + " case score if score >= 70:\n", |
| 136 | + " print('C')\n", |
| 137 | + " case score if score >= 60:\n", |
| 138 | + " print('D')\n", |
| 139 | + " case _:\n", |
| 140 | + " print('F')\n", |
| 141 | + " \n", |
| 142 | + "print_grade(94)\n", |
| 143 | + "print_grade(48)" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "markdown", |
| 148 | + "metadata": {}, |
| 149 | + "source": [ |
| 150 | + "#### Python Objects \n", |
| 151 | + "match statements can also use Python objects and instance variables." |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": 5, |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [ |
| 159 | + { |
| 160 | + "name": "stdout", |
| 161 | + "output_type": "stream", |
| 162 | + "text": [ |
| 163 | + "welcome to the business program!\n", |
| 164 | + "welcome to the science program!\n" |
| 165 | + ] |
| 166 | + } |
| 167 | + ], |
| 168 | + "source": [ |
| 169 | + "class Student:\n", |
| 170 | + " def __init__(self, n, i, m):\n", |
| 171 | + " self.name = n\n", |
| 172 | + " self.id = i\n", |
| 173 | + " self.major = m\n", |
| 174 | + "\n", |
| 175 | + "def welcome(student):\n", |
| 176 | + " match student.major:\n", |
| 177 | + " case 'engineering':\n", |
| 178 | + " print('welcome to the engineering program!')\n", |
| 179 | + " case 'business':\n", |
| 180 | + " print('welcome to the business program!')\n", |
| 181 | + " case 'pharmacy':\n", |
| 182 | + " print('welcome to the pharmacy program!')\n", |
| 183 | + " case x:\n", |
| 184 | + " print(f'welcome to the {x} program!')\n", |
| 185 | + " \n", |
| 186 | + "new_student = Student('Suresh', 5723, 'business')\n", |
| 187 | + "welcome(new_student)\n", |
| 188 | + "\n", |
| 189 | + "new_student = Student('Britney', 5724, 'science')\n", |
| 190 | + "welcome(new_student)" |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": null, |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [], |
| 198 | + "source": [] |
| 199 | + } |
| 200 | + ], |
| 201 | + "metadata": { |
| 202 | + "kernelspec": { |
| 203 | + "display_name": "Python 3 (ipykernel)", |
| 204 | + "language": "python", |
| 205 | + "name": "python3" |
| 206 | + }, |
| 207 | + "language_info": { |
| 208 | + "codemirror_mode": { |
| 209 | + "name": "ipython", |
| 210 | + "version": 3 |
| 211 | + }, |
| 212 | + "file_extension": ".py", |
| 213 | + "mimetype": "text/x-python", |
| 214 | + "name": "python", |
| 215 | + "nbconvert_exporter": "python", |
| 216 | + "pygments_lexer": "ipython3", |
| 217 | + "version": "3.10.0" |
| 218 | + } |
| 219 | + }, |
| 220 | + "nbformat": 4, |
| 221 | + "nbformat_minor": 2 |
| 222 | +} |
0 commit comments