Files
Python/Unpacking Variables.ipynb
2020-05-12 09:34:36 -07:00

361 lines
6.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Packing & Unpacking Variables"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Assign variables\n",
"In Python you can assign multiple variables at a time using commas."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"a, b, c = 1, 2, 3\n",
"print(a)\n",
"print(b)\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Swap a pair of Variables in Python\n",
"x,y = y,x"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x=12, y=5\n"
]
}
],
"source": [
"x = 5; y = 12\n",
"x, y = y, x\n",
"print('x=' + str(x) + ', y=' + str(y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Swap a trio of Variables in Python\n",
"Yes, this trick works for 3 variables too."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"88 99 77\n"
]
}
],
"source": [
"x, y, z = 77, 88, 99\n",
"z, x, y = x, y, z\n",
"print(x, y, z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Split String into multiple variables\n",
"But be careful because the number of variables must match the number of substrings from the split."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"5\n",
"6\n"
]
}
],
"source": [
"a, b, c = '4 5 6'.split()\n",
"print(a)\n",
"print(b)\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Splitting a List into variables is magically easy"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n",
"9\n",
"10\n"
]
}
],
"source": [
"my_list = [8, 9, 10]\n",
"a, b, c = my_list\n",
"print(a)\n",
"print(b)\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Split Tuple into variables"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25 26 27\n"
]
}
],
"source": [
"tup = (25,26,27)\n",
"x, y, z = tup\n",
"print(x, y, z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### This gives you a Tuple, not a List"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(8, 9, 10)\n",
"<class 'tuple'>\n"
]
}
],
"source": [
"var = a, b, c\n",
"print(var)\n",
"print(type(var))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### *args for Functions\n",
"Used for passing a non-keyworded, variable-length argument list to a function. \n",
"Received as a Tuple."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Forest', 'Hill', 'High')\n",
"<class 'tuple'>\n"
]
}
],
"source": [
"def pack_it(*args):\n",
" print(args)\n",
" print(type(args))\n",
" \n",
"x = 'Forest'; y = 'Hill'; z = 'High'\n",
"pack_it(x, y, z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This unpacks the List before sending it, so it can be received by the function as separate variables."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cullen\n",
"McDonough\n"
]
}
],
"source": [
"def unpack_it(x, y):\n",
" print(x)\n",
" print(y)\n",
" \n",
"args = ['Cullen', 'McDonough']\n",
"unpack_it(*args)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### **kwargs for Functions\n",
"Used for passing keyworded, variable-length argument dictionary to functions. \n",
"This works, but it's kinda annoying because some normal Python dictionaries fail. \n",
"func (1:'Edsel', 2:'Betamax') does not work."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'a': 'Edsel', 'b': 'Betamax', 'c': 'mGaetz'}\n",
"Edsel\n",
"<class 'dict'>\n"
]
}
],
"source": [
"def func(**losers):\n",
" print(losers)\n",
" print(losers['a'])\n",
" print(type(losers))\n",
" \n",
"func(a='Edsel', b='Betamax', c='mGaetz')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Edsel\n"
]
}
],
"source": [
"def func(a, b, c):\n",
" print(a)\n",
"\n",
"losers = {'a':'Edsel', 'b':'Betamax', 'c':'mGaetz'}\n",
"func(**losers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}