Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created January 31, 2020 09:13
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/861c593afb172ac4926bec5f758d544c to your computer and use it in GitHub Desktop.
Save ischurov/861c593afb172ac4926bec5f758d544c to your computer and use it in GitHub Desktop.
lecture 5.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = 1\nn = 9\nfor i in range(1, n + 1):\n f = f * i\nf",
"execution_count": 1,
"outputs": [
{
"data": {
"text/plain": "362880"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def factorial(n):\n f = 1\n for i in range(1, n + 1):\n f = f * i\n return f",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = factorial(9) + 1",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "factorial(3)",
"execution_count": 6,
"outputs": [
{
"data": {
"text/plain": "6"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f",
"execution_count": 7,
"outputs": [
{
"data": {
"text/plain": "362880"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 12\ndef foo():\n y = x + 1\n # x - global variable\n return y\nz = foo()",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "z",
"execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "13"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo()",
"execution_count": 11,
"outputs": [
{
"data": {
"text/plain": "13"
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 12\ndef bar():\n x = x + 1\n return x\nz = bar()\nprint(z)",
"execution_count": 13,
"outputs": [
{
"ename": "UnboundLocalError",
"evalue": "local variable 'x' referenced before assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-13-3ecca3c7bdb7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\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[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbar\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 6\u001b[0m \u001b[0mprint\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[0;32m<ipython-input-13-3ecca3c7bdb7>\u001b[0m in \u001b[0;36mbar\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m12\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbar\u001b[0m\u001b[0;34m(\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----> 3\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\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[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbar\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;31mUnboundLocalError\u001b[0m: local variable 'x' referenced before assignment"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 12\ndef bar():\n x = 0\n x = x + 1\n return x\nz = bar()\nprint(z)\nprint(x)",
"execution_count": 15,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1\n12\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 12\ndef bar():\n print(x)\n return x\nz = bar()\nprint(z)",
"execution_count": 17,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "12\n12\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 12\ndef bar():\n print(x)\n x = 0\n return x\nz = bar()\nprint(z)",
"execution_count": 18,
"outputs": [
{
"ename": "UnboundLocalError",
"evalue": "local variable 'x' referenced before assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-18-8511200cc72f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\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[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbar\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 7\u001b[0m \u001b[0mprint\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[0;32m<ipython-input-18-8511200cc72f>\u001b[0m in \u001b[0;36mbar\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m12\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbar\u001b[0m\u001b[0;34m(\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----> 3\u001b[0;31m \u001b[0mprint\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[1;32m 4\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[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mUnboundLocalError\u001b[0m: local variable 'x' referenced before assignment"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def print_answer(n):\n print(n)\n \ndef return_answer(n):\n return n",
"execution_count": 20,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print_answer(42)",
"execution_count": 21,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "42\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "return_answer(42)",
"execution_count": 22,
"outputs": [
{
"data": {
"text/plain": "42"
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = print_answer(42)",
"execution_count": 24,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "42\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(x)",
"execution_count": 25,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "None\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "type(None)",
"execution_count": 26,
"outputs": [
{
"data": {
"text/plain": "NoneType"
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 28,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "None",
"execution_count": 29,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(None)",
"execution_count": 30,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "None\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = return_answer(42)",
"execution_count": 31,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 32,
"outputs": [
{
"data": {
"text/plain": "42"
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def foo(x, y):\n return x + 2 * y",
"execution_count": 35,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo(2, 3)",
"execution_count": 36,
"outputs": [
{
"data": {
"text/plain": "8"
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo(x=2, y=3)",
"execution_count": 37,
"outputs": [
{
"data": {
"text/plain": "8"
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo(y=3, x=2)",
"execution_count": 38,
"outputs": [
{
"data": {
"text/plain": "8"
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def bar(x, y=100):\n return x + 2 * y",
"execution_count": 39,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "bar(10)",
"execution_count": 40,
"outputs": [
{
"data": {
"text/plain": "210"
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "bar(10, 2)",
"execution_count": 41,
"outputs": [
{
"data": {
"text/plain": "14"
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def bar(x=0, y=100):\n return x + 2 * y",
"execution_count": 43,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "bar()",
"execution_count": 44,
"outputs": [
{
"data": {
"text/plain": "200"
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "bar(4)",
"execution_count": 45,
"outputs": [
{
"data": {
"text/plain": "204"
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "bar(y=15)",
"execution_count": 46,
"outputs": [
{
"data": {
"text/plain": "30"
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(3, 2, 15, 100)",
"execution_count": 47,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "3 2 15 100\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"Hello\", 12)",
"execution_count": 48,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello 12\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def foo(x, *args):\n print(\"x =\", x)\n print(\"args =\", args)",
"execution_count": 49,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo(100)",
"execution_count": 50,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "x = 100\nargs = ()\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo(100, 32)",
"execution_count": 51,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "x = 100\nargs = (32,)\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo(100, 32, \"Hello\")",
"execution_count": 52,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "x = 100\nargs = (32, 'Hello')\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_tuple = (12, 43, 45)",
"execution_count": 53,
"outputs": []
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "my_tuple[0] = 100",
"execution_count": 54,
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-54-a4676a524503>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_tuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = list(my_tuple)",
"execution_count": 55,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list",
"execution_count": 56,
"outputs": [
{
"data": {
"text/plain": "[12, 43, 45]"
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list[0] = 100",
"execution_count": 57,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list",
"execution_count": 58,
"outputs": [
{
"data": {
"text/plain": "[100, 43, 45]"
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_tuple",
"execution_count": 59,
"outputs": [
{
"data": {
"text/plain": "(12, 43, 45)"
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sum([1, 2, 15])",
"execution_count": 60,
"outputs": [
{
"data": {
"text/plain": "18"
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def my_sum(*args):\n return sum(args)",
"execution_count": 61,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_sum(1, 2, 15)",
"execution_count": 62,
"outputs": [
{
"data": {
"text/plain": "18"
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sum((1, 15, 543))",
"execution_count": 63,
"outputs": [
{
"data": {
"text/plain": "559"
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def sum(x):\n print(\"Everything broken!\")",
"execution_count": 64,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sum([1, 2, 3])",
"execution_count": 65,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Everything broken!\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "del sum",
"execution_count": 66,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sum([1, 2, 3])",
"execution_count": 67,
"outputs": [
{
"data": {
"text/plain": "6"
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from math import sqrt",
"execution_count": 70,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "del sqrt",
"execution_count": 71,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import math",
"execution_count": 73,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "math.sqrt(4)",
"execution_count": 74,
"outputs": [
{
"data": {
"text/plain": "2.0"
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "del math",
"execution_count": 75,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "math.sqrt(4)",
"execution_count": 76,
"outputs": [
{
"ename": "NameError",
"evalue": "name 'math' 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<ipython-input-76-65fb388543e8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\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 'math' is not defined"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from math import sqrt\ndef distance_to_0(x, y, z):\n return sqrt(x ** 2 + y ** 2 + z ** 2)",
"execution_count": 81,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "distance_to_0(1, 2, 2)",
"execution_count": 83,
"outputs": [
{
"data": {
"text/plain": "3.0"
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "v = [1, 5, 7]",
"execution_count": 84,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "distance_to_0(*v)",
"execution_count": 86,
"outputs": [
{
"data": {
"text/plain": "8.660254037844387"
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "distance_to_0(*[1, 5, 7])",
"execution_count": 87,
"outputs": [
{
"data": {
"text/plain": "8.660254037844387"
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "distance_to_0(1, 5, 7)",
"execution_count": 88,
"outputs": [
{
"data": {
"text/plain": "8.660254037844387"
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = [1, 2, 3, 4, 5, 6]",
"execution_count": 89,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "first, *middle, last = my_list",
"execution_count": 90,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "first",
"execution_count": 91,
"outputs": [
{
"data": {
"text/plain": "1"
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "first, middle, last = my_list",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "middle",
"execution_count": 92,
"outputs": [
{
"data": {
"text/plain": "[2, 3, 4, 5]"
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "last",
"execution_count": 93,
"outputs": [
{
"data": {
"text/plain": "6"
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "first, second, *middle, last = my_list",
"execution_count": 99,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "second",
"execution_count": 100,
"outputs": [
{
"data": {
"text/plain": "2"
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "middle",
"execution_count": 101,
"outputs": [
{
"data": {
"text/plain": "[3, 4, 5]"
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "last",
"execution_count": 97,
"outputs": [
{
"data": {
"text/plain": "6"
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def sum_diff(x, y):\n return x + y, x - y",
"execution_count": 102,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s, d = sum_diff(3, 4)",
"execution_count": 104,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s",
"execution_count": 105,
"outputs": [
{
"data": {
"text/plain": "7"
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "d",
"execution_count": 106,
"outputs": [
{
"data": {
"text/plain": "-1"
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def foo(x):\n x.append(10)\n return x",
"execution_count": 107,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = [100, 200]\nz = foo(a)",
"execution_count": 108,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "z",
"execution_count": 109,
"outputs": [
{
"data": {
"text/plain": "[100, 200, 10]"
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a",
"execution_count": 110,
"outputs": [
{
"data": {
"text/plain": "[100, 200, 10]"
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "aaa = [10]\ndef foo():\n aaa[0] = aaa[0] + 1",
"execution_count": 111,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo()",
"execution_count": 112,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "aaa",
"execution_count": 113,
"outputs": [
{
"data": {
"text/plain": "[11]"
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = 10\nprint(id(a))\ndef bar():\n a = 11\n print(id(a))\nbar()",
"execution_count": 118,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "4441412016\n4441412048\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "aaa = [10]\nprint(id(aaa))\ndef foo():\n print(id(aaa))\n aaa.append(100)\n print(id(aaa))\nprint(id(aaa))\nfoo()\nprint(id(aaa))",
"execution_count": 117,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "4472502600\n4472502600\n4472502600\n4472502600\n4472502600\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "aaa",
"execution_count": 116,
"outputs": [
{
"data": {
"text/plain": "[10, 100]"
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "t = ([3], [5])",
"execution_count": 119,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "type(t)",
"execution_count": 120,
"outputs": [
{
"data": {
"text/plain": "tuple"
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "t[0][0] = 100",
"execution_count": 121,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "t",
"execution_count": 122,
"outputs": [
{
"data": {
"text/plain": "([100], [5])"
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "id(t[0])",
"execution_count": 123,
"outputs": [
{
"data": {
"text/plain": "4472502536"
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "id(t[1])",
"execution_count": 124,
"outputs": [
{
"data": {
"text/plain": "4472892488"
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def foo(x):\n return 42 + x\n\ndef bar(x):\n return foo(x) * 10",
"execution_count": 127,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "bar(12)",
"execution_count": 128,
"outputs": [
{
"data": {
"text/plain": "540"
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def foo(x):\n return 42 + x\n\ndef bar(x):\n def foo(x):\n return -x\n return foo(x) * 10",
"execution_count": 129,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "bar(12)",
"execution_count": 130,
"outputs": [
{
"data": {
"text/plain": "-120"
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo(5)",
"execution_count": 131,
"outputs": [
{
"data": {
"text/plain": "47"
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def subs42(asdfasf):\n return asdfasf(42)",
"execution_count": 136,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from math import sqrt",
"execution_count": 137,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "subs42(sqrt)",
"execution_count": 138,
"outputs": [
{
"data": {
"text/plain": "6.48074069840786"
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "subs42(print)",
"execution_count": 139,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "42\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "students = ['Alice', 'Bob', 'Claudia']\ngrades = [5, 4, 3]",
"execution_count": 140,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook = {'Alice': 5, 'Bob': 4, 'Claudia': 3, 'Alice': 4}",
"execution_count": 145,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook['Alice']",
"execution_count": 146,
"outputs": [
{
"data": {
"text/plain": "4"
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook['Bob']",
"execution_count": 147,
"outputs": [
{
"data": {
"text/plain": "4"
},
"execution_count": 147,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook['Claudia']",
"execution_count": 148,
"outputs": [
{
"data": {
"text/plain": "3"
},
"execution_count": 148,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 149,
"outputs": [
{
"data": {
"text/plain": "{'Alice': 4, 'Bob': 4, 'Claudia': 3}"
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook['Alice'] = 5",
"execution_count": 150,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 151,
"outputs": [
{
"data": {
"text/plain": "{'Alice': 5, 'Bob': 4, 'Claudia': 3}"
},
"execution_count": 151,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook = {'Alice': {'Math': 4, 'Writing': 5},\n 'Bob': {'Math': 3, 'Writing': 4}}",
"execution_count": 152,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 153,
"outputs": [
{
"data": {
"text/plain": "{'Alice': {'Math': 4, 'Writing': 5}, 'Bob': {'Math': 3, 'Writing': 4}}"
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook['Bob']['Writing']",
"execution_count": 154,
"outputs": [
{
"data": {
"text/plain": "4"
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook1 = {'Alice': 4, 'Bob': 3}",
"execution_count": 159,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook1",
"execution_count": 160,
"outputs": [
{
"data": {
"text/plain": "{'Alice': 4, 'Bob': 3}"
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook2 = {'Bob': 3, 'Alice': 4}",
"execution_count": 161,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook2",
"execution_count": 162,
"outputs": [
{
"data": {
"text/plain": "{'Bob': 3, 'Alice': 4}"
},
"execution_count": 162,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook1 == gradebook2",
"execution_count": 163,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 163,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook = {'Alice': 5, 'Bob': 4, 'Claudia': 3, 'Alice': 4}",
"execution_count": 164,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 165,
"outputs": [
{
"data": {
"text/plain": "{'Alice': 4, 'Bob': 4, 'Claudia': 3}"
},
"execution_count": 165,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "del gradebook['Bob']",
"execution_count": 166,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 167,
"outputs": [
{
"data": {
"text/plain": "{'Alice': 4, 'Claudia': 3}"
},
"execution_count": 167,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook['Anna'] = 5",
"execution_count": 168,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 169,
"outputs": [
{
"data": {
"text/plain": "{'Alice': 4, 'Claudia': 3, 'Anna': 5}"
},
"execution_count": 169,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "for student, grade in gradebook.items():\n print(student, grade)",
"execution_count": 170,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Alice 4\nClaudia 3\nAnna 5\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list(gradebook.items())",
"execution_count": 171,
"outputs": [
{
"data": {
"text/plain": "[('Alice', 4), ('Claudia', 3), ('Anna', 5)]"
},
"execution_count": 171,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "for student in gradebook:\n print(student)",
"execution_count": 172,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Alice\nClaudia\nAnna\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for grade in gradebook.values():\n print(grade)",
"execution_count": 173,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "4\n3\n5\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 174,
"outputs": [
{
"data": {
"text/plain": "{'Alice': 4, 'Claudia': 3, 'Anna': 5}"
},
"execution_count": 174,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook['Bob']",
"execution_count": 175,
"outputs": [
{
"ename": "KeyError",
"evalue": "'Bob'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-175-264f18c99dee>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgradebook\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Bob'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 'Bob'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "'Bob' in gradebook",
"execution_count": 176,
"outputs": [
{
"data": {
"text/plain": "False"
},
"execution_count": 176,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def get_grade(gradebook, student):\n if student in gradebook:\n return gradebook[student]\n else:\n return None",
"execution_count": 177,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "get_grade(gradebook, \"Bob\")",
"execution_count": 178,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "get_grade(gradebook, \"Alice\")",
"execution_count": 179,
"outputs": [
{
"data": {
"text/plain": "4"
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook.get(\"Alice\", 0)",
"execution_count": 182,
"outputs": [
{
"data": {
"text/plain": "4"
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook.get(\"Bob\", 0)",
"execution_count": 183,
"outputs": [
{
"data": {
"text/plain": "0"
},
"execution_count": 183,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook['Maria'] = 5",
"execution_count": 184,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 185,
"outputs": [
{
"data": {
"text/plain": "{'Alice': 4, 'Claudia': 3, 'Anna': 5, 'Maria': 5}"
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook.update({'Christine': 3, 'Melony': 5})",
"execution_count": 186,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 187,
"outputs": [
{
"data": {
"text/plain": "{'Alice': 4, 'Claudia': 3, 'Anna': 5, 'Maria': 5, 'Christine': 3, 'Melony': 5}"
},
"execution_count": 187,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.2",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "lecture 5.ipynb",
"public": false
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment