Skip to content

Instantly share code, notes, and snippets.

@kantale
Last active November 22, 2018 17:56
Show Gist options
  • Save kantale/f5a3806bf73bf155bf90627dafd81d94 to your computer and use it in GitHub Desktop.
Save kantale/f5a3806bf73bf155bf90627dafd81d94 to your computer and use it in GitHub Desktop.
Πρόχειρες σημειώσεις από το μάθημα python, 6η διάλεξη, 22 Νοεμβρίου 2018
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def mean(l):\n",
" return sum(l)/len(l)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"a = []\n",
"for i in range(1,100,3):\n",
" a.append(i)\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"49.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean(a)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"a = [i for i in range(1,100,3)]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"49.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean(a)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"49.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean([i for i in range(1,100,3)])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1,\n",
" 4,\n",
" 7,\n",
" 10,\n",
" 13,\n",
" 16,\n",
" 19,\n",
" 22,\n",
" 25,\n",
" 28,\n",
" 31,\n",
" 34,\n",
" 37,\n",
" 40,\n",
" 43,\n",
" 46,\n",
" 49,\n",
" 52,\n",
" 55,\n",
" 58,\n",
" 61,\n",
" 64,\n",
" 67,\n",
" 70,\n",
" 73,\n",
" 76,\n",
" 79,\n",
" 82,\n",
" 85,\n",
" 88,\n",
" 91,\n",
" 94,\n",
" 97,\n",
" 'mitsos']"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[i for i in range(1,100,3)] + ['mitsos']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"a = []\n",
"b = []\n",
"for i in range(1,100,3):\n",
" a.append(i)\n",
" b.append(i+1)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2,\n",
" 5,\n",
" 8,\n",
" 11,\n",
" 14,\n",
" 17,\n",
" 20,\n",
" 23,\n",
" 26,\n",
" 29,\n",
" 32,\n",
" 35,\n",
" 38,\n",
" 41,\n",
" 44,\n",
" 47,\n",
" 50,\n",
" 53,\n",
" 56,\n",
" 59,\n",
" 62,\n",
" 65,\n",
" 68,\n",
" 71,\n",
" 74,\n",
" 77,\n",
" 80,\n",
" 83,\n",
" 86,\n",
" 89,\n",
" 92,\n",
" 95,\n",
" 98]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def mean_1(l):\n",
" return sum(l)/len(l)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"mean_2 = lambda l:sum(l)/len(l)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.333333333333333"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean_1([3,5,8])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.333333333333333"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean_2([3,5,8])"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"a = [\"mitsos\", \"kwstas\", \"maria\"]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['kwstas', 'maria', 'mitsos']"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['mitsos', 'kwstas', 'maria']"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('t', 'mitsos'), ('s', 'kwstas'), ('r', 'maria')]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[(x[2], x) for x in a]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('r', 'maria'), ('s', 'kwstas'), ('t', 'mitsos')]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted([(x[2], x) for x in a])"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['maria', 'kwstas', 'mitsos']"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[y[1] for y in sorted([(x[2], x) for x in a])]"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return x[2]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['maria', 'kwstas', 'mitsos']"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=f)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['mitsos', 'kwstas', 'maria']"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 < 2"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"mitso\" < \"kistos\""
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{}.keys() < {'a':'b'}.keys()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[1,2,3] < [0,5,6,7]"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[] < ['a']"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['mitsos', 'kwstas', 'maria']"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['maria', 'mitsos', 'kwstas']"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[{x[3]:x for x in a}[y] for y in sorted({x[3]:x for x in a})]"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return x[2]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['maria', 'kwstas', 'mitsos']"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=f)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['mitsos', 'kwstas', 'maria']"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['maria', 'kwstas', 'mitsos']"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=lambda x : x[2])"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return x[2]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"f = lambda x : x[2]"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"a = [\n",
" {'country': 'Greece', 'pop': 10000000},\n",
" {'country': 'Esthonia', 'pop': 3000000},\n",
" {'country': 'China', 'pop': 1300000000},\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return x['pop']"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Esthonia', 'pop': 3000000},\n",
" {'country': 'Greece', 'pop': 10000000},\n",
" {'country': 'China', 'pop': 1300000000}]"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=f)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3000000, 10000000, 1300000000]"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(map(f, a))"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Greece', 'pop': 10000000},\n",
" {'country': 'Esthonia', 'pop': 3000000},\n",
" {'country': 'China', 'pop': 1300000000}]"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(10000000, {'country': 'Greece', 'pop': 10000000}),\n",
" (3000000, {'country': 'Esthonia', 'pop': 3000000}),\n",
" (1300000000, {'country': 'China', 'pop': 1300000000})]"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[(f(x), x) for x in a]"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(3000000, {'country': 'Esthonia', 'pop': 3000000}),\n",
" (10000000, {'country': 'Greece', 'pop': 10000000}),\n",
" (1300000000, {'country': 'China', 'pop': 1300000000})]"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted([(f(x), x) for x in a])"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Esthonia', 'pop': 3000000},\n",
" {'country': 'Greece', 'pop': 10000000},\n",
" {'country': 'China', 'pop': 1300000000}]"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[y[1] for y in sorted([(f(x), x) for x in a])]"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Esthonia', 'pop': 3000000},\n",
" {'country': 'Greece', 'pop': 10000000},\n",
" {'country': 'China', 'pop': 1300000000}]"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=f)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"a = [\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'Esthonia', 'men':2000000, 'women': 1000000},\n",
" {'country': 'China', 'men': 600000000, 'women': 700000000},\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"def f2(x):\n",
" return x['men']+x['women']"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Esthonia', 'men': 2000000, 'women': 1000000},\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'China', 'men': 600000000, 'women': 700000000}]"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a,key=f2)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Esthonia', 'men': 2000000, 'women': 1000000},\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'China', 'men': 600000000, 'women': 700000000}]"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=lambda x : x['men'] + x['women'])"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Esthonia', 'men': 2000000, 'women': 1000000},\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'China', 'men': 600000000, 'women': 700000000}]"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=lambda x : sum([x['men'], x['women']]))"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'country': 'Esthonia', 'men': 2000000, 'women': 1000000}"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"min(a, key=lambda x : sum([x['men'], x['women']]))"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'country': 'China', 'men': 600000000, 'women': 700000000}"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max(a, key=lambda x : sum([x['men'], x['women']]))"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'China', 'men': 600000000, 'women': 700000000},\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'Esthonia', 'men': 2000000, 'women': 1000000}]"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=lambda x : sum([x['men'], x['women']]), reverse=True)"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'China', 'men': 600000000, 'women': 700000000},\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'Esthonia', 'men': 2000000, 'women': 1000000}]"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=lambda x : -sum([x['men'], x['women']]))"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"a = [\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'Esthonia', 'men':2000000, 'women': 1000000},\n",
" {'country': 'China', 'men': 600000000, 'women': 700000000},\n",
" {'country': 'Alaska', 'men': 5000000, 'women': 5000000},\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Esthonia', 'men': 2000000, 'women': 1000000},\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'Alaska', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'China', 'men': 600000000, 'women': 700000000}]"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=lambda x : x['men']+x['women'])"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 1, 1, 1, 1, 1, 1, 2, 3]"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted([1,2,3,1,1,1,1,1,1,])"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Esthonia', 'men': 2000000, 'women': 1000000},\n",
" {'country': 'Alaska', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'China', 'men': 600000000, 'women': 700000000}]"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(a, key=lambda x : (x['men']+x['women'], x['country']))"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(10000000, 'Greece'),\n",
" (3000000, 'Esthonia'),\n",
" (1300000000, 'China'),\n",
" (10000000, 'Alaska')]"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda x : (x['men']+x['women'], x['country']), a))"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'country': 'Esthonia', 'men': 2000000, 'women': 1000000},\n",
" {'country': 'Alaska', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'Greece', 'men': 5000000, 'women': 5000000},\n",
" {'country': 'China', 'men': 600000000, 'women': 700000000}]"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# DO NOT DO IT\n",
"sorted(a, key=lambda x : ( (lambda y:y['men']+y['women'])(x), \n",
" (lambda y:y['country'])(x) ))"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"a = lambda x : len(x)"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a('mitsos')"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"a = lambda x,y:x+y"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a(5,6)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"def a(x,y):\n",
" return x+y"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"a = ['mitsos', 'kwstas', 'maria']"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['sostim', 'satswk', 'airam']\n"
]
}
],
"source": [
"b = []\n",
"for x in a:\n",
" b.append(x[::-1])\n",
"print (b)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['sostim', 'satswk', 'airam']"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x[::-1] for x in a]"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['sostim', 'satswk', 'airam']"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda x:x[::-1], a))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"any([False, False, False])"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"any([False, True, False])"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"any([0,0,0,1,0,0,0])"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all([True, True, True])"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 141,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all([True, False, True])"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"####################"
]
}
],
"source": [
"# IGNORE \n",
"import time\n",
"for x in range(20):\n",
" print ('#' + '*', end='')\n",
" time.sleep(1)\n",
" print ('\\b', end='')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Generators"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
"def f():\n",
" return 1"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 165,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f()"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"def f():\n",
" return 1\n",
" return 2"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 167,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f()"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
"def f():\n",
" yield 1\n",
" yield 2"
]
},
{
"cell_type": "code",
"execution_count": 174,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 174,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = f()\n",
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 175,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 176,
"metadata": {},
"outputs": [
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-176-e734f8aca5ac>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2]"
]
},
"execution_count": 177,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(f())"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [],
"source": [
"def is_prime(x):\n",
" for i in range(2,x):\n",
" if x%2 == 0:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(5)"
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 184,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(10)"
]
},
{
"cell_type": "code",
"execution_count": 197,
"metadata": {},
"outputs": [],
"source": [
"def prime_generator():\n",
" tt = 1\n",
" while True: # Mexri na sbhsei o hlios\n",
" if is_prime(tt):\n",
" yield tt\n",
" tt = tt + 1\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {},
"outputs": [],
"source": [
"g = prime_generator()"
]
},
{
"cell_type": "code",
"execution_count": 188,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 188,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 189,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 190,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 190,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 191,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 191,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 192,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 192,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 193,
"metadata": {},
"outputs": [],
"source": [
"g = prime_generator()"
]
},
{
"cell_type": "code",
"execution_count": 194,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 194,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 199,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1,\n",
" 2,\n",
" 3,\n",
" 5,\n",
" 7,\n",
" 9,\n",
" 11,\n",
" 13,\n",
" 15,\n",
" 17,\n",
" 19,\n",
" 21,\n",
" 23,\n",
" 25,\n",
" 27,\n",
" 29,\n",
" 31,\n",
" 33,\n",
" 35,\n",
" 37,\n",
" 39,\n",
" 41,\n",
" 43,\n",
" 45,\n",
" 47,\n",
" 49,\n",
" 51,\n",
" 53,\n",
" 55,\n",
" 57,\n",
" 59,\n",
" 61,\n",
" 63,\n",
" 65,\n",
" 67,\n",
" 69,\n",
" 71,\n",
" 73,\n",
" 75,\n",
" 77,\n",
" 79,\n",
" 81,\n",
" 83,\n",
" 85,\n",
" 87,\n",
" 89,\n",
" 91,\n",
" 93,\n",
" 95,\n",
" 97,\n",
" 99,\n",
" 101,\n",
" 103,\n",
" 105,\n",
" 107,\n",
" 109,\n",
" 111,\n",
" 113,\n",
" 115,\n",
" 117,\n",
" 119,\n",
" 121,\n",
" 123,\n",
" 125,\n",
" 127,\n",
" 129,\n",
" 131,\n",
" 133,\n",
" 135,\n",
" 137,\n",
" 139,\n",
" 141,\n",
" 143,\n",
" 145,\n",
" 147,\n",
" 149,\n",
" 151,\n",
" 153,\n",
" 155,\n",
" 157,\n",
" 159,\n",
" 161,\n",
" 163,\n",
" 165,\n",
" 167,\n",
" 169,\n",
" 171,\n",
" 173,\n",
" 175,\n",
" 177,\n",
" 179,\n",
" 181,\n",
" 183,\n",
" 185,\n",
" 187,\n",
" 189,\n",
" 191,\n",
" 193,\n",
" 195,\n",
" 197,\n",
" 199,\n",
" 201,\n",
" 203,\n",
" 205,\n",
" 207,\n",
" 209,\n",
" 211,\n",
" 213,\n",
" 215,\n",
" 217,\n",
" 219,\n",
" 221,\n",
" 223,\n",
" 225,\n",
" 227,\n",
" 229,\n",
" 231,\n",
" 233,\n",
" 235,\n",
" 237,\n",
" 239,\n",
" 241,\n",
" 243,\n",
" 245,\n",
" 247,\n",
" 249,\n",
" 251,\n",
" 253,\n",
" 255,\n",
" 257,\n",
" 259,\n",
" 261,\n",
" 263,\n",
" 265,\n",
" 267,\n",
" 269,\n",
" 271,\n",
" 273,\n",
" 275,\n",
" 277,\n",
" 279,\n",
" 281,\n",
" 283,\n",
" 285,\n",
" 287,\n",
" 289,\n",
" 291,\n",
" 293,\n",
" 295,\n",
" 297,\n",
" 299,\n",
" 301,\n",
" 303,\n",
" 305,\n",
" 307,\n",
" 309,\n",
" 311,\n",
" 313,\n",
" 315,\n",
" 317,\n",
" 319,\n",
" 321,\n",
" 323,\n",
" 325,\n",
" 327,\n",
" 329,\n",
" 331,\n",
" 333,\n",
" 335,\n",
" 337,\n",
" 339,\n",
" 341,\n",
" 343,\n",
" 345,\n",
" 347,\n",
" 349,\n",
" 351,\n",
" 353,\n",
" 355,\n",
" 357,\n",
" 359,\n",
" 361,\n",
" 363,\n",
" 365,\n",
" 367,\n",
" 369,\n",
" 371,\n",
" 373,\n",
" 375,\n",
" 377,\n",
" 379,\n",
" 381,\n",
" 383,\n",
" 385,\n",
" 387,\n",
" 389,\n",
" 391,\n",
" 393,\n",
" 395,\n",
" 397,\n",
" 399,\n",
" 401,\n",
" 403,\n",
" 405,\n",
" 407,\n",
" 409,\n",
" 411,\n",
" 413,\n",
" 415,\n",
" 417,\n",
" 419,\n",
" 421,\n",
" 423,\n",
" 425,\n",
" 427,\n",
" 429,\n",
" 431,\n",
" 433,\n",
" 435,\n",
" 437,\n",
" 439,\n",
" 441,\n",
" 443,\n",
" 445,\n",
" 447,\n",
" 449,\n",
" 451,\n",
" 453,\n",
" 455,\n",
" 457,\n",
" 459,\n",
" 461,\n",
" 463,\n",
" 465,\n",
" 467,\n",
" 469,\n",
" 471,\n",
" 473,\n",
" 475,\n",
" 477,\n",
" 479,\n",
" 481,\n",
" 483,\n",
" 485,\n",
" 487,\n",
" 489,\n",
" 491,\n",
" 493,\n",
" 495,\n",
" 497,\n",
" 499,\n",
" 501,\n",
" 503,\n",
" 505,\n",
" 507,\n",
" 509,\n",
" 511,\n",
" 513,\n",
" 515,\n",
" 517,\n",
" 519,\n",
" 521,\n",
" 523,\n",
" 525,\n",
" 527,\n",
" 529,\n",
" 531,\n",
" 533,\n",
" 535,\n",
" 537,\n",
" 539,\n",
" 541,\n",
" 543,\n",
" 545,\n",
" 547,\n",
" 549,\n",
" 551,\n",
" 553,\n",
" 555,\n",
" 557,\n",
" 559,\n",
" 561,\n",
" 563,\n",
" 565,\n",
" 567,\n",
" 569,\n",
" 571,\n",
" 573,\n",
" 575,\n",
" 577,\n",
" 579,\n",
" 581,\n",
" 583,\n",
" 585,\n",
" 587,\n",
" 589,\n",
" 591,\n",
" 593,\n",
" 595,\n",
" 597,\n",
" 599,\n",
" 601,\n",
" 603,\n",
" 605,\n",
" 607,\n",
" 609,\n",
" 611,\n",
" 613,\n",
" 615,\n",
" 617,\n",
" 619,\n",
" 621,\n",
" 623,\n",
" 625,\n",
" 627,\n",
" 629,\n",
" 631,\n",
" 633,\n",
" 635,\n",
" 637,\n",
" 639,\n",
" 641,\n",
" 643,\n",
" 645,\n",
" 647,\n",
" 649,\n",
" 651,\n",
" 653,\n",
" 655,\n",
" 657,\n",
" 659,\n",
" 661,\n",
" 663,\n",
" 665,\n",
" 667,\n",
" 669,\n",
" 671,\n",
" 673,\n",
" 675,\n",
" 677,\n",
" 679,\n",
" 681,\n",
" 683,\n",
" 685,\n",
" 687,\n",
" 689,\n",
" 691,\n",
" 693,\n",
" 695,\n",
" 697,\n",
" 699,\n",
" 701,\n",
" 703,\n",
" 705,\n",
" 707,\n",
" 709,\n",
" 711,\n",
" 713,\n",
" 715,\n",
" 717,\n",
" 719,\n",
" 721,\n",
" 723,\n",
" 725,\n",
" 727,\n",
" 729,\n",
" 731,\n",
" 733,\n",
" 735,\n",
" 737,\n",
" 739,\n",
" 741,\n",
" 743,\n",
" 745,\n",
" 747,\n",
" 749,\n",
" 751,\n",
" 753,\n",
" 755,\n",
" 757,\n",
" 759,\n",
" 761,\n",
" 763,\n",
" 765,\n",
" 767,\n",
" 769,\n",
" 771,\n",
" 773,\n",
" 775,\n",
" 777,\n",
" 779,\n",
" 781,\n",
" 783,\n",
" 785,\n",
" 787,\n",
" 789,\n",
" 791,\n",
" 793,\n",
" 795,\n",
" 797,\n",
" 799,\n",
" 801,\n",
" 803,\n",
" 805,\n",
" 807,\n",
" 809,\n",
" 811,\n",
" 813,\n",
" 815,\n",
" 817,\n",
" 819,\n",
" 821,\n",
" 823,\n",
" 825,\n",
" 827,\n",
" 829,\n",
" 831,\n",
" 833,\n",
" 835,\n",
" 837,\n",
" 839,\n",
" 841,\n",
" 843,\n",
" 845,\n",
" 847,\n",
" 849,\n",
" 851,\n",
" 853,\n",
" 855,\n",
" 857,\n",
" 859,\n",
" 861,\n",
" 863,\n",
" 865,\n",
" 867,\n",
" 869,\n",
" 871,\n",
" 873,\n",
" 875,\n",
" 877,\n",
" 879,\n",
" 881,\n",
" 883,\n",
" 885,\n",
" 887,\n",
" 889,\n",
" 891,\n",
" 893,\n",
" 895,\n",
" 897,\n",
" 899,\n",
" 901,\n",
" 903,\n",
" 905,\n",
" 907,\n",
" 909,\n",
" 911,\n",
" 913,\n",
" 915,\n",
" 917,\n",
" 919,\n",
" 921,\n",
" 923,\n",
" 925,\n",
" 927,\n",
" 929,\n",
" 931,\n",
" 933,\n",
" 935,\n",
" 937,\n",
" 939,\n",
" 941,\n",
" 943,\n",
" 945,\n",
" 947,\n",
" 949,\n",
" 951,\n",
" 953,\n",
" 955,\n",
" 957,\n",
" 959,\n",
" 961,\n",
" 963,\n",
" 965,\n",
" 967,\n",
" 969,\n",
" 971,\n",
" 973,\n",
" 975,\n",
" 977,\n",
" 979,\n",
" 981,\n",
" 983,\n",
" 985,\n",
" 987,\n",
" 989,\n",
" 991,\n",
" 993,\n",
" 995,\n",
" 997,\n",
" 999,\n",
" 1001,\n",
" 1003,\n",
" 1005,\n",
" 1007,\n",
" 1009,\n",
" 1011,\n",
" 1013,\n",
" 1015,\n",
" 1017,\n",
" 1019,\n",
" 1021,\n",
" 1023,\n",
" 1025,\n",
" 1027,\n",
" 1029,\n",
" 1031,\n",
" 1033,\n",
" 1035,\n",
" 1037,\n",
" 1039,\n",
" 1041,\n",
" 1043,\n",
" 1045,\n",
" 1047,\n",
" 1049,\n",
" 1051,\n",
" 1053,\n",
" 1055,\n",
" 1057,\n",
" 1059,\n",
" 1061,\n",
" 1063,\n",
" 1065,\n",
" 1067,\n",
" 1069,\n",
" 1071,\n",
" 1073,\n",
" 1075,\n",
" 1077,\n",
" 1079,\n",
" 1081,\n",
" 1083,\n",
" 1085,\n",
" 1087,\n",
" 1089,\n",
" 1091,\n",
" 1093,\n",
" 1095,\n",
" 1097,\n",
" 1099,\n",
" 1101,\n",
" 1103,\n",
" 1105,\n",
" 1107,\n",
" 1109,\n",
" 1111,\n",
" 1113,\n",
" 1115,\n",
" 1117,\n",
" 1119,\n",
" 1121,\n",
" 1123,\n",
" 1125,\n",
" 1127,\n",
" 1129,\n",
" 1131,\n",
" 1133,\n",
" 1135,\n",
" 1137,\n",
" 1139,\n",
" 1141,\n",
" 1143,\n",
" 1145,\n",
" 1147,\n",
" 1149,\n",
" 1151,\n",
" 1153,\n",
" 1155,\n",
" 1157,\n",
" 1159,\n",
" 1161,\n",
" 1163,\n",
" 1165,\n",
" 1167,\n",
" 1169,\n",
" 1171,\n",
" 1173,\n",
" 1175,\n",
" 1177,\n",
" 1179,\n",
" 1181,\n",
" 1183,\n",
" 1185,\n",
" 1187,\n",
" 1189,\n",
" 1191,\n",
" 1193,\n",
" 1195,\n",
" 1197,\n",
" 1199,\n",
" 1201,\n",
" 1203,\n",
" 1205,\n",
" 1207,\n",
" 1209,\n",
" 1211,\n",
" 1213,\n",
" 1215,\n",
" 1217,\n",
" 1219,\n",
" 1221,\n",
" 1223,\n",
" 1225,\n",
" 1227,\n",
" 1229,\n",
" 1231,\n",
" 1233,\n",
" 1235,\n",
" 1237,\n",
" 1239,\n",
" 1241,\n",
" 1243,\n",
" 1245,\n",
" 1247,\n",
" 1249,\n",
" 1251,\n",
" 1253,\n",
" 1255,\n",
" 1257,\n",
" 1259,\n",
" 1261,\n",
" 1263,\n",
" 1265,\n",
" 1267,\n",
" 1269,\n",
" 1271,\n",
" 1273,\n",
" 1275,\n",
" 1277,\n",
" 1279,\n",
" 1281,\n",
" 1283,\n",
" 1285,\n",
" 1287,\n",
" 1289,\n",
" 1291,\n",
" 1293,\n",
" 1295,\n",
" 1297,\n",
" 1299,\n",
" 1301,\n",
" 1303,\n",
" 1305,\n",
" 1307,\n",
" 1309,\n",
" 1311,\n",
" 1313,\n",
" 1315,\n",
" 1317,\n",
" 1319,\n",
" 1321,\n",
" 1323,\n",
" 1325,\n",
" 1327,\n",
" 1329,\n",
" 1331,\n",
" 1333,\n",
" 1335,\n",
" 1337,\n",
" 1339,\n",
" 1341,\n",
" 1343,\n",
" 1345,\n",
" 1347,\n",
" 1349,\n",
" 1351,\n",
" 1353,\n",
" 1355,\n",
" 1357,\n",
" 1359,\n",
" 1361,\n",
" 1363,\n",
" 1365,\n",
" 1367,\n",
" 1369,\n",
" 1371,\n",
" 1373,\n",
" 1375,\n",
" 1377,\n",
" 1379,\n",
" 1381,\n",
" 1383,\n",
" 1385,\n",
" 1387,\n",
" 1389,\n",
" 1391,\n",
" 1393,\n",
" 1395,\n",
" 1397,\n",
" 1399,\n",
" 1401,\n",
" 1403,\n",
" 1405,\n",
" 1407,\n",
" 1409,\n",
" 1411,\n",
" 1413,\n",
" 1415,\n",
" 1417,\n",
" 1419,\n",
" 1421,\n",
" 1423,\n",
" 1425,\n",
" 1427,\n",
" 1429,\n",
" 1431,\n",
" 1433,\n",
" 1435,\n",
" 1437,\n",
" 1439,\n",
" 1441,\n",
" 1443,\n",
" 1445,\n",
" 1447,\n",
" 1449,\n",
" 1451,\n",
" 1453,\n",
" 1455,\n",
" 1457,\n",
" 1459,\n",
" 1461,\n",
" 1463,\n",
" 1465,\n",
" 1467,\n",
" 1469,\n",
" 1471,\n",
" 1473,\n",
" 1475,\n",
" 1477,\n",
" 1479,\n",
" 1481,\n",
" 1483,\n",
" 1485,\n",
" 1487,\n",
" 1489,\n",
" 1491,\n",
" 1493,\n",
" 1495,\n",
" 1497,\n",
" 1499,\n",
" 1501,\n",
" 1503,\n",
" 1505,\n",
" 1507,\n",
" 1509,\n",
" 1511,\n",
" 1513,\n",
" 1515,\n",
" 1517,\n",
" 1519,\n",
" 1521,\n",
" 1523,\n",
" 1525,\n",
" 1527,\n",
" 1529,\n",
" 1531,\n",
" 1533,\n",
" 1535,\n",
" 1537,\n",
" 1539,\n",
" 1541,\n",
" 1543,\n",
" 1545,\n",
" 1547,\n",
" 1549,\n",
" 1551,\n",
" 1553,\n",
" 1555,\n",
" 1557,\n",
" 1559,\n",
" 1561,\n",
" 1563,\n",
" 1565,\n",
" 1567,\n",
" 1569,\n",
" 1571,\n",
" 1573,\n",
" 1575,\n",
" 1577,\n",
" 1579,\n",
" 1581,\n",
" 1583,\n",
" 1585,\n",
" 1587,\n",
" 1589,\n",
" 1591,\n",
" 1593,\n",
" 1595,\n",
" 1597,\n",
" 1599,\n",
" 1601,\n",
" 1603,\n",
" 1605,\n",
" 1607,\n",
" 1609,\n",
" 1611,\n",
" 1613,\n",
" 1615,\n",
" 1617,\n",
" 1619,\n",
" 1621,\n",
" 1623,\n",
" 1625,\n",
" 1627,\n",
" 1629,\n",
" 1631,\n",
" 1633,\n",
" 1635,\n",
" 1637,\n",
" 1639,\n",
" 1641,\n",
" 1643,\n",
" 1645,\n",
" 1647,\n",
" 1649,\n",
" 1651,\n",
" 1653,\n",
" 1655,\n",
" 1657,\n",
" 1659,\n",
" 1661,\n",
" 1663,\n",
" 1665,\n",
" 1667,\n",
" 1669,\n",
" 1671,\n",
" 1673,\n",
" 1675,\n",
" 1677,\n",
" 1679,\n",
" 1681,\n",
" 1683,\n",
" 1685,\n",
" 1687,\n",
" 1689,\n",
" 1691,\n",
" 1693,\n",
" 1695,\n",
" 1697,\n",
" 1699,\n",
" 1701,\n",
" 1703,\n",
" 1705,\n",
" 1707,\n",
" 1709,\n",
" 1711,\n",
" 1713,\n",
" 1715,\n",
" 1717,\n",
" 1719,\n",
" 1721,\n",
" 1723,\n",
" 1725,\n",
" 1727,\n",
" 1729,\n",
" 1731,\n",
" 1733,\n",
" 1735,\n",
" 1737,\n",
" 1739,\n",
" 1741,\n",
" 1743,\n",
" 1745,\n",
" 1747,\n",
" 1749,\n",
" 1751,\n",
" 1753,\n",
" 1755,\n",
" 1757,\n",
" 1759,\n",
" 1761,\n",
" 1763,\n",
" 1765,\n",
" 1767,\n",
" 1769,\n",
" 1771,\n",
" 1773,\n",
" 1775,\n",
" 1777,\n",
" 1779,\n",
" 1781,\n",
" 1783,\n",
" 1785,\n",
" 1787,\n",
" 1789,\n",
" 1791,\n",
" 1793,\n",
" 1795,\n",
" 1797,\n",
" 1799,\n",
" 1801,\n",
" 1803,\n",
" 1805,\n",
" 1807,\n",
" 1809,\n",
" 1811,\n",
" 1813,\n",
" 1815,\n",
" 1817,\n",
" 1819,\n",
" 1821,\n",
" 1823,\n",
" 1825,\n",
" 1827,\n",
" 1829,\n",
" 1831,\n",
" 1833,\n",
" 1835,\n",
" 1837,\n",
" 1839,\n",
" 1841,\n",
" 1843,\n",
" 1845,\n",
" 1847,\n",
" 1849,\n",
" 1851,\n",
" 1853,\n",
" 1855,\n",
" 1857,\n",
" 1859,\n",
" 1861,\n",
" 1863,\n",
" 1865,\n",
" 1867,\n",
" 1869,\n",
" 1871,\n",
" 1873,\n",
" 1875,\n",
" 1877,\n",
" 1879,\n",
" 1881,\n",
" 1883,\n",
" 1885,\n",
" 1887,\n",
" 1889,\n",
" 1891,\n",
" 1893,\n",
" 1895,\n",
" 1897,\n",
" 1899,\n",
" 1901,\n",
" 1903,\n",
" 1905,\n",
" 1907,\n",
" 1909,\n",
" 1911,\n",
" 1913,\n",
" 1915,\n",
" 1917,\n",
" 1919,\n",
" 1921,\n",
" 1923,\n",
" 1925,\n",
" 1927,\n",
" 1929,\n",
" 1931,\n",
" 1933,\n",
" 1935,\n",
" 1937,\n",
" 1939,\n",
" 1941,\n",
" 1943,\n",
" 1945,\n",
" 1947,\n",
" 1949,\n",
" 1951,\n",
" 1953,\n",
" 1955,\n",
" 1957,\n",
" 1959,\n",
" 1961,\n",
" 1963,\n",
" 1965,\n",
" 1967,\n",
" 1969,\n",
" 1971,\n",
" 1973,\n",
" 1975,\n",
" 1977,\n",
" 1979,\n",
" 1981,\n",
" 1983,\n",
" 1985,\n",
" 1987,\n",
" 1989,\n",
" 1991,\n",
" 1993,\n",
" 1995,\n",
" 1997]"
]
},
"execution_count": 199,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = prime_generator()\n",
"[next(g) for x in range(1000)]"
]
},
{
"cell_type": "code",
"execution_count": 200,
"metadata": {},
"outputs": [],
"source": [
"r = []\n",
"for p in prime_generator():\n",
" if p>1000:\n",
" break\n",
" r.append(p)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 201,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1,\n",
" 2,\n",
" 3,\n",
" 5,\n",
" 7,\n",
" 9,\n",
" 11,\n",
" 13,\n",
" 15,\n",
" 17,\n",
" 19,\n",
" 21,\n",
" 23,\n",
" 25,\n",
" 27,\n",
" 29,\n",
" 31,\n",
" 33,\n",
" 35,\n",
" 37,\n",
" 39,\n",
" 41,\n",
" 43,\n",
" 45,\n",
" 47,\n",
" 49,\n",
" 51,\n",
" 53,\n",
" 55,\n",
" 57,\n",
" 59,\n",
" 61,\n",
" 63,\n",
" 65,\n",
" 67,\n",
" 69,\n",
" 71,\n",
" 73,\n",
" 75,\n",
" 77,\n",
" 79,\n",
" 81,\n",
" 83,\n",
" 85,\n",
" 87,\n",
" 89,\n",
" 91,\n",
" 93,\n",
" 95,\n",
" 97,\n",
" 99,\n",
" 101,\n",
" 103,\n",
" 105,\n",
" 107,\n",
" 109,\n",
" 111,\n",
" 113,\n",
" 115,\n",
" 117,\n",
" 119,\n",
" 121,\n",
" 123,\n",
" 125,\n",
" 127,\n",
" 129,\n",
" 131,\n",
" 133,\n",
" 135,\n",
" 137,\n",
" 139,\n",
" 141,\n",
" 143,\n",
" 145,\n",
" 147,\n",
" 149,\n",
" 151,\n",
" 153,\n",
" 155,\n",
" 157,\n",
" 159,\n",
" 161,\n",
" 163,\n",
" 165,\n",
" 167,\n",
" 169,\n",
" 171,\n",
" 173,\n",
" 175,\n",
" 177,\n",
" 179,\n",
" 181,\n",
" 183,\n",
" 185,\n",
" 187,\n",
" 189,\n",
" 191,\n",
" 193,\n",
" 195,\n",
" 197,\n",
" 199,\n",
" 201,\n",
" 203,\n",
" 205,\n",
" 207,\n",
" 209,\n",
" 211,\n",
" 213,\n",
" 215,\n",
" 217,\n",
" 219,\n",
" 221,\n",
" 223,\n",
" 225,\n",
" 227,\n",
" 229,\n",
" 231,\n",
" 233,\n",
" 235,\n",
" 237,\n",
" 239,\n",
" 241,\n",
" 243,\n",
" 245,\n",
" 247,\n",
" 249,\n",
" 251,\n",
" 253,\n",
" 255,\n",
" 257,\n",
" 259,\n",
" 261,\n",
" 263,\n",
" 265,\n",
" 267,\n",
" 269,\n",
" 271,\n",
" 273,\n",
" 275,\n",
" 277,\n",
" 279,\n",
" 281,\n",
" 283,\n",
" 285,\n",
" 287,\n",
" 289,\n",
" 291,\n",
" 293,\n",
" 295,\n",
" 297,\n",
" 299,\n",
" 301,\n",
" 303,\n",
" 305,\n",
" 307,\n",
" 309,\n",
" 311,\n",
" 313,\n",
" 315,\n",
" 317,\n",
" 319,\n",
" 321,\n",
" 323,\n",
" 325,\n",
" 327,\n",
" 329,\n",
" 331,\n",
" 333,\n",
" 335,\n",
" 337,\n",
" 339,\n",
" 341,\n",
" 343,\n",
" 345,\n",
" 347,\n",
" 349,\n",
" 351,\n",
" 353,\n",
" 355,\n",
" 357,\n",
" 359,\n",
" 361,\n",
" 363,\n",
" 365,\n",
" 367,\n",
" 369,\n",
" 371,\n",
" 373,\n",
" 375,\n",
" 377,\n",
" 379,\n",
" 381,\n",
" 383,\n",
" 385,\n",
" 387,\n",
" 389,\n",
" 391,\n",
" 393,\n",
" 395,\n",
" 397,\n",
" 399,\n",
" 401,\n",
" 403,\n",
" 405,\n",
" 407,\n",
" 409,\n",
" 411,\n",
" 413,\n",
" 415,\n",
" 417,\n",
" 419,\n",
" 421,\n",
" 423,\n",
" 425,\n",
" 427,\n",
" 429,\n",
" 431,\n",
" 433,\n",
" 435,\n",
" 437,\n",
" 439,\n",
" 441,\n",
" 443,\n",
" 445,\n",
" 447,\n",
" 449,\n",
" 451,\n",
" 453,\n",
" 455,\n",
" 457,\n",
" 459,\n",
" 461,\n",
" 463,\n",
" 465,\n",
" 467,\n",
" 469,\n",
" 471,\n",
" 473,\n",
" 475,\n",
" 477,\n",
" 479,\n",
" 481,\n",
" 483,\n",
" 485,\n",
" 487,\n",
" 489,\n",
" 491,\n",
" 493,\n",
" 495,\n",
" 497,\n",
" 499,\n",
" 501,\n",
" 503,\n",
" 505,\n",
" 507,\n",
" 509,\n",
" 511,\n",
" 513,\n",
" 515,\n",
" 517,\n",
" 519,\n",
" 521,\n",
" 523,\n",
" 525,\n",
" 527,\n",
" 529,\n",
" 531,\n",
" 533,\n",
" 535,\n",
" 537,\n",
" 539,\n",
" 541,\n",
" 543,\n",
" 545,\n",
" 547,\n",
" 549,\n",
" 551,\n",
" 553,\n",
" 555,\n",
" 557,\n",
" 559,\n",
" 561,\n",
" 563,\n",
" 565,\n",
" 567,\n",
" 569,\n",
" 571,\n",
" 573,\n",
" 575,\n",
" 577,\n",
" 579,\n",
" 581,\n",
" 583,\n",
" 585,\n",
" 587,\n",
" 589,\n",
" 591,\n",
" 593,\n",
" 595,\n",
" 597,\n",
" 599,\n",
" 601,\n",
" 603,\n",
" 605,\n",
" 607,\n",
" 609,\n",
" 611,\n",
" 613,\n",
" 615,\n",
" 617,\n",
" 619,\n",
" 621,\n",
" 623,\n",
" 625,\n",
" 627,\n",
" 629,\n",
" 631,\n",
" 633,\n",
" 635,\n",
" 637,\n",
" 639,\n",
" 641,\n",
" 643,\n",
" 645,\n",
" 647,\n",
" 649,\n",
" 651,\n",
" 653,\n",
" 655,\n",
" 657,\n",
" 659,\n",
" 661,\n",
" 663,\n",
" 665,\n",
" 667,\n",
" 669,\n",
" 671,\n",
" 673,\n",
" 675,\n",
" 677,\n",
" 679,\n",
" 681,\n",
" 683,\n",
" 685,\n",
" 687,\n",
" 689,\n",
" 691,\n",
" 693,\n",
" 695,\n",
" 697,\n",
" 699,\n",
" 701,\n",
" 703,\n",
" 705,\n",
" 707,\n",
" 709,\n",
" 711,\n",
" 713,\n",
" 715,\n",
" 717,\n",
" 719,\n",
" 721,\n",
" 723,\n",
" 725,\n",
" 727,\n",
" 729,\n",
" 731,\n",
" 733,\n",
" 735,\n",
" 737,\n",
" 739,\n",
" 741,\n",
" 743,\n",
" 745,\n",
" 747,\n",
" 749,\n",
" 751,\n",
" 753,\n",
" 755,\n",
" 757,\n",
" 759,\n",
" 761,\n",
" 763,\n",
" 765,\n",
" 767,\n",
" 769,\n",
" 771,\n",
" 773,\n",
" 775,\n",
" 777,\n",
" 779,\n",
" 781,\n",
" 783,\n",
" 785,\n",
" 787,\n",
" 789,\n",
" 791,\n",
" 793,\n",
" 795,\n",
" 797,\n",
" 799,\n",
" 801,\n",
" 803,\n",
" 805,\n",
" 807,\n",
" 809,\n",
" 811,\n",
" 813,\n",
" 815,\n",
" 817,\n",
" 819,\n",
" 821,\n",
" 823,\n",
" 825,\n",
" 827,\n",
" 829,\n",
" 831,\n",
" 833,\n",
" 835,\n",
" 837,\n",
" 839,\n",
" 841,\n",
" 843,\n",
" 845,\n",
" 847,\n",
" 849,\n",
" 851,\n",
" 853,\n",
" 855,\n",
" 857,\n",
" 859,\n",
" 861,\n",
" 863,\n",
" 865,\n",
" 867,\n",
" 869,\n",
" 871,\n",
" 873,\n",
" 875,\n",
" 877,\n",
" 879,\n",
" 881,\n",
" 883,\n",
" 885,\n",
" 887,\n",
" 889,\n",
" 891,\n",
" 893,\n",
" 895,\n",
" 897,\n",
" 899,\n",
" 901,\n",
" 903,\n",
" 905,\n",
" 907,\n",
" 909,\n",
" 911,\n",
" 913,\n",
" 915,\n",
" 917,\n",
" 919,\n",
" 921,\n",
" 923,\n",
" 925,\n",
" 927,\n",
" 929,\n",
" 931,\n",
" 933,\n",
" 935,\n",
" 937,\n",
" 939,\n",
" 941,\n",
" 943,\n",
" 945,\n",
" 947,\n",
" 949,\n",
" 951,\n",
" 953,\n",
" 955,\n",
" 957,\n",
" 959,\n",
" 961,\n",
" 963,\n",
" 965,\n",
" 967,\n",
" 969,\n",
" 971,\n",
" 973,\n",
" 975,\n",
" 977,\n",
" 979,\n",
" 981,\n",
" 983,\n",
" 985,\n",
" 987,\n",
" 989,\n",
" 991,\n",
" 993,\n",
" 995,\n",
" 997,\n",
" 999]"
]
},
"execution_count": 201,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r"
]
},
{
"cell_type": "code",
"execution_count": 204,
"metadata": {},
"outputs": [],
"source": [
"def f2():\n",
" yield \"mitsos\"\n",
" yield \"kwstas\"\n",
" yield \"maria\""
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mitsos\n",
"kwstas\n",
"maria\n"
]
}
],
"source": [
"for x in f2():\n",
" print (x)"
]
},
{
"cell_type": "code",
"execution_count": 207,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['mitsos', 'kwstas', 'maria']"
]
},
"execution_count": 207,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(f2())"
]
},
{
"cell_type": "code",
"execution_count": 208,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['mitsos', 'kwstas', 'maria']"
]
},
"execution_count": 208,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x for x in f2()]"
]
},
{
"cell_type": "code",
"execution_count": 209,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['mitsos', 'maria']"
]
},
"execution_count": 209,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x for x in f2() if x[0]=='m']"
]
},
{
"cell_type": "code",
"execution_count": 210,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"def f():\n",
" return random.random()"
]
},
{
"cell_type": "code",
"execution_count": 219,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.12101745240192052"
]
},
"execution_count": 219,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f()"
]
},
{
"cell_type": "code",
"execution_count": 222,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9999999994152604"
]
},
"execution_count": 222,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# TOO MUCH MEMORY!!!!\n",
"max([f() for x in range(100000000)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Generator comprehension"
]
},
{
"cell_type": "code",
"execution_count": 223,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9999999749745118"
]
},
"execution_count": 223,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"max( (f() for x in range(100000000)) )"
]
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.9999999859372565\n"
]
}
],
"source": [
"my_max = 0.0\n",
"for x in range(100000000):\n",
" r_number = f()\n",
" if r_number > my_max:\n",
" my_max = r_number\n",
"\n",
"print(my_max)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 226,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"49996491.862183705"
]
},
"execution_count": 226,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum( (f() for x in range(100000000)) )"
]
},
{
"cell_type": "code",
"execution_count": 229,
"metadata": {},
"outputs": [],
"source": [
"name = 'mitsos'"
]
},
{
"cell_type": "code",
"execution_count": 228,
"metadata": {},
"outputs": [],
"source": [
"name = 'kostas'"
]
},
{
"cell_type": "code",
"execution_count": 231,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mitsos\n"
]
}
],
"source": [
"print (name)"
]
},
{
"cell_type": "code",
"execution_count": 235,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.031144083432153247"
]
},
"execution_count": 235,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f()"
]
},
{
"cell_type": "code",
"execution_count": 236,
"metadata": {},
"outputs": [],
"source": [
"class Person:\n",
" pass\n"
]
},
{
"cell_type": "code",
"execution_count": 237,
"metadata": {},
"outputs": [],
"source": [
"mitsos = Person()"
]
},
{
"cell_type": "code",
"execution_count": 238,
"metadata": {},
"outputs": [],
"source": [
"mitsos.name = 'mitsos'"
]
},
{
"cell_type": "code",
"execution_count": 239,
"metadata": {},
"outputs": [],
"source": [
"class Country:\n",
" pass\n",
"\n",
"greece = Country()\n",
"greece.name = 'Greece'\n",
"greece.pop = 23423423424"
]
},
{
"cell_type": "code",
"execution_count": 240,
"metadata": {},
"outputs": [],
"source": [
"a = [1,2,3]"
]
},
{
"cell_type": "code",
"execution_count": 241,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"__main__.Country"
]
},
"execution_count": 241,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(greece)"
]
},
{
"cell_type": "code",
"execution_count": 242,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 242,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"cell_type": "code",
"execution_count": 245,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__add__',\n",
" '__class__',\n",
" '__contains__',\n",
" '__delattr__',\n",
" '__delitem__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__iadd__',\n",
" '__imul__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__iter__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__mul__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__reversed__',\n",
" '__rmul__',\n",
" '__setattr__',\n",
" '__setitem__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" 'append',\n",
" 'clear',\n",
" 'copy',\n",
" 'count',\n",
" 'extend',\n",
" 'index',\n",
" 'insert',\n",
" 'pop',\n",
" 'remove',\n",
" 'reverse',\n",
" 'sort']"
]
},
"execution_count": 245,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(a)"
]
},
{
"cell_type": "code",
"execution_count": 246,
"metadata": {},
"outputs": [],
"source": [
"a = [1,2,3]\n",
"b = [5,6,7]"
]
},
{
"cell_type": "code",
"execution_count": 247,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 247,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(a)"
]
},
{
"cell_type": "code",
"execution_count": 248,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 248,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.__len__()"
]
},
{
"cell_type": "code",
"execution_count": 249,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 249,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5+6"
]
},
{
"cell_type": "code",
"execution_count": 253,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 253,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(5).__add__(6)"
]
},
{
"cell_type": "code",
"execution_count": 255,
"metadata": {},
"outputs": [],
"source": [
"def f(a, b, operation):\n",
" return operation(a,b)"
]
},
{
"cell_type": "code",
"execution_count": 256,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 256,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(5,4, lambda x,y:x+y)"
]
},
{
"cell_type": "code",
"execution_count": 257,
"metadata": {},
"outputs": [],
"source": [
"def my_add(a,b):\n",
" if '__add__' in dir(a) and '__add__' in dir(b):\n",
" return a+b\n",
" else:\n",
" print('DOES NOT ADD')\n"
]
},
{
"cell_type": "code",
"execution_count": 259,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 259,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_add(6,5)"
]
},
{
"cell_type": "code",
"execution_count": 260,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 6, 7, 8]"
]
},
"execution_count": 260,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_add([1,2,3], [6,7,8])"
]
},
{
"cell_type": "code",
"execution_count": 262,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DOES NOT ADD\n"
]
}
],
"source": [
"my_add({'a':1}, {'b': 2})"
]
},
{
"cell_type": "code",
"execution_count": 263,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-263-79404d5c5cee>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0msec\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0msec\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"sec = 1\n",
"while True:\n",
" print (sec)\n",
" sec += 1\n",
" time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": 267,
"metadata": {},
"outputs": [],
"source": [
"a = '''\n",
"82.07\t94.21\t147.1\n",
"85.35\t95.32\t173.3\n",
"88.48\t92.24\t79.84\n",
"79.49\t91.12\t92.68\n",
"82.35\t95.03\t129.1\n",
"82.68\t93.74\t150.9\n",
"83.49\t88.98\t95.14\n",
"84.85\t95.32\t158.9\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 275,
"metadata": {},
"outputs": [],
"source": [
"tmp = [list(map(float, x.split())) for x in a.split('\\n') if x]"
]
},
{
"cell_type": "code",
"execution_count": 277,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[82.07, 94.21, 147.1],\n",
" [85.35, 95.32, 173.3],\n",
" [88.48, 92.24, 79.84],\n",
" [79.49, 91.12, 92.68],\n",
" [82.35, 95.03, 129.1],\n",
" [82.68, 93.74, 150.9],\n",
" [83.49, 88.98, 95.14],\n",
" [84.85, 95.32, 158.9]]"
]
},
"execution_count": 277,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tmp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Which line (entry) has the lower mean ?"
]
},
{
"cell_type": "code",
"execution_count": 279,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[88.48, 92.24, 79.84]"
]
},
"execution_count": 279,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"min(tmp, key=lambda x : sum(x)/len(x))"
]
},
{
"cell_type": "code",
"execution_count": 280,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, [88.48, 92.24, 79.84])"
]
},
"execution_count": 280,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"min(enumerate(tmp), key=lambda x: sum(x[1])/len(x[1]))"
]
},
{
"cell_type": "code",
"execution_count": 284,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, [88.48, 92.24, 79.84])"
]
},
"execution_count": 284,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"min(enumerate(tmp), key=lambda x: sum(x[1])/len(x[1]))"
]
},
{
"cell_type": "code",
"execution_count": 285,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, [82.07, 94.21, 147.1]),\n",
" (1, [85.35, 95.32, 173.3]),\n",
" (2, [88.48, 92.24, 79.84]),\n",
" (3, [79.49, 91.12, 92.68]),\n",
" (4, [82.35, 95.03, 129.1]),\n",
" (5, [82.68, 93.74, 150.9]),\n",
" (6, [83.49, 88.98, 95.14]),\n",
" (7, [84.85, 95.32, 158.9])]"
]
},
"execution_count": 285,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(enumerate(tmp))"
]
},
{
"cell_type": "code",
"execution_count": 286,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"maria\n"
]
}
],
"source": [
"if '':\n",
" print ('kwstas')\n",
"else:\n",
" print ('maria')"
]
},
{
"cell_type": "code",
"execution_count": 287,
"metadata": {},
"outputs": [],
"source": [
"a = '''\n",
"82.07\t94.21\t147.1\n",
"85.35\t95.32\t173.3\n",
"88.48\t92.24\t79.84\n",
"79.49\t91.12\t92.68\n",
"82.35\t95.03\t129.1\n",
"82.68\t93.74\t150.9\n",
"83.49\t88.98\t95.14\n",
"84.85\t95.32\t158.9\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 290,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['82.07\\t94.21\\t147.1',\n",
" '85.35\\t95.32\\t173.3',\n",
" '88.48\\t92.24\\t79.84',\n",
" '79.49\\t91.12\\t92.68',\n",
" '82.35\\t95.03\\t129.1',\n",
" '82.68\\t93.74\\t150.9',\n",
" '83.49\\t88.98\\t95.14',\n",
" '84.85\\t95.32\\t158.9']"
]
},
"execution_count": 290,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.split('\\n')[1:-1]"
]
},
{
"cell_type": "code",
"execution_count": 291,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['82.07\\t94.21\\t147.1',\n",
" '85.35\\t95.32\\t173.3',\n",
" '88.48\\t92.24\\t79.84',\n",
" '79.49\\t91.12\\t92.68',\n",
" '82.35\\t95.03\\t129.1',\n",
" '82.68\\t93.74\\t150.9',\n",
" '83.49\\t88.98\\t95.14',\n",
" '84.85\\t95.32\\t158.9']"
]
},
"execution_count": 291,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x for x in a.split('\\n') if x]"
]
},
{
"cell_type": "code",
"execution_count": 294,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['82.07\\t94.21\\t147.1',\n",
" '85.35\\t95.32\\t173.3',\n",
" '88.48\\t92.24\\t79.84',\n",
" '79.49\\t91.12\\t92.68',\n",
" '82.35\\t95.03\\t129.1',\n",
" '82.68\\t93.74\\t150.9',\n",
" '83.49\\t88.98\\t95.14',\n",
" '84.85\\t95.32\\t158.9']"
]
},
"execution_count": 294,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(filter(lambda x :x, a.split('\\n')))"
]
},
{
"cell_type": "code",
"execution_count": 295,
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return x%2==0 "
]
},
{
"cell_type": "code",
"execution_count": 296,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 296,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(5)"
]
},
{
"cell_type": "code",
"execution_count": 297,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 297,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(4)"
]
},
{
"cell_type": "code",
"execution_count": 298,
"metadata": {},
"outputs": [],
"source": [
"a =[1,2,3,4,3,2,1,2,3,4,5,6]"
]
},
{
"cell_type": "code",
"execution_count": 300,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 2, 2, 4, 6]"
]
},
"execution_count": 300,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(filter(f, a))"
]
},
{
"cell_type": "code",
"execution_count": 304,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 2, 2, 4, 6]"
]
},
"execution_count": 304,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(filter(lambda x: not x%2, a))"
]
},
{
"cell_type": "code",
"execution_count": 306,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[False, True, False, True, False, True, False, True, False, True, False, True]"
]
},
"execution_count": 306,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda x: not x%2, a))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ISODYNAMA\n",
"[f(x) for x in array]\n",
"map(f, array)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ISODYNAMA\n",
"[x for x in array if f(x)]\n",
"filter(f, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ISODYNAMA\n",
"[g(x) for x in array if f(x)]\n",
"map(g, filter(f,x))"
]
},
{
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment