|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Packing & Unpacking Variables" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "### Assign variables\n", |
| 15 | + "In Python you can assign multiple variables at a time using commas." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": 1, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [ |
| 23 | + { |
| 24 | + "name": "stdout", |
| 25 | + "output_type": "stream", |
| 26 | + "text": [ |
| 27 | + "1\n", |
| 28 | + "2\n", |
| 29 | + "3\n" |
| 30 | + ] |
| 31 | + } |
| 32 | + ], |
| 33 | + "source": [ |
| 34 | + "a, b, c = 1, 2, 3\n", |
| 35 | + "print(a)\n", |
| 36 | + "print(b)\n", |
| 37 | + "print(c)" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "markdown", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "### Swap a pair of Variables in Python\n", |
| 45 | + "x,y = y,x" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 2, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [ |
| 53 | + { |
| 54 | + "name": "stdout", |
| 55 | + "output_type": "stream", |
| 56 | + "text": [ |
| 57 | + "x=12, y=5\n" |
| 58 | + ] |
| 59 | + } |
| 60 | + ], |
| 61 | + "source": [ |
| 62 | + "x = 5; y = 12\n", |
| 63 | + "x, y = y, x\n", |
| 64 | + "print('x=' + str(x) + ', y=' + str(y))" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "metadata": {}, |
| 70 | + "source": [ |
| 71 | + "### Swap a trio of Variables in Python\n", |
| 72 | + "Yes, this trick works for 3 variables too." |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": 3, |
| 78 | + "metadata": {}, |
| 79 | + "outputs": [ |
| 80 | + { |
| 81 | + "name": "stdout", |
| 82 | + "output_type": "stream", |
| 83 | + "text": [ |
| 84 | + "88 99 77\n" |
| 85 | + ] |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "x, y, z = 77, 88, 99\n", |
| 90 | + "z, x, y = x, y, z\n", |
| 91 | + "print(x, y, z)" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "markdown", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "### Split String into multiple variables\n", |
| 99 | + "But be careful because the number of variables must match the number of substrings from the split." |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": 4, |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [ |
| 107 | + { |
| 108 | + "name": "stdout", |
| 109 | + "output_type": "stream", |
| 110 | + "text": [ |
| 111 | + "4\n", |
| 112 | + "5\n", |
| 113 | + "6\n" |
| 114 | + ] |
| 115 | + } |
| 116 | + ], |
| 117 | + "source": [ |
| 118 | + "a, b, c = '4 5 6'.split()\n", |
| 119 | + "print(a)\n", |
| 120 | + "print(b)\n", |
| 121 | + "print(c)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "metadata": {}, |
| 127 | + "source": [ |
| 128 | + "### Splitting a List into variables is magically easy" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": 5, |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [ |
| 136 | + { |
| 137 | + "name": "stdout", |
| 138 | + "output_type": "stream", |
| 139 | + "text": [ |
| 140 | + "8\n", |
| 141 | + "9\n", |
| 142 | + "10\n" |
| 143 | + ] |
| 144 | + } |
| 145 | + ], |
| 146 | + "source": [ |
| 147 | + "my_list = [8, 9, 10]\n", |
| 148 | + "a, b, c = my_list\n", |
| 149 | + "print(a)\n", |
| 150 | + "print(b)\n", |
| 151 | + "print(c)" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "markdown", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "### Split Tuple into variables" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": 6, |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [ |
| 166 | + { |
| 167 | + "name": "stdout", |
| 168 | + "output_type": "stream", |
| 169 | + "text": [ |
| 170 | + "25 26 27\n" |
| 171 | + ] |
| 172 | + } |
| 173 | + ], |
| 174 | + "source": [ |
| 175 | + "tup = (25,26,27)\n", |
| 176 | + "x, y, z = tup\n", |
| 177 | + "print(x, y, z)" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "### This gives you a Tuple, not a List" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": 7, |
| 190 | + "metadata": {}, |
| 191 | + "outputs": [ |
| 192 | + { |
| 193 | + "name": "stdout", |
| 194 | + "output_type": "stream", |
| 195 | + "text": [ |
| 196 | + "(8, 9, 10)\n", |
| 197 | + "<class 'tuple'>\n" |
| 198 | + ] |
| 199 | + } |
| 200 | + ], |
| 201 | + "source": [ |
| 202 | + "var = a, b, c\n", |
| 203 | + "print(var)\n", |
| 204 | + "print(type(var))" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "markdown", |
| 209 | + "metadata": {}, |
| 210 | + "source": [ |
| 211 | + "### *args for Functions\n", |
| 212 | + "Used for passing a non-keyworded, variable-length argument list to a function. \n", |
| 213 | + "Received as a Tuple." |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "code", |
| 218 | + "execution_count": 8, |
| 219 | + "metadata": {}, |
| 220 | + "outputs": [ |
| 221 | + { |
| 222 | + "name": "stdout", |
| 223 | + "output_type": "stream", |
| 224 | + "text": [ |
| 225 | + "('Forest', 'Hill', 'High')\n", |
| 226 | + "<class 'tuple'>\n" |
| 227 | + ] |
| 228 | + } |
| 229 | + ], |
| 230 | + "source": [ |
| 231 | + "def pack_it(*args):\n", |
| 232 | + " print(args)\n", |
| 233 | + " print(type(args))\n", |
| 234 | + " \n", |
| 235 | + "x = 'Forest'; y = 'Hill'; z = 'High'\n", |
| 236 | + "pack_it(x, y, z)" |
| 237 | + ] |
| 238 | + }, |
| 239 | + { |
| 240 | + "cell_type": "markdown", |
| 241 | + "metadata": {}, |
| 242 | + "source": [ |
| 243 | + "This unpacks the List before sending it, so it can be received by the function as separate variables." |
| 244 | + ] |
| 245 | + }, |
| 246 | + { |
| 247 | + "cell_type": "code", |
| 248 | + "execution_count": 9, |
| 249 | + "metadata": {}, |
| 250 | + "outputs": [ |
| 251 | + { |
| 252 | + "name": "stdout", |
| 253 | + "output_type": "stream", |
| 254 | + "text": [ |
| 255 | + "Cullen\n", |
| 256 | + "McDonough\n" |
| 257 | + ] |
| 258 | + } |
| 259 | + ], |
| 260 | + "source": [ |
| 261 | + "def unpack_it(x, y):\n", |
| 262 | + " print(x)\n", |
| 263 | + " print(y)\n", |
| 264 | + " \n", |
| 265 | + "args = ['Cullen', 'McDonough']\n", |
| 266 | + "unpack_it(*args)" |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "markdown", |
| 271 | + "metadata": {}, |
| 272 | + "source": [ |
| 273 | + "### **kwargs for Functions\n", |
| 274 | + "Used for passing keyworded, variable-length argument dictionary to functions. \n", |
| 275 | + "This works, but it's kinda annoying because some normal Python dictionaries fail. \n", |
| 276 | + "func (1:'Edsel', 2:'Betamax') does not work." |
| 277 | + ] |
| 278 | + }, |
| 279 | + { |
| 280 | + "cell_type": "code", |
| 281 | + "execution_count": 10, |
| 282 | + "metadata": {}, |
| 283 | + "outputs": [ |
| 284 | + { |
| 285 | + "name": "stdout", |
| 286 | + "output_type": "stream", |
| 287 | + "text": [ |
| 288 | + "{'a': 'Edsel', 'b': 'Betamax', 'c': 'mGaetz'}\n", |
| 289 | + "Edsel\n", |
| 290 | + "<class 'dict'>\n" |
| 291 | + ] |
| 292 | + } |
| 293 | + ], |
| 294 | + "source": [ |
| 295 | + "def func(**losers):\n", |
| 296 | + " print(losers)\n", |
| 297 | + " print(losers['a'])\n", |
| 298 | + " print(type(losers))\n", |
| 299 | + " \n", |
| 300 | + "func(a='Edsel', b='Betamax', c='mGaetz')" |
| 301 | + ] |
| 302 | + }, |
| 303 | + { |
| 304 | + "cell_type": "markdown", |
| 305 | + "metadata": {}, |
| 306 | + "source": [ |
| 307 | + "This works, but it's kinda annoying because you have to use strings for the keys, so some normal Python dictionaries will give you an error. {1:'Edsel', 2:'Betamax'} fails." |
| 308 | + ] |
| 309 | + }, |
| 310 | + { |
| 311 | + "cell_type": "code", |
| 312 | + "execution_count": 11, |
| 313 | + "metadata": {}, |
| 314 | + "outputs": [ |
| 315 | + { |
| 316 | + "name": "stdout", |
| 317 | + "output_type": "stream", |
| 318 | + "text": [ |
| 319 | + "Edsel\n" |
| 320 | + ] |
| 321 | + } |
| 322 | + ], |
| 323 | + "source": [ |
| 324 | + "def func(a, b, c):\n", |
| 325 | + " print(a)\n", |
| 326 | + "\n", |
| 327 | + "losers = {'a':'Edsel', 'b':'Betamax', 'c':'mGaetz'}\n", |
| 328 | + "func(**losers)" |
| 329 | + ] |
| 330 | + }, |
| 331 | + { |
| 332 | + "cell_type": "code", |
| 333 | + "execution_count": null, |
| 334 | + "metadata": {}, |
| 335 | + "outputs": [], |
| 336 | + "source": [] |
| 337 | + } |
| 338 | + ], |
| 339 | + "metadata": { |
| 340 | + "kernelspec": { |
| 341 | + "display_name": "Python 3", |
| 342 | + "language": "python", |
| 343 | + "name": "python3" |
| 344 | + }, |
| 345 | + "language_info": { |
| 346 | + "codemirror_mode": { |
| 347 | + "name": "ipython", |
| 348 | + "version": 3 |
| 349 | + }, |
| 350 | + "file_extension": ".py", |
| 351 | + "mimetype": "text/x-python", |
| 352 | + "name": "python", |
| 353 | + "nbconvert_exporter": "python", |
| 354 | + "pygments_lexer": "ipython3", |
| 355 | + "version": "3.7.0" |
| 356 | + } |
| 357 | + }, |
| 358 | + "nbformat": 4, |
| 359 | + "nbformat_minor": 2 |
| 360 | +} |
0 commit comments