Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created February 11, 2021 16:35
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 ischurov/a19c494a50bfc5bb343957b963ce47f4 to your computer and use it in GitHub Desktop.
Save ischurov/a19c494a50bfc5bb343957b963ce47f4 to your computer and use it in GitHub Desktop.
lesson09.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"id": "popular-booth",
"cell_type": "markdown",
"source": "## Наследование в Python"
},
{
"metadata": {
"trusted": false
},
"id": "ahead-breakfast",
"cell_type": "code",
"source": "class User():\n def __init__(self, email, first_name, last_name):\n self.email = email\n self.first_name = first_name\n self.last_name = last_name\n self.banned = False\n def say_hello(self):\n if self.banned:\n print(\"User is banned\")\n else:\n print(f\"Hello, my name is {self.first_name} {self.last_name}\")\n def __str__(self):\n return f\"<{self.first_name} {self.last_name}>\"",
"execution_count": 51,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "technical-museum",
"cell_type": "code",
"source": "alice = User(\"alice@wonderland\", \"Alice\", \"Smith\")",
"execution_count": 52,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "spanish-crystal",
"cell_type": "code",
"source": "alice.say_hello()",
"execution_count": 53,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello, my name is Alice Smith\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "macro-connecticut",
"cell_type": "code",
"source": "class Admin(User):\n def ban(self, other):\n if isinstance(other, Admin):\n print(\"Cannot ban another admin\")\n elif not isinstance(other, User):\n print(f\"I don't know how to ban {other}\")\n else:\n other.banned = True\n print(f\"{self} banned {other}\")\n def __str__(self):\n return f\"<** {super().__str__()} **>\"",
"execution_count": 59,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "eastern-skiing",
"cell_type": "code",
"source": "mark = Admin(\"mark@fb\", \"Mark\", \"Zuckerberg\")",
"execution_count": 60,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "duplicate-visitor",
"cell_type": "code",
"source": "mark.ban(alice)",
"execution_count": 61,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "<** <Mark Zuckerberg> **> banned <Alice Smith>\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "geological-illustration",
"cell_type": "code",
"source": "alice.banned",
"execution_count": 62,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "fundamental-community",
"cell_type": "code",
"source": "alice.say_hello()",
"execution_count": 63,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "User is banned\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "extraordinary-happiness",
"cell_type": "code",
"source": "mark.say_hello()",
"execution_count": 64,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello, my name is Mark Zuckerberg\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "dental-decimal",
"cell_type": "code",
"source": "mark.ban(\"Alice\")",
"execution_count": 65,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "I don't know how to ban Alice\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "complicated-planning",
"cell_type": "code",
"source": "isinstance(\"Alice\", User)",
"execution_count": 66,
"outputs": [
{
"data": {
"text/plain": "False"
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "neutral-blame",
"cell_type": "code",
"source": "isinstance(\"Alice\", str)",
"execution_count": 67,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "personalized-running",
"cell_type": "code",
"source": "isinstance(mark, User)",
"execution_count": 68,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "legitimate-arizona",
"cell_type": "code",
"source": "type(mark)",
"execution_count": 69,
"outputs": [
{
"data": {
"text/plain": "__main__.Admin"
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "featured-engineer",
"cell_type": "code",
"source": "True + True",
"execution_count": 70,
"outputs": [
{
"data": {
"text/plain": "2"
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "colonial-government",
"cell_type": "code",
"source": "False == 0",
"execution_count": 71,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "attached-documentary",
"cell_type": "code",
"source": "True == 1",
"execution_count": 72,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "weighted-charge",
"cell_type": "code",
"source": "print(True)",
"execution_count": 73,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "True\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "naughty-algeria",
"cell_type": "code",
"source": "True == 1",
"execution_count": 74,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "accompanied-health",
"cell_type": "code",
"source": "isinstance(True, int)",
"execution_count": 75,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "vanilla-punch",
"cell_type": "code",
"source": "my_list = [1, 3, 2, 5, 6, 6, 7, 10]",
"execution_count": 76,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "enclosed-vehicle",
"cell_type": "code",
"source": "sum([x % 2 == 0 for x in my_list])",
"execution_count": 78,
"outputs": [
{
"data": {
"text/plain": "4"
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "greater-cliff",
"cell_type": "code",
"source": "[x % 2 == 0 for x in my_list]",
"execution_count": 79,
"outputs": [
{
"data": {
"text/plain": "[False, False, True, False, True, True, False, True]"
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "greek-failure",
"cell_type": "code",
"source": "first_list = [1, 2, 3]\nsecond_list = ['a', 'b', 'c']\nfor first, second in zip(first_list, second_list):\n print(first, second)",
"execution_count": 81,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1 a\n2 b\n3 c\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "valued-session",
"cell_type": "code",
"source": "list(zip(first_list, second_list))",
"execution_count": 82,
"outputs": [
{
"data": {
"text/plain": "[(1, 'a'), (2, 'b'), (3, 'c')]"
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "dirty-pearl",
"cell_type": "code",
"source": "first_list = [1, 2, 3]\nsecond_list = ['a', 'b', 'c']\nzipped_lists = zip(first_list, second_list)\nfor first, second in zipped_lists:\n print(first, second)\nprint(\"Once again\")\nfor first, second in zipped_lists:\n print(first, second)",
"execution_count": 85,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1 a\n2 b\n3 c\nOnce again\n"
}
]
},
{
"metadata": {},
"id": "exposed-ghost",
"cell_type": "markdown",
"source": "## Итераторы"
},
{
"metadata": {
"trusted": false
},
"id": "breathing-religion",
"cell_type": "code",
"source": "class MyIter():\n def __init__(self):\n self.state = 5\n def __next__(self):\n cur_state = self.state\n if cur_state >= 0:\n self.state = self.state - 1\n return cur_state\n else:\n raise StopIteration\n def __iter__(self):\n return self",
"execution_count": 86,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "subtle-myanmar",
"cell_type": "code",
"source": "countdown = MyIter()",
"execution_count": 88,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "middle-yeast",
"cell_type": "code",
"source": "for x in countdown:\n print(x)",
"execution_count": 89,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "5\n4\n3\n2\n1\n0\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "geographic-equilibrium",
"cell_type": "code",
"source": "for x in countdown:\n print(x)",
"execution_count": 90,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "mechanical-blackberry",
"cell_type": "code",
"source": "countdown = MyIter()\nlist(countdown)",
"execution_count": 91,
"outputs": [
{
"data": {
"text/plain": "[5, 4, 3, 2, 1, 0]"
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "native-kazakhstan",
"cell_type": "code",
"source": "class Naturals():\n def __init__(self):\n self.state = 0\n def __next__(self):\n self.state = self.state + 1\n return self.state\n def __iter__(self):\n return self",
"execution_count": 92,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "adjusted-committee",
"cell_type": "code",
"source": "N = Naturals()",
"execution_count": 93,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "original-swaziland",
"cell_type": "code",
"source": "for x in N:\n print(x)\n if x >= 10:\n break",
"execution_count": 94,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "injured-account",
"cell_type": "code",
"source": "my_list = [1, 2, 3, 10]",
"execution_count": 95,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "legitimate-solomon",
"cell_type": "code",
"source": "my_list.__next__()",
"execution_count": 96,
"outputs": [
{
"ename": "AttributeError",
"evalue": "'list' object has no attribute '__next__'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-96-40b369d77577>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__next__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute '__next__'"
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "complete-feature",
"cell_type": "code",
"source": "my_list_iter = iter(my_list)",
"execution_count": 97,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "valued-johnson",
"cell_type": "code",
"source": "my_list_iter.__next__()",
"execution_count": 98,
"outputs": [
{
"data": {
"text/plain": "1"
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "wrapped-celebrity",
"cell_type": "code",
"source": "my_list_iter.__next__()",
"execution_count": 99,
"outputs": [
{
"data": {
"text/plain": "2"
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "periodic-ozone",
"cell_type": "code",
"source": "my_list_iter.__next__()",
"execution_count": 100,
"outputs": [
{
"data": {
"text/plain": "3"
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "finite-surprise",
"cell_type": "code",
"source": "my_list_iter.__next__()",
"execution_count": 101,
"outputs": [
{
"data": {
"text/plain": "10"
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "broke-james",
"cell_type": "code",
"source": "my_list_iter.__next__()",
"execution_count": 102,
"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-102-62e86fe19164>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_list_iter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__next__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "external-stations",
"cell_type": "code",
"source": "my_list_iter = iter(my_list)",
"execution_count": 103,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "greatest-lithuania",
"cell_type": "code",
"source": "next(my_list_iter)",
"execution_count": 105,
"outputs": [
{
"data": {
"text/plain": "1"
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "integrated-brooks",
"cell_type": "code",
"source": "next(my_list_iter)",
"execution_count": 106,
"outputs": [
{
"data": {
"text/plain": "2"
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "material-system",
"cell_type": "code",
"source": "next(my_list_iter)",
"execution_count": 107,
"outputs": [
{
"data": {
"text/plain": "3"
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "dietary-command",
"cell_type": "code",
"source": "next(my_list_iter)",
"execution_count": 108,
"outputs": [
{
"data": {
"text/plain": "10"
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "lyric-joseph",
"cell_type": "code",
"source": "next(my_list_iter)",
"execution_count": 109,
"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-109-80fba3049312>\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[0mmy_list_iter\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "fundamental-source",
"cell_type": "code",
"source": "next(my_list_iter, -99)",
"execution_count": 110,
"outputs": [
{
"data": {
"text/plain": "-99"
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "gorgeous-variance",
"cell_type": "code",
"source": "for x in my_list:\n print(x)",
"execution_count": 111,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1\n2\n3\n10\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "liberal-asthma",
"cell_type": "code",
"source": "my_list_iter = iter(my_list)\nx = next(my_list_iter)\nprint(x)\n\nx = next(my_list_iter)\nprint(x)\n\n# ...\n# do until StopIteration",
"execution_count": 114,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1\n2\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "prerequisite-darwin",
"cell_type": "code",
"source": "my_list = [1, 2, 10, 4, 2, 10, 13, 15, 20, 15]",
"execution_count": 115,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "tracked-collar",
"cell_type": "code",
"source": "sum([x for x in my_list if x % 2 == 0])",
"execution_count": 116,
"outputs": [
{
"data": {
"text/plain": "48"
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "meaningful-magnet",
"cell_type": "code",
"source": "sum((x for x in my_list if x % 2 == 0))",
"execution_count": 117,
"outputs": [
{
"data": {
"text/plain": "48"
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "numerous-elite",
"cell_type": "code",
"source": "even_numbers = (x for x in my_list if x % 2 == 0)",
"execution_count": 118,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "powered-whole",
"cell_type": "code",
"source": "next(even_numbers)",
"execution_count": 119,
"outputs": [
{
"data": {
"text/plain": "2"
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "lonely-wesley",
"cell_type": "code",
"source": "next(even_numbers)",
"execution_count": 120,
"outputs": [
{
"data": {
"text/plain": "10"
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "crucial-cream",
"cell_type": "code",
"source": "next(even_numbers)",
"execution_count": 121,
"outputs": [
{
"data": {
"text/plain": "4"
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "operating-oriental",
"cell_type": "code",
"source": "next(even_numbers)",
"execution_count": 122,
"outputs": [
{
"data": {
"text/plain": "2"
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "catholic-creature",
"cell_type": "code",
"source": "next(even_numbers)",
"execution_count": 123,
"outputs": [
{
"data": {
"text/plain": "10"
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "lesbian-blade",
"cell_type": "code",
"source": "next(even_numbers)",
"execution_count": 124,
"outputs": [
{
"data": {
"text/plain": "20"
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "speaking-avatar",
"cell_type": "code",
"source": "next(even_numbers)",
"execution_count": 125,
"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-125-5fe2a8618cb6>\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[0meven_numbers\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "mounted-encyclopedia",
"cell_type": "code",
"source": "N",
"execution_count": 126,
"outputs": [
{
"data": {
"text/plain": "<__main__.Naturals at 0x7f89c80e6438>"
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "committed-improvement",
"cell_type": "code",
"source": "evens = (n for n in N if n % 2 == 0)",
"execution_count": 128,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "thirty-raleigh",
"cell_type": "code",
"source": "evens",
"execution_count": 129,
"outputs": [
{
"data": {
"text/plain": "<generator object <genexpr> at 0x7f89c7cee308>"
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "presidential-danger",
"cell_type": "code",
"source": "for n in evens:\n print(n)\n if n >= 100:\n break",
"execution_count": 131,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "12\n14\n16\n18\n20\n22\n24\n26\n28\n30\n32\n34\n36\n38\n40\n42\n44\n46\n48\n50\n52\n54\n56\n58\n60\n62\n64\n66\n68\n70\n72\n74\n76\n78\n80\n82\n84\n86\n88\n90\n92\n94\n96\n98\n100\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "charming-juvenile",
"cell_type": "code",
"source": "for n in evens:\n print(n)\n if n >= 200:\n break",
"execution_count": 132,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "102\n104\n106\n108\n110\n112\n114\n116\n118\n120\n122\n124\n126\n128\n130\n132\n134\n136\n138\n140\n142\n144\n146\n148\n150\n152\n154\n156\n158\n160\n162\n164\n166\n168\n170\n172\n174\n176\n178\n180\n182\n184\n186\n188\n190\n192\n194\n196\n198\n200\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "subtle-holly",
"cell_type": "code",
"source": "import itertools",
"execution_count": 133,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "equipped-suicide",
"cell_type": "code",
"source": "numbers = [1, 2, 3]\nletters = ['a', 'b', 'c', 'd']",
"execution_count": 135,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "geological-sequence",
"cell_type": "code",
"source": "for number, letter in itertools.product(numbers, letters):\n print(number, letter)",
"execution_count": 137,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1 a\n1 b\n1 c\n1 d\n2 a\n2 b\n2 c\n2 d\n3 a\n3 b\n3 c\n3 d\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "direct-ensemble",
"cell_type": "code",
"source": "list(itertools.combinations([1, 2, 3, 4, 5], 3))",
"execution_count": 138,
"outputs": [
{
"data": {
"text/plain": "[(1, 2, 3),\n (1, 2, 4),\n (1, 2, 5),\n (1, 3, 4),\n (1, 3, 5),\n (1, 4, 5),\n (2, 3, 4),\n (2, 3, 5),\n (2, 4, 5),\n (3, 4, 5)]"
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "rolled-lexington",
"cell_type": "code",
"source": "list(itertools.permutations([1, 2, 3, 4]))",
"execution_count": 139,
"outputs": [
{
"data": {
"text/plain": "[(1, 2, 3, 4),\n (1, 2, 4, 3),\n (1, 3, 2, 4),\n (1, 3, 4, 2),\n (1, 4, 2, 3),\n (1, 4, 3, 2),\n (2, 1, 3, 4),\n (2, 1, 4, 3),\n (2, 3, 1, 4),\n (2, 3, 4, 1),\n (2, 4, 1, 3),\n (2, 4, 3, 1),\n (3, 1, 2, 4),\n (3, 1, 4, 2),\n (3, 2, 1, 4),\n (3, 2, 4, 1),\n (3, 4, 1, 2),\n (3, 4, 2, 1),\n (4, 1, 2, 3),\n (4, 1, 3, 2),\n (4, 2, 1, 3),\n (4, 2, 3, 1),\n (4, 3, 1, 2),\n (4, 3, 2, 1)]"
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "illegal-temple",
"cell_type": "code",
"source": "from random import randrange",
"execution_count": 142,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "advisory-airport",
"cell_type": "code",
"source": "for x in itertools.cycle([1, 2, 3]):\n print(x)\n if randrange(10) == 0:\n break",
"execution_count": 143,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1\n2\n3\n1\n2\n3\n1\n2\n3\n1\n2\n3\n1\n2\n3\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "historical-wednesday",
"cell_type": "code",
"source": "def Naturals_gen():\n print(\"Started generation\")\n x = 0\n while True:\n print(\"Inside the infinite loop\")\n x = x + 1\n yield x",
"execution_count": 147,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "associate-lawsuit",
"cell_type": "code",
"source": "N = Naturals_gen()",
"execution_count": 148,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "vulnerable-moisture",
"cell_type": "code",
"source": "next(N)",
"execution_count": 149,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Started generation\nInside the infinite loop\n"
},
{
"data": {
"text/plain": "1"
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "automated-burden",
"cell_type": "code",
"source": "next(N)",
"execution_count": 150,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Inside the infinite loop\n"
},
{
"data": {
"text/plain": "2"
},
"execution_count": 150,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "south-popularity",
"cell_type": "code",
"source": "type(N)",
"execution_count": 151,
"outputs": [
{
"data": {
"text/plain": "generator"
},
"execution_count": 151,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "controlling-package",
"cell_type": "code",
"source": "def one_two_three():\n yield 1\n yield 2\n yield 3",
"execution_count": 152,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "desperate-brunei",
"cell_type": "code",
"source": "x = one_two_three()",
"execution_count": 153,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "colonial-melbourne",
"cell_type": "code",
"source": "list(x)",
"execution_count": 154,
"outputs": [
{
"data": {
"text/plain": "[1, 2, 3]"
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "forbidden-replica",
"cell_type": "code",
"source": "x = one_two_three()",
"execution_count": 155,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "relevant-slope",
"cell_type": "code",
"source": "next(x)",
"execution_count": 156,
"outputs": [
{
"data": {
"text/plain": "1"
},
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "smoking-stations",
"cell_type": "code",
"source": "next(x)",
"execution_count": 157,
"outputs": [
{
"data": {
"text/plain": "2"
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "fundamental-welcome",
"cell_type": "code",
"source": "next(x)",
"execution_count": 158,
"outputs": [
{
"data": {
"text/plain": "3"
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "pending-appreciation",
"cell_type": "code",
"source": "next(x)",
"execution_count": 159,
"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-159-92de4e9f6b1e>\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[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "municipal-pierre",
"cell_type": "code",
"source": "my_list = [10, 30, 25, 35, 12, 13, 15, 20, 21]",
"execution_count": 160,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "corporate-authority",
"cell_type": "code",
"source": "for x in my_list:\n if x % 2 == 1:\n first_odd = x\n break\nfirst_odd",
"execution_count": 161,
"outputs": [
{
"data": {
"text/plain": "25"
},
"execution_count": 161,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "acting-calcium",
"cell_type": "code",
"source": "my_list = [2, 4, 8]\nfirst_odd = next((x for x in my_list if x % 2 == 1), None)",
"execution_count": 167,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "surprised-puppy",
"cell_type": "code",
"source": "print(first_odd)",
"execution_count": 169,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "None\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "posted-delay",
"cell_type": "code",
"source": "x = one_two_three()\nlist(reversed(list(x)))",
"execution_count": 174,
"outputs": [
{
"data": {
"text/plain": "[3, 2, 1]"
},
"execution_count": 174,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "sixth-surprise",
"cell_type": "code",
"source": "N = Naturals()\nfor x in itertools.islice(N, 3, 15):\n print(x)",
"execution_count": 176,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "minor-setting",
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.10",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "lesson09.ipynb",
"public": false
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment