Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created February 21, 2018 21:16
Show Gist options
  • Save ischurov/231000c613a8570064659f0761a90fba to your computer and use it in GitHub Desktop.
Save ischurov/231000c613a8570064659f0761a90fba to your computer and use it in GitHub Desktop.
Lecture 2 Python ICEF 2018
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = [1, 2, 10]",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list.reverse()",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "[10, 2, 1]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_tuple = (3, 6, 10, 3)",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_tuple[2]",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "10"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_tuple[2] = 100",
"execution_count": 7,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-7-8de944ba5063>\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;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\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_tuple.append(6)",
"execution_count": 8,
"outputs": [
{
"output_type": "error",
"ename": "AttributeError",
"evalue": "'tuple' object has no attribute 'append'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-8-4a40ddf90b2d>\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[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_tuple.index(3)",
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "0"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_tuple",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "(3, 6, 10, 3)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list_from_tuple = list(my_tuple)",
"execution_count": 15,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list_from_tuple[0] = 100",
"execution_count": 16,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list_from_tuple",
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "[100, 6, 10, 3]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = [0, 10, 20]\nother_list = my_list\nother_list[0] = 100",
"execution_count": 20,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "[100, 10, 20]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "n = 5\nf = 1\nfor i in range(1, n + 1):\n f = f * i\nprint(f)",
"execution_count": 22,
"outputs": [
{
"output_type": "stream",
"text": "120\n",
"name": "stdout"
}
]
},
{
"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": 23,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(factorial(4))",
"execution_count": 24,
"outputs": [
{
"output_type": "stream",
"text": "24\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def plus(x, y):\n return x + y",
"execution_count": 25,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "plus(3, 7) * plus(2, 4)",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "60"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = plus(3, 7)",
"execution_count": 27,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 28,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 28,
"data": {
"text/plain": "10"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from math import factorial",
"execution_count": 29,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "factorial(700)",
"execution_count": 31,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 31,
"data": {
"text/plain": "2422040124750272179867875093812352218590983385729207299450679664929938160215647420444519051666484819249321456671497049842327525093874817343838393757631459228450828499972271274140160311057830558463636337124079332447820739281101037112665387537180790257577919273108262916904750405235055060084012219492892375635136296622020023178109619818046179906897450420548912610870589088056503913584562211037693288782960900195074130999799035970711436279339094292032866260496375825461427727555710003007752906141470639574390024988514914264449865006458873226951941899545970333910351588559232940829569276986080222200289966128343931630028789203382654749603473516314765262772257171154686716862814184728741187147936349501653197457455660413134506049122044947052623384682088864790673309569292384215611788014274954905914148362303226200246816441301934846080254998647325270606104512088058712293349862185399243309054299576381718806247238195232604642614329894070636163753672091232751612378348273840757873567717532879242518337119540602943609411629349009566043720836737401090882392975031224612531245642687296717053747734506443314924558119560479901478736209556925161517737110399754730551854066328420014728657896286936523787080206476327157136441318773432751007263108056958251693811280957243202460157111778617472683761623869704457588005158037495665069625778930898095725794710701639238231528115579619120287378689238934335198508665933917257143975277707590597511989345068701735940169672561864713107115016747368992690116082633762172346688969840862517264384000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = 100\ndef factorial(n):\n f = 1\n for i in range(1, n + 1):\n f = f * i\n return f\nprint(factorial(2))\nprint(f)",
"execution_count": 33,
"outputs": [
{
"output_type": "stream",
"text": "2\n100\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = 5\ndef foo():\n print(a)\nfoo()",
"execution_count": 34,
"outputs": [
{
"output_type": "stream",
"text": "5\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = 5\ndef foo():\n a = 7\n print(a)\nfoo()\nprint(a)",
"execution_count": 36,
"outputs": [
{
"output_type": "stream",
"text": "7\n5\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def bar():\n print(jhlihlkg)",
"execution_count": 37,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "jhlihlkg = 765\nbar()",
"execution_count": 39,
"outputs": [
{
"output_type": "stream",
"text": "765\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = 10\ndef change_a():\n global a\n a = 75\nchange_a()\nprint(a)",
"execution_count": 44,
"outputs": [
{
"output_type": "stream",
"text": "75\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = 5\nx = 3\ndef foo(x):\n a = x\nfoo(x)",
"execution_count": 45,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def my_sum(x, y, z):\n return x + y + z",
"execution_count": 46,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_sum(5, 7, 765, 5)",
"execution_count": 50,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "my_sum() takes 3 positional arguments but 4 were given",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-50-7e2fb58ff0bc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_sum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m765\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: my_sum() takes 3 positional arguments but 4 were given"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def hello(firstname, lastname):\n print(\"Hello\", firstname, lastname)\nhello(\"Alice\", \"Smith\")",
"execution_count": 51,
"outputs": [
{
"output_type": "stream",
"text": "Hello Alice Smith\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def hello(firstname=\"Unknown\", lastname=\"Unknown\"):\n print(\"Hello\", firstname, lastname)\nhello(\"Alice\", \"Smith\")",
"execution_count": 54,
"outputs": [
{
"output_type": "stream",
"text": "Hello Alice Smith\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "hello(\"Alice\")",
"execution_count": 55,
"outputs": [
{
"output_type": "stream",
"text": "Hello Alice Unknown\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "hello()",
"execution_count": 56,
"outputs": [
{
"output_type": "stream",
"text": "Hello Unknown Unknown\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "hello(lastname=\"Smith\", firstname=\"John\")",
"execution_count": 58,
"outputs": [
{
"output_type": "stream",
"text": "Hello John Smith\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "hello(\"John\", \"Smith\")",
"execution_count": 59,
"outputs": [
{
"output_type": "stream",
"text": "Hello John Smith\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def my_sum(x, y, z):\n return x + y + z",
"execution_count": 60,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = my_sum([1, 2], [4, 5, 6], [7, 8])",
"execution_count": 62,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x[3]",
"execution_count": 63,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 63,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def my_sum(*args):\n return sum(args)",
"execution_count": 67,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_sum(3, 6, 9, 76)",
"execution_count": 68,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 68,
"data": {
"text/plain": "94"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sum([6, 8, 9, 12])",
"execution_count": 69,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 69,
"data": {
"text/plain": "35"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sum(6, 7, 9)",
"execution_count": 70,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "sum expected at most 2 arguments, got 3",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-70-5939d9eb24de>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m9\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: sum expected at most 2 arguments, got 3"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def foo(x, y=7, *args):\n print(\"x =\", x)\n print(\"y =\", y)\n print(\"args =\", args)",
"execution_count": 71,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo()",
"execution_count": 72,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "foo() missing 1 required positional argument: 'x'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-72-c19b6d9633cf>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: foo() missing 1 required positional argument: 'x'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo(6)",
"execution_count": 73,
"outputs": [
{
"output_type": "stream",
"text": "x = 6\ny = 7\nargs = ()\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "foo(6, 10)",
"execution_count": 74,
"outputs": [
{
"output_type": "stream",
"text": "x = 6\ny = 10\nargs = ()\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "foo(6, 10, 12, 15)",
"execution_count": 76,
"outputs": [
{
"output_type": "stream",
"text": "x = 6\ny = 10\nargs = (12, 15)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def bar(x, y):\n print(\"x =\", x)\n print(\"y =\", y)\npair = [\"Hello\", \"World\"]\nbar(*pair)",
"execution_count": 78,
"outputs": [
{
"output_type": "stream",
"text": "x = Hello\ny = World\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "bar(*[\"Hello\", \"World\"])\nbar(\"Hello\", \"World\")",
"execution_count": 79,
"outputs": [
{
"output_type": "stream",
"text": "x = Hello\ny = World\nx = Hello\ny = World\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def print_hello():\n print(\"Hello\")",
"execution_count": 80,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def return_hello():\n return \"Hello\"",
"execution_count": 81,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s = return_hello()",
"execution_count": 82,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s",
"execution_count": 83,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 83,
"data": {
"text/plain": "'Hello'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s = print_hello()",
"execution_count": 84,
"outputs": [
{
"output_type": "stream",
"text": "Hello\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(s)",
"execution_count": 86,
"outputs": [
{
"output_type": "stream",
"text": "None\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print_hello()",
"execution_count": 87,
"outputs": [
{
"output_type": "stream",
"text": "Hello\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "return_hello()",
"execution_count": 88,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 88,
"data": {
"text/plain": "'Hello'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 5",
"execution_count": 90,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 91,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 91,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "students = [\"Alice\", \"Bob\", \"Claudia\"]\ngrades = [5, 4, 3]",
"execution_count": 92,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook = {\"Alice\": 5, \"Bob\": 4, \"Claudia\": 3}",
"execution_count": 93,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook[\"Bob\"]",
"execution_count": 94,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 94,
"data": {
"text/plain": "4"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook[4]",
"execution_count": 95,
"outputs": [
{
"output_type": "error",
"ename": "KeyError",
"evalue": "4",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-95-3ea720fd45e5>\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;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 4"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook[\"Claudia\"] = 4",
"execution_count": 97,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 98,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 98,
"data": {
"text/plain": "{'Alice': 5, 'Bob': 4, 'Claudia': 4}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook[\"John\"] = 2",
"execution_count": 99,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 100,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 100,
"data": {
"text/plain": "{'Alice': 5, 'Bob': 4, 'Claudia': 4, 'John': 2}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "del gradebook['Bob']",
"execution_count": 101,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 102,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 102,
"data": {
"text/plain": "{'Alice': 5, 'Claudia': 4, 'John': 2}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook[\"Bob\"]",
"execution_count": 103,
"outputs": [
{
"output_type": "error",
"ename": "KeyError",
"evalue": "'Bob'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-103-6e064fd53b7b>\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[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 'Bob'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "if \"Alice\" in gradebook:\n print(\"Alice is here\")",
"execution_count": 105,
"outputs": [
{
"output_type": "stream",
"text": "Alice is here\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook = dict([('Alice', 3), (\"Bob\", 4)])",
"execution_count": 106,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 107,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 107,
"data": {
"text/plain": "{'Alice': 3, 'Bob': 4}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list_of_pairs = [[4, 7], \n [2, 3], \n [6, 4]]\nlist_of_pairs[0]",
"execution_count": 108,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 108,
"data": {
"text/plain": "[4, 7]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list_of_pairs[1][0]",
"execution_count": 110,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 110,
"data": {
"text/plain": "2"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "students = [\"Alice\", \"Bob\", \"Claudia\"]\ngrades = [5, 4, 3]",
"execution_count": 111,
"outputs": []
},
{
"metadata": {
"trusted": true,
"scrolled": false
},
"cell_type": "code",
"source": "list(zip(students, grades))",
"execution_count": 121,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 121,
"data": {
"text/plain": "[('Alice', 5), ('Bob', 4), ('Claudia', 3)]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for student, grade in zip(students, grades):\n print(student, \"has grade\", grade)",
"execution_count": 123,
"outputs": [
{
"output_type": "stream",
"text": "Alice has grade 5\nBob has grade 4\nClaudia has grade 3\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook = dict(zip(students, grades))",
"execution_count": 124,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 125,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 125,
"data": {
"text/plain": "{'Alice': 5, 'Bob': 4, 'Claudia': 3}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for k in gradebook:\n print(k)",
"execution_count": 127,
"outputs": [
{
"output_type": "stream",
"text": "Alice\nBob\nClaudia\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for k, v in gradebook.items():\n print(k, v)",
"execution_count": 128,
"outputs": [
{
"output_type": "stream",
"text": "Alice 5\nBob 4\nClaudia 3\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true,
"scrolled": false
},
"cell_type": "code",
"source": "gradebook.items()",
"execution_count": 129,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 129,
"data": {
"text/plain": "dict_items([('Alice', 5), ('Bob', 4), ('Claudia', 3)])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for v in gradebook.values():\n print(v)",
"execution_count": 130,
"outputs": [
{
"output_type": "stream",
"text": "5\n4\n3\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook.keys()",
"execution_count": 131,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 131,
"data": {
"text/plain": "dict_keys(['Alice', 'Bob', 'Claudia'])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "{\"Alice\": 4, \"Alice\": 5}",
"execution_count": 132,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 132,
"data": {
"text/plain": "{'Alice': 5}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook = {\"Alice\": [5, 3], \"Bob\": [3], \"Claudia\": []}",
"execution_count": 136,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook['Alice']",
"execution_count": 137,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 137,
"data": {
"text/plain": "[5, 3]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sums = {(1, 2): 3, (5, 3): 8}",
"execution_count": 135,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sums[(1, 2)]",
"execution_count": 138,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 138,
"data": {
"text/plain": "3"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sums = {[1, 2]: 3, [5, 3]: 8}",
"execution_count": 139,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-139-3a8f9ca1ba6c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msums\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m8\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook",
"execution_count": 140,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 140,
"data": {
"text/plain": "{'Alice': [5, 3], 'Bob': [3], 'Claudia': []}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook[\"John\"]",
"execution_count": 141,
"outputs": [
{
"output_type": "error",
"ename": "KeyError",
"evalue": "'John'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-141-4f8e7bce80b7>\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\"John\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 'John'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gradebook.get(\"Jkll\", [])",
"execution_count": 151,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 151,
"data": {
"text/plain": "[]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": 152,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "some_strings = [\"123\", \"657\", \"12\"]\n# want [123, 657, 12]\nsome_ints = []\nfor element in some_strings:\n some_ints.append(int(element))\nsome_ints",
"execution_count": 153,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 153,
"data": {
"text/plain": "[123, 657, 12]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "$\\{x^2 \\mid x \\in \\mathbb Z\\}$"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "some_ints = [int(element) ** 2 \n for element in some_strings]\nsome_ints",
"execution_count": 155,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 155,
"data": {
"text/plain": "[15129, 431649, 144]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ints = [-6, 7, 8, 9, 12, -8, 7, 0]",
"execution_count": 164,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "positives = [x for x in ints if x >= 0]",
"execution_count": 165,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "positives",
"execution_count": 166,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 166,
"data": {
"text/plain": "[7, 8, 9, 12, 7, 0]"
},
"metadata": {}
}
]
},
{
"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.6.1",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "Lecture 2 Python ICEF 2018",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment