Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created Feb 9, 2022
Embed
What would you like to do?
Lesson09.ipynb
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "## Наука о данных\n### Совместный бакалавриат ВШЭ-РЭШ, 2021-2022 учебный год\n_Илья Щуров_\n\n[Страница курса](http://math-info.hse.ru/s21/j)"
},
{
"metadata": {
"trusted": false
},
"id": "robust-wichita",
"cell_type": "code",
"source": "x = int(input(\"Please, enter some number: \"))\nif x == 0:\n print(\"Can't use 0\")\nelse:\n print(1 / x)",
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Please, enter some number: jasdlfjaslf\n"
},
{
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: 'jasdlfjaslf'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/h2/9nyrt4p55kq6pdvqg02_zmj40000gn/T/ipykernel_42623/28762699.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Please, enter some number: \"\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[1;32m 2\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Can't use 0\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\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[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'jasdlfjaslf'"
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "postal-skiing",
"cell_type": "code",
"source": "set(\"hello\")",
"execution_count": 7,
"outputs": [
{
"data": {
"text/plain": "{'e', 'h', 'l', 'o'}"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "durable-chapel",
"cell_type": "code",
"source": "s = input(\"Please, enter some number: \")\nif not set(s).issubset(set(\"-0123456789\")):\n print(\"Should be number\")\nelse:\n x = int(s)\n if x == 0:\n print(\"Can't use 0\")\n else:\n print(1 / x)",
"execution_count": 13,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Please, enter some number: 1-11-1\n"
},
{
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: '1-11-1'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/h2/9nyrt4p55kq6pdvqg02_zmj40000gn/T/ipykernel_42623/4055085847.py\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[0;34m\"Should be number\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Can't use 0\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '1-11-1'"
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "strange-philip",
"cell_type": "code",
"source": "try:\n x = int(input(\"Please, enter some number: \"))\n print(1 / x)\n print(\"Success\")\nexcept ZeroDivisionError:\n print(\"Can't use 0\")\nexcept ValueError:\n print(\"It seems to be not a number\")\nprint(\"That's all\")",
"execution_count": 30,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Please, enter some number: 1.\nIt seems to be not a number\nThat's all\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "suburban-control",
"cell_type": "code",
"source": "try:\n x = int(input(\"Please, enter some number: \"))\n print(1 / x)\n print(\"Success\")\nexcept Exception as e:\n print(\"Unkown error occurred\")\n print(type(e))\n print(e)\nprint(\"Let's continue\")",
"execution_count": 40,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Please, enter some number: 0\nUnkown error occurred\n<class 'ZeroDivisionError'>\ndivision by zero\nLet's continue\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "representative-scholar",
"cell_type": "code",
"source": "type(E)",
"execution_count": 39,
"outputs": [
{
"data": {
"text/plain": "ZeroDivisionError"
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "pressing-dating",
"cell_type": "code",
"source": "# Don't\ntry:\n x = int(input(\"Please, enter some number: \"))\n print(1 / x)\n print(\"Success\")\nexcept:\n pass\nprint(\"Let's continue\")",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "unavailable-broad",
"cell_type": "code",
"source": "class User:\n def __init__(self, login, first_name, last_name):\n self.login = \"@\" + login\n self.first_name = first_name\n self.last_name = last_name\n \n def say_hello(self):\n print(f\"Hello, I'm {self.first_name} {self.last_name}\")\n",
"execution_count": 62,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "biblical-transport",
"cell_type": "code",
"source": "alice = User(\"alice\", \"Alice\", \"Smith\")\nbob = User(\"bobby\", \"Bob\", \"Johnson\")",
"execution_count": 64,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "industrial-jumping",
"cell_type": "code",
"source": "alice.first_name",
"execution_count": 65,
"outputs": [
{
"data": {
"text/plain": "'Alice'"
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "negative-phrase",
"cell_type": "code",
"source": "alice.last_name",
"execution_count": 66,
"outputs": [
{
"data": {
"text/plain": "'Smith'"
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "exterior-copying",
"cell_type": "code",
"source": "alice.login",
"execution_count": 67,
"outputs": [
{
"data": {
"text/plain": "'@alice'"
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "smaller-prairie",
"cell_type": "code",
"source": "alice.say_hello()",
"execution_count": 68,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello, I'm Alice Smith\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "banned-envelope",
"cell_type": "code",
"source": "type(alice.say_hello)",
"execution_count": 69,
"outputs": [
{
"data": {
"text/plain": "method"
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "excessive-allocation",
"cell_type": "code",
"source": "type(alice)",
"execution_count": 70,
"outputs": [
{
"data": {
"text/plain": "__main__.User"
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "military-summit",
"cell_type": "code",
"source": "alice.last_name = \"Johnson\"",
"execution_count": 71,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "happy-victorian",
"cell_type": "code",
"source": "alice.say_hello()",
"execution_count": 72,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello, I'm Alice Johnson\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "super-major",
"cell_type": "code",
"source": "alice",
"execution_count": 74,
"outputs": [
{
"data": {
"text/plain": "<__main__.User at 0x113ab5810>"
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "prime-checklist",
"cell_type": "code",
"source": "print(alice)",
"execution_count": 75,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "<__main__.User object at 0x113ab5810>\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "heavy-cornell",
"cell_type": "code",
"source": "class User:\n def __init__(self, login, first_name, last_name):\n self.login = \"@\" + login\n self.first_name = first_name\n self.last_name = last_name\n \n def say_hello(self):\n print(f\"Hello, I'm {self.first_name} {self.last_name}\")\n \n def __str__(self):\n return f\"User: ({self.login} <{self.first_name} {self.last_name}>)\"\n \n def __repr__(self):\n return f\"User({self.login[1:]!r}, {self.first_name!r}, {self.last_name!r})\"",
"execution_count": 208,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "loving-vietnam",
"cell_type": "code",
"source": "alice = User(\"alice\", \"Alice\", \"Smith\")\nbob = User(\"bobby\", \"Bob\", \"Johnson\")",
"execution_count": 209,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "threatened-cocktail",
"cell_type": "code",
"source": "print(alice)",
"execution_count": 138,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "User: (@alice <Alice Smith>)\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "positive-custody",
"cell_type": "code",
"source": "str(alice)",
"execution_count": 139,
"outputs": [
{
"data": {
"text/plain": "'User: (@alice <Alice Smith>)'"
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "differential-ready",
"cell_type": "code",
"source": "alice",
"execution_count": 140,
"outputs": [
{
"data": {
"text/plain": "User('alice', 'Alice', 'Smith')"
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "binary-visiting",
"cell_type": "code",
"source": "alice.__repr__()",
"execution_count": 210,
"outputs": [
{
"data": {
"text/plain": "\"User('alice', 'Alice', 'Smith')\""
},
"execution_count": 210,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "smart-coordinate",
"cell_type": "code",
"source": "repr(alice)",
"execution_count": 211,
"outputs": [
{
"data": {
"text/plain": "\"User('alice', 'Alice', 'Smith')\""
},
"execution_count": 211,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "documentary-potato",
"cell_type": "code",
"source": "s = \"123\"",
"execution_count": 141,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "attempted-unknown",
"cell_type": "code",
"source": "print(s)",
"execution_count": 142,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "123\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "together-april",
"cell_type": "code",
"source": "print(repr(s))",
"execution_count": 143,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "'123'\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "annual-reviewer",
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "figured-blind",
"cell_type": "code",
"source": "repr(s)",
"execution_count": 144,
"outputs": [
{
"data": {
"text/plain": "\"'123'\""
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "answering-mailman",
"cell_type": "code",
"source": "repr(repr(repr(repr(\"1\"))))",
"execution_count": 145,
"outputs": [
{
"data": {
"text/plain": "'\\'\\\\\\'\"\\\\\\\\\\\\\\'1\\\\\\\\\\\\\\'\"\\\\\\'\\''"
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "operational-swiss",
"cell_type": "code",
"source": "repr(\"1\")",
"execution_count": 146,
"outputs": [
{
"data": {
"text/plain": "\"'1'\""
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "amateur-authority",
"cell_type": "code",
"source": "repr(1)",
"execution_count": 147,
"outputs": [
{
"data": {
"text/plain": "'1'"
},
"execution_count": 147,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "simple-toronto",
"cell_type": "code",
"source": "repr(repr(\"1\"))",
"execution_count": 148,
"outputs": [
{
"data": {
"text/plain": "'\"\\'1\\'\"'"
},
"execution_count": 148,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "buried-arena",
"cell_type": "code",
"source": "print(\"\\\"\")",
"execution_count": 149,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "\"\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "noble-forwarding",
"cell_type": "code",
"source": "alice",
"execution_count": 150,
"outputs": [
{
"data": {
"text/plain": "User('alice', 'Alice', 'Smith')"
},
"execution_count": 150,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "excess-steel",
"cell_type": "code",
"source": "print(repr(alice))",
"execution_count": 123,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "User('alice', 'Alice', 'Smith')\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "patent-donor",
"cell_type": "code",
"source": "User('@alice', 'Alice', 'Smith')",
"execution_count": 124,
"outputs": [
{
"data": {
"text/plain": "User('@alice', 'Alice', 'Smith')"
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "greatest-presence",
"cell_type": "code",
"source": "my_list = [\"Hello\", \"World\"]",
"execution_count": 125,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "spare-composer",
"cell_type": "code",
"source": "my_list",
"execution_count": 126,
"outputs": [
{
"data": {
"text/plain": "['Hello', 'World']"
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "binding-arcade",
"cell_type": "code",
"source": "\", \".join(my_list)",
"execution_count": 127,
"outputs": [
{
"data": {
"text/plain": "'Hello, World'"
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "engaging-importance",
"cell_type": "code",
"source": "for word in my_list:\n print(word)",
"execution_count": 129,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello\nWorld\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "seventh-intensity",
"cell_type": "code",
"source": "class User:\n def __init__(self, login, first_name, last_name):\n self.login = \"@\" + login\n self.first_name = first_name\n self.last_name = last_name\n \n def say_hello(self):\n print(f\"Hello, I'm {self.first_name} {self.last_name}\")\n \n def __str__(self):\n return f\"User: ({self.login} <{self.first_name} {self.last_name}>)\"\n \n def __repr__(self):\n return f\"User({self.login[1:]!r}, {self.first_name!r}, {self.last_name!r})\"\n \n ",
"execution_count": 151,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "appropriate-genome",
"cell_type": "code",
"source": "alice = User('alice', 'Alice', 'Smith')\nalice_backup = alice",
"execution_count": 155,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "reverse-storm",
"cell_type": "code",
"source": "del alice",
"execution_count": 156,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "grand-screw",
"cell_type": "code",
"source": "alice_backup",
"execution_count": 157,
"outputs": [
{
"data": {
"text/plain": "User('alice', 'Alice', 'Smith')"
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "continental-petroleum",
"cell_type": "code",
"source": "alice",
"execution_count": 158,
"outputs": [
{
"ename": "NameError",
"evalue": "name 'alice' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/h2/9nyrt4p55kq6pdvqg02_zmj40000gn/T/ipykernel_42623/1603943322.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0malice\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'alice' is not defined"
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "million-article",
"cell_type": "code",
"source": "all_users = []\nclass User:\n def __init__(self, login, first_name, last_name):\n self.login = \"@\" + login\n self.first_name = first_name\n self.last_name = last_name\n self.banned = False\n self.friends = []\n all_users.append(self)\n \n def say_hello(self):\n if self.banned:\n print(\"User is banned and cannot say 'Hello'\")\n else:\n print(f\"Hello, I'm {self.first_name} {self.last_name}\")\n \n def __str__(self):\n return f\"User: ({self.login} <{self.first_name} {self.last_name}>)\"\n \n def __repr__(self):\n return f\"User({self.login[1:]!r}, {self.first_name!r}, {self.last_name!r})\"\n \n def ban(self):\n self.banned = True\n \n def add_friend(self, other):\n if not isinstance(other, User):\n raise ValueError(\"other is not of class User\")\n self.friends.append(other)\n \n def say_hello_to_friends(self):\n for friend in self.friends:\n print(f\"Hello, {friend.first_name}!\")",
"execution_count": 180,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "wanted-intersection",
"cell_type": "code",
"source": "alice = User('alice', 'Alice', 'Smith')\nbob = User('bob', 'Bob', 'Johnson')\nclaudia = User('cla', 'Claudia', 'Smith')\nalice.add_friend(bob)\nalice.add_friend(claudia)",
"execution_count": 181,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "arabic-cincinnati",
"cell_type": "code",
"source": "all_users",
"execution_count": 182,
"outputs": [
{
"data": {
"text/plain": "[User('alice', 'Alice', 'Smith'),\n User('bob', 'Bob', 'Johnson'),\n User('cla', 'Claudia', 'Smith')]"
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "operational-welcome",
"cell_type": "code",
"source": "alice.say_hello()",
"execution_count": 173,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello, I'm Alice Smith\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "viral-filter",
"cell_type": "code",
"source": "alice.ban()",
"execution_count": 174,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "spiritual-headquarters",
"cell_type": "code",
"source": "alice.say_hello()",
"execution_count": 175,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "User is banned and cannot say 'Hello'\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "european-mechanics",
"cell_type": "code",
"source": "alice.say_hello_to_friends()",
"execution_count": 176,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello, Bob!\nHello, Claudia!\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "muslim-remains",
"cell_type": "code",
"source": "alice.add_friend(\"Bob Smith\")",
"execution_count": 177,
"outputs": [
{
"ename": "ValueError",
"evalue": "other is not of class User",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/h2/9nyrt4p55kq6pdvqg02_zmj40000gn/T/ipykernel_42623/2808046096.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0malice\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_friend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Bob Smith\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/var/folders/h2/9nyrt4p55kq6pdvqg02_zmj40000gn/T/ipykernel_42623/3675124455.py\u001b[0m in \u001b[0;36madd_friend\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0madd_friend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\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[1;32m 25\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mUser\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[0;32m---> 26\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"other is not of class User\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 27\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfriends\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 28\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: other is not of class User"
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "official-defendant",
"cell_type": "code",
"source": "alice.friends.append(\"Bob Smith\")",
"execution_count": 178,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "meaning-multiple",
"cell_type": "code",
"source": "alice.friends",
"execution_count": 179,
"outputs": [
{
"data": {
"text/plain": "[User('bob', 'Bob', 'Johnson'), User('cla', 'Claudia', 'Smith'), 'Bob Smith']"
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "individual-destiny",
"cell_type": "code",
"source": "all_users = []\nclass User:\n def __init__(self, login, first_name, last_name):\n self.login = \"@\" + login\n self.first_name = first_name\n self.last_name = last_name\n self.banned = False\n self._friends = []\n all_users.append(self)\n \n def say_hello(self):\n if self.banned:\n print(\"User is banned and cannot say 'Hello'\")\n else:\n print(f\"Hello, I'm {self.first_name} {self.last_name}\")\n \n def __str__(self):\n return f\"User: ({self.login} <{self.first_name} {self.last_name}>)\"\n \n def __repr__(self):\n return f\"User({self.login[1:]!r}, {self.first_name!r}, {self.last_name!r})\"\n \n def ban(self):\n self.banned = True\n \n def add_friend(self, other):\n if not isinstance(other, User):\n raise ValueError(\"other is not of class User\")\n self._friends.append(other)\n \n def say_hello_to_friends(self):\n for friend in self._friends:\n print(f\"Hello, {friend.first_name}!\")",
"execution_count": 183,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "awful-harrison",
"cell_type": "code",
"source": "class MyInt:\n def __init__(self, value):\n self._value = value\n \n def __repr__(self):\n return str(self._value)\n \n def __add__(self, other):\n if isinstance(other, int):\n other_value = other\n elif isinstance(other, MyInt):\n other_value = other._value\n else:\n raise ValueError(\"Unkown type\")\n return MyInt(self._value * other_value)",
"execution_count": 186,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "electrical-degree",
"cell_type": "code",
"source": "x = MyInt(20)\ny = MyInt(10)",
"execution_count": 196,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "statewide-market",
"cell_type": "code",
"source": "x + 4",
"execution_count": 188,
"outputs": [
{
"data": {
"text/plain": "80"
},
"execution_count": 188,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "fiscal-southeast",
"cell_type": "code",
"source": "x.__add__(4)",
"execution_count": 195,
"outputs": [
{
"data": {
"text/plain": "80"
},
"execution_count": 195,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "deadly-amount",
"cell_type": "code",
"source": "print(x)",
"execution_count": 189,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "20\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "objective-yahoo",
"cell_type": "code",
"source": "x + 3",
"execution_count": 190,
"outputs": [
{
"data": {
"text/plain": "60"
},
"execution_count": 190,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "tough-might",
"cell_type": "code",
"source": "x + 3 + 5",
"execution_count": 191,
"outputs": [
{
"data": {
"text/plain": "300"
},
"execution_count": 191,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "resident-bidder",
"cell_type": "code",
"source": "x + 1 + 2",
"execution_count": 192,
"outputs": [
{
"data": {
"text/plain": "40"
},
"execution_count": 192,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "considerable-jacksonville",
"cell_type": "code",
"source": "x",
"execution_count": 193,
"outputs": [
{
"data": {
"text/plain": "20"
},
"execution_count": 193,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "sapphire-imaging",
"cell_type": "code",
"source": "x + 3 + 4",
"execution_count": 194,
"outputs": [
{
"data": {
"text/plain": "240"
},
"execution_count": 194,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "raising-worry",
"cell_type": "code",
"source": "x + y",
"execution_count": 197,
"outputs": [
{
"data": {
"text/plain": "200"
},
"execution_count": 197,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "congressional-theater",
"cell_type": "code",
"source": "z = 10",
"execution_count": 198,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "labeled-racing",
"cell_type": "code",
"source": "z.__add__(123)",
"execution_count": 199,
"outputs": [
{
"data": {
"text/plain": "133"
},
"execution_count": 199,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "sorted-detroit",
"cell_type": "code",
"source": "def repr(x):\n return x.__repr__()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "smoking-identification",
"cell_type": "code",
"source": "alice.__repr__()",
"execution_count": 200,
"outputs": [
{
"data": {
"text/plain": "\"User('alice', 'Alice', 'Smith')\""
},
"execution_count": 200,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "sustainable-length",
"cell_type": "code",
"source": "repr(alice)",
"execution_count": 201,
"outputs": [
{
"data": {
"text/plain": "\"User('alice', 'Alice', 'Smith')\""
},
"execution_count": 201,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "incorporate-warrant",
"cell_type": "code",
"source": "add(z)",
"execution_count": 202,
"outputs": [
{
"ename": "NameError",
"evalue": "name 'add' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/h2/9nyrt4p55kq6pdvqg02_zmj40000gn/T/ipykernel_42623/2747237090.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'add' is not defined"
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "innocent-target",
"cell_type": "code",
"source": "(10).__add__(30)",
"execution_count": 203,
"outputs": [
{
"data": {
"text/plain": "40"
},
"execution_count": 203,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "operating-enforcement",
"cell_type": "code",
"source": "from operator import add",
"execution_count": 204,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "ethical-prison",
"cell_type": "code",
"source": "add(10, 40)",
"execution_count": 205,
"outputs": [
{
"data": {
"text/plain": "50"
},
"execution_count": 205,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "exposed-insert",
"cell_type": "code",
"source": "def subs_2_and_5(f):\n return f(2, 5)",
"execution_count": 206,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "coordinated-trash",
"cell_type": "code",
"source": "subs_2_and_5(add)",
"execution_count": 207,
"outputs": [
{
"data": {
"text/plain": "7"
},
"execution_count": 207,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "smart-piece",
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "py3.10",
"display_name": "Python 3.10",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.10.0",
"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