Skip to content

Instantly share code, notes, and snippets.

@rpatrik96
Created September 12, 2018 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpatrik96/5411ef4c86b07c054164309d6f1381ef to your computer and use it in GitHub Desktop.
Save rpatrik96/5411ef4c86b07c054164309d6f1381ef to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Tips&Tricks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Post can be found here soon: https://medium.com/@SmartLabAI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Python Proms"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Merging containers"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = [1, 2, 3]\n",
"b = [3, 4, 5]\n",
"c = [1, 2, 3, 3, 4, 5]\n"
]
}
],
"source": [
"# list -the same for sets (use {} ) and tuples (use () )\n",
"a = [1, 2, 3]\n",
"b = [3, 4, 5]\n",
"c = [*a, *b]\n",
"\n",
"print(\"a = \", a)\n",
"print(\"b = \", b)\n",
"print(\"c = \", c)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x = {'a': 1, 'b': 2}\n",
"y = {'b': 3, 'c': 4, 'd': 5}\n",
"z = {'a': 1, 'b': 3, 'c': 4, 'd': 5} <-- Mind the difference for duplicate keys!\n",
"z2 = {'b': 2, 'c': 4, 'd': 5, 'a': 1}\n"
]
}
],
"source": [
"# dict\n",
"x = {'a' : 1, 'b' : 2}\n",
"y = {'b' : 3, 'c' : 4, 'd' : 5}\n",
"z = {**x, **y}\n",
"z2 = {**y, **x}\n",
"\n",
"print(\"x = \", x)\n",
"print(\"y = \", y)\n",
"print(\"z = \", z, \" <-- Mind the difference for duplicate keys!\")\n",
"print(\"z2 = \", z2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Passing arguments"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"# non-keyword arguments\n",
"t = (1, 2, 3)\n",
"def tt(*args):\n",
" for arg in args:\n",
" print(arg+1)\n",
"\n",
"tt(*t)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Key: a value: 1\n",
"Key: b value: 2\n"
]
}
],
"source": [
"# keyword arguments\n",
"d = {'a' : 1, 'b' : 2}\n",
"def dd(**kwargs):\n",
" for key, value in kwargs.items():\n",
" print(\"Key: \", key, \" value: \", value)\n",
"\n",
"dd(**d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ternary operators"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r = 5 if True else 4\n",
"r"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### List comparisons"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"l_even = [0, 2, 4, 6, 8]\n",
"s_even_sq = {0, 64, 4, 36, 16}\n"
]
}
],
"source": [
"l = list(range(10))\n",
"l_even = [x for x in l if x%2 == 0]\n",
"s_even_sq = {x**2 for x in l if x%2 == 0}\n",
"\n",
"print(\"l = \", l)\n",
"print(\"l_even = \", l_even)\n",
"print(\"s_even_sq = \", s_even_sq)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### To be or not to be"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Equality"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a == b : True\n",
"a == c : True\n"
]
}
],
"source": [
"a = 1\n",
"b = 1\n",
"c = a\n",
"print(\"a == b :\", a == b)\n",
"print(\"a == c :\", a == c)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x == y : True\n",
"x == z : True\n"
]
}
],
"source": [
"x = [1, 2, 3, 4, 5]\n",
"y = [1, 2, 3, 4, 5]\n",
"z = x\n",
"print(\"x == y :\", x == y)\n",
"print(\"x == z :\", x == z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Identity"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a is b : True\n",
"a is c : True\n"
]
}
],
"source": [
"print(\"a is b :\", a is b)\n",
"print(\"a is c :\", a is c)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is y : False <--\n",
"x is z : True\n"
]
}
],
"source": [
"print(\"x is y :\", x is y, \"<--\")\n",
"print(\"x is z :\", x is z)"
]
}
],
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment