Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created February 2, 2022 16:33
Show Gist options
  • Save ischurov/e973119e3ab9991eb73bbbc7fcaabdaf to your computer and use it in GitHub Desktop.
Save ischurov/e973119e3ab9991eb73bbbc7fcaabdaf to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "charged-canyon",
"metadata": {},
"source": [
"## Наука о данных\n",
"### Совместный бакалавриат ВШЭ-РЭШ, 2021-2022 учебный год\n",
"_Илья Щуров_\n",
"\n",
"[Страница курса](http://math-info.hse.ru/s21/j)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "immediate-prerequisite",
"metadata": {},
"outputs": [],
"source": [
"students = ['Alice', 'Bob', 'Claudia']\n",
"grades = [4, 5, 3]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "asian-drinking",
"metadata": {},
"outputs": [],
"source": [
"table = [('Alice', 4), ('Claudia', 3), ('Bob', 5)]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "saved-affairs",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table[2][1]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "sharp-skiing",
"metadata": {},
"outputs": [],
"source": [
"gradebook = {'Alice': 4, 'Bob': 5, 'Claudia': 3}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "northern-tongue",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook['Bob']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "drawn-tobacco",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook['Claudia']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "affecting-tumor",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 4, 'Bob': 5, 'Claudia': 3}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "daily-champagne",
"metadata": {},
"outputs": [],
"source": [
"gradebook['Daniel'] = 4"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "floral-generator",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 4, 'Bob': 5, 'Claudia': 3, 'Daniel': 4}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "complimentary-theology",
"metadata": {},
"outputs": [],
"source": [
"gradebook['Alice'] = 5"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "soviet-benjamin",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 5, 'Bob': 5, 'Claudia': 3, 'Daniel': 4}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "explicit-modem",
"metadata": {},
"outputs": [],
"source": [
"del gradebook['Bob']"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "committed-answer",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 5, 'Claudia': 3, 'Daniel': 4}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "funny-tracker",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Alice has grade 5.\n",
"Claudia has grade 3.\n",
"Daniel has grade 4.\n"
]
}
],
"source": [
"for student in gradebook:\n",
" print(f\"{student} has grade {gradebook[student]}.\")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "common-telescope",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Alice has grade 5.\n",
"Claudia has grade 3.\n",
"Daniel has grade 4.\n"
]
}
],
"source": [
"for student, grade in gradebook.items():\n",
" print(f\"{student} has grade {grade}.\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "controlled-qatar",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_items([('Alice', 5), ('Claudia', 3), ('Daniel', 4)])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook.items()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "lightweight-deployment",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "too many values to unpack (expected 2)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-23-031e2d12988b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mstudent\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgrade\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mgradebook\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"{student} has grade {grade}.\"\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: too many values to unpack (expected 2)"
]
}
],
"source": [
"for student, grade in gradebook:\n",
" print(f\"{student} has grade {grade}.\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "ecological-georgia",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([5, 3, 4])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook.values()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "amber-utilization",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 5, 'Claudia': 3, 'Daniel': 4}"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "public-solid",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook['Alice']"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "accepted-revolution",
"metadata": {},
"outputs": [],
"source": [
"comment = {'Alice': 'Good job!', 'Claudia': \"Try again\"}"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "pointed-fellowship",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Good job!'"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comment['Alice']"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "critical-stamp",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 'Good job!', 'Claudia': 'Try again'}"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comment"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "departmental-vaccine",
"metadata": {},
"outputs": [],
"source": [
"gradebook = {'Alice': [3, 4], 'Bob': [2], 'Claudia': [2, 4, 5]}"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "processed-miniature",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook['Claudia'][2]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "cloudy-grave",
"metadata": {},
"outputs": [],
"source": [
"squares = {1: 1, 2: 4, 5: 25}"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "detected-housing",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 1, 2: 4, 5: 25}"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"squares"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "flexible-buffalo",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"squares[1]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "solid-legislation",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"squares[2]"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "another-sending",
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "0",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-37-6b453444c824>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msquares\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[0m",
"\u001b[0;31mKeyError\u001b[0m: 0"
]
}
],
"source": [
"squares[0]"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "seven-neighborhood",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 1, 2: 4, 5: 25}"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"squares"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "typical-straight",
"metadata": {},
"outputs": [],
"source": [
"sums = {(1, 2): 3, (3, 5): 8, (1, 6): 7, (3, 2): 5}"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "limited-excellence",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{(1, 2): 3, (3, 5): 8, (1, 6): 7, (3, 2): 5}"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sums"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "secret-eagle",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sums[(1, 2)]"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "loving-johnson",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sums[1, 2]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "funded-stocks",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-43-f2142ead9929>\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;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\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[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
],
"source": [
"sums = {[1, 2]: 3, [3, 5]: 8}"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "grave-struggle",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-44-b97876a2fe30>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\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;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\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[0msums\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m8\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[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\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[0m\n\u001b[1;32m 5\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\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[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
],
"source": [
"a = [1, 2]\n",
"b = [3, 5]\n",
"sums = {a: 3, b: 8}\n",
"a[0] = 3\n",
"a[1] = 5\n",
"sums[[3, 5]]\n",
"# ???"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "european-location",
"metadata": {},
"outputs": [],
"source": [
"squares = {x: x ** 2 for x in range(2, 8)}"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "designing-swiss",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"squares"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "sophisticated-distance",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{3: 4, 1: 5, 2: 8}"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dict([(3, 4), (1, 5), (2, 8)])"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "ancient-niger",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'x': 1, 'y': 10, 'z': 14}"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dict(x=1, y=10, z=14)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "available-harvey",
"metadata": {},
"outputs": [],
"source": [
"def say_hello(first_name, last_name, *middle_names, **kwargs):\n",
" print(f\"Hello, {kwargs['title']} {first_name} \" + \n",
" \" \".join(middle_names) + \" \" + last_name)\n",
" print(kwargs)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "satisfactory-probe",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, Mrs Alice W Q Smith\n",
"{'title': 'Mrs', 'age': 18}\n"
]
}
],
"source": [
"say_hello('Alice', 'Smith', 'W', 'Q', title='Mrs', age=18)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "integrated-ordering",
"metadata": {},
"outputs": [],
"source": [
"gradebook = {'Alice': 4, 'Bob': 5, 'Claudia': 3}"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "vertical-desktop",
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'Daniel'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-67-36887fa2ac19>\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'Daniel'\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: 'Daniel'"
]
}
],
"source": [
"gradebook['Daniel']"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "present-saying",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Alice' in gradebook"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "local-boost",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Bob' in gradebook"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "becoming-constant",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Daniel' in gradebook"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "chinese-validity",
"metadata": {},
"outputs": [],
"source": [
"def get_grade(name):\n",
" if name in gradebook:\n",
" return gradebook[name]\n",
" else:\n",
" print(\"No such student\")"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "eligible-empty",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_grade(\"Alice\")"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "assisted-infection",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No such student\n"
]
}
],
"source": [
"get_grade(\"Daniel\")"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "civil-preserve",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook['Alice']"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "surprised-anime",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Yes\n"
]
}
],
"source": [
"if gradebook['Alice']:\n",
" print(\"Yes\")"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "ignored-ecuador",
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'Daniel'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-77-18195a243199>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mgradebook\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Daniel'\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Yes\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: 'Daniel'"
]
}
],
"source": [
"if gradebook['Daniel']:\n",
" print(\"Yes\")"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "prescribed-administration",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook.get(\"Alice\")"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "active-scope",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook.get(\"Bob\")"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "swiss-constant",
"metadata": {},
"outputs": [],
"source": [
"x = gradebook.get(\"Daniel\")"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "hairy-freeze",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "injured-technique",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook.get(\"Daniel\", 0)"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "genuine-somewhere",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 in [1, 2, 3, 4, 5]"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "suited-rough",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 in [1, 2, 3, 4, 5]"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "cloudy-footage",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"el\" in \"Hello\""
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "streaming-formula",
"metadata": {},
"outputs": [],
"source": [
"gradebook1 = {'Alice': 3, 'Bob': 4}\n",
"gradebook2 = {'Alice': 4, 'Claudia': 5}"
]
},
{
"cell_type": "code",
"execution_count": 90,
"id": "instrumental-elite",
"metadata": {},
"outputs": [],
"source": [
"gradebook1.update(gradebook2)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "changed-fantasy",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 4, 'Bob': 4, 'Claudia': 5}"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook1"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "arranged-parking",
"metadata": {},
"outputs": [],
"source": [
"gradebook1 = {'Alice': 3, 'Bob': 4}\n",
"gradebook2 = {'Alice': 4, 'Claudia': 5}\n",
"new_gradebook = gradebook1 | gradebook2"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "treated-building",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 4, 'Bob': 4, 'Claudia': 5}"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_gradebook"
]
},
{
"cell_type": "code",
"execution_count": 95,
"id": "exempt-foundation",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 5, 'Bob': 4}"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook = {'Alice': 3, 'Bob': 4}\n",
"backup_gradebook = gradebook\n",
"gradebook['Alice'] = 5\n",
"backup_gradebook"
]
},
{
"cell_type": "code",
"execution_count": 96,
"id": "brazilian-terrain",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 3, 'Bob': 4}"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook = {'Alice': 3, 'Bob': 4}\n",
"backup_gradebook = gradebook.copy()\n",
"gradebook['Alice'] = 5\n",
"backup_gradebook"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "ongoing-jordan",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 5, 'Bob': 4}"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook.setdefault(\"Alice\", 2)\n",
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "tracked-response",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 5, 'Bob': 4, 'Daniel': 2}"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook.setdefault(\"Daniel\", 2)\n",
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": 101,
"id": "plastic-blues",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 5, 'Bob': 4, 'Daniel': 2}"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Out[99]"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "hourly-bikini",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'gradebook.setdefault(\"Daniel\", 2)\\ngradebook'"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"In[99]"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "precise-sussex",
"metadata": {},
"outputs": [],
"source": [
"x = 100"
]
},
{
"cell_type": "code",
"execution_count": 105,
"id": "metric-cradle",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"globals()['x']"
]
},
{
"cell_type": "code",
"execution_count": 106,
"id": "referenced-instrument",
"metadata": {},
"outputs": [],
"source": [
"for i in range(10):\n",
" globals()[f'x{i}'] = i ** 2"
]
},
{
"cell_type": "code",
"execution_count": 107,
"id": "center-theme",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "future-facing",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x2"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "dramatic-oxygen",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x3"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "exclusive-husband",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"81"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x9"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "convertible-garden",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x0"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "expressed-night",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Alice 5\n",
"Bob 4\n",
"Daniel 2\n"
]
}
],
"source": [
"for k, v in gradebook.items():\n",
" print(k, v)"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "organic-screen",
"metadata": {},
"outputs": [],
"source": [
"gradebook = {'Bob': 3, 'Claudia': 5, 'Alice': 3}"
]
},
{
"cell_type": "code",
"execution_count": 116,
"id": "south-allen",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bob 3\n",
"Claudia 5\n",
"Alice 3\n"
]
}
],
"source": [
"for k, v in gradebook.items():\n",
" print(k, v)"
]
},
{
"cell_type": "code",
"execution_count": 117,
"id": "colonial-topic",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Bob': 3, 'Claudia': 5, 'Alice': 3}"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "advanced-proposition",
"metadata": {},
"outputs": [],
"source": [
"squares = {x: x**2 for x in range(100)}"
]
},
{
"cell_type": "code",
"execution_count": 119,
"id": "friendly-going",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 0,\n",
" 1: 1,\n",
" 2: 4,\n",
" 3: 9,\n",
" 4: 16,\n",
" 5: 25,\n",
" 6: 36,\n",
" 7: 49,\n",
" 8: 64,\n",
" 9: 81,\n",
" 10: 100,\n",
" 11: 121,\n",
" 12: 144,\n",
" 13: 169,\n",
" 14: 196,\n",
" 15: 225,\n",
" 16: 256,\n",
" 17: 289,\n",
" 18: 324,\n",
" 19: 361,\n",
" 20: 400,\n",
" 21: 441,\n",
" 22: 484,\n",
" 23: 529,\n",
" 24: 576,\n",
" 25: 625,\n",
" 26: 676,\n",
" 27: 729,\n",
" 28: 784,\n",
" 29: 841,\n",
" 30: 900,\n",
" 31: 961,\n",
" 32: 1024,\n",
" 33: 1089,\n",
" 34: 1156,\n",
" 35: 1225,\n",
" 36: 1296,\n",
" 37: 1369,\n",
" 38: 1444,\n",
" 39: 1521,\n",
" 40: 1600,\n",
" 41: 1681,\n",
" 42: 1764,\n",
" 43: 1849,\n",
" 44: 1936,\n",
" 45: 2025,\n",
" 46: 2116,\n",
" 47: 2209,\n",
" 48: 2304,\n",
" 49: 2401,\n",
" 50: 2500,\n",
" 51: 2601,\n",
" 52: 2704,\n",
" 53: 2809,\n",
" 54: 2916,\n",
" 55: 3025,\n",
" 56: 3136,\n",
" 57: 3249,\n",
" 58: 3364,\n",
" 59: 3481,\n",
" 60: 3600,\n",
" 61: 3721,\n",
" 62: 3844,\n",
" 63: 3969,\n",
" 64: 4096,\n",
" 65: 4225,\n",
" 66: 4356,\n",
" 67: 4489,\n",
" 68: 4624,\n",
" 69: 4761,\n",
" 70: 4900,\n",
" 71: 5041,\n",
" 72: 5184,\n",
" 73: 5329,\n",
" 74: 5476,\n",
" 75: 5625,\n",
" 76: 5776,\n",
" 77: 5929,\n",
" 78: 6084,\n",
" 79: 6241,\n",
" 80: 6400,\n",
" 81: 6561,\n",
" 82: 6724,\n",
" 83: 6889,\n",
" 84: 7056,\n",
" 85: 7225,\n",
" 86: 7396,\n",
" 87: 7569,\n",
" 88: 7744,\n",
" 89: 7921,\n",
" 90: 8100,\n",
" 91: 8281,\n",
" 92: 8464,\n",
" 93: 8649,\n",
" 94: 8836,\n",
" 95: 9025,\n",
" 96: 9216,\n",
" 97: 9409,\n",
" 98: 9604,\n",
" 99: 9801}"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"squares"
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "western-polymer",
"metadata": {},
"outputs": [],
"source": [
"del squares[78]"
]
},
{
"cell_type": "code",
"execution_count": 122,
"id": "favorite-accordance",
"metadata": {},
"outputs": [],
"source": [
"squares[101] = 101 ** 2"
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "domestic-arcade",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n",
"26\n",
"27\n",
"28\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
"34\n",
"35\n",
"36\n",
"37\n",
"38\n",
"39\n",
"40\n",
"41\n",
"42\n",
"43\n",
"44\n",
"45\n",
"46\n",
"47\n",
"48\n",
"49\n",
"50\n",
"51\n",
"52\n",
"53\n",
"54\n",
"55\n",
"56\n",
"57\n",
"58\n",
"59\n",
"60\n",
"61\n",
"62\n",
"63\n",
"64\n",
"65\n",
"66\n",
"67\n",
"68\n",
"69\n",
"70\n",
"71\n",
"72\n",
"73\n",
"74\n",
"75\n",
"76\n",
"77\n",
"79\n",
"80\n",
"81\n",
"82\n",
"83\n",
"84\n",
"85\n",
"86\n",
"87\n",
"88\n",
"89\n",
"90\n",
"91\n",
"92\n",
"93\n",
"94\n",
"95\n",
"96\n",
"97\n",
"98\n",
"99\n",
"101\n"
]
}
],
"source": [
"for k in squares:\n",
" print(k)"
]
},
{
"cell_type": "code",
"execution_count": 127,
"id": "moving-multimedia",
"metadata": {},
"outputs": [],
"source": [
"x = {1, 2, 5}\n",
"y = {1, 100, 100, 5, 12}"
]
},
{
"cell_type": "code",
"execution_count": 129,
"id": "appropriate-polymer",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"set"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(y)"
]
},
{
"cell_type": "code",
"execution_count": 130,
"id": "numerical-dispute",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 5, 12, 100}"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": 132,
"id": "nervous-festival",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 in x"
]
},
{
"cell_type": "code",
"execution_count": 133,
"id": "documentary-hypothetical",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 in x"
]
},
{
"cell_type": "code",
"execution_count": 135,
"id": "universal-joshua",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 5, 12, 100}"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x | y"
]
},
{
"cell_type": "code",
"execution_count": 136,
"id": "qualified-state",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 5}"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x & y"
]
},
{
"cell_type": "code",
"execution_count": 137,
"id": "general-malaysia",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{2}"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x - y"
]
},
{
"cell_type": "code",
"execution_count": 138,
"id": "charming-carolina",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.issubset(y)"
]
},
{
"cell_type": "code",
"execution_count": 140,
"id": "pursuant-ireland",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{1, 2}.issubset({1, 2, 3})"
]
},
{
"cell_type": "code",
"execution_count": 144,
"id": "sticky-cross",
"metadata": {},
"outputs": [],
"source": [
"my_list = [1, 2, 3, 4, 10, 100]"
]
},
{
"cell_type": "code",
"execution_count": 145,
"id": "detected-adobe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(set(my_list)) == len(my_list)"
]
},
{
"cell_type": "code",
"execution_count": 146,
"id": "remarkable-grove",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{(1, 2), (3, 4)}"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{(1, 2), (3, 4)}"
]
},
{
"cell_type": "code",
"execution_count": 147,
"id": "selective-sixth",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-147-a0ff115cb325>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \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;34m[\u001b[0m\u001b[0;36m3\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[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
],
"source": [
"{[1, 2], [3, 4]}"
]
},
{
"cell_type": "code",
"execution_count": 148,
"id": "systematic-failing",
"metadata": {},
"outputs": [],
"source": [
"x = {1, 2}\n",
"x.add(3)"
]
},
{
"cell_type": "code",
"execution_count": 149,
"id": "moved-ability",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3}"
]
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 157,
"id": "changing-paint",
"metadata": {},
"outputs": [],
"source": [
"frozen_x = frozenset({1, 2, 3})"
]
},
{
"cell_type": "code",
"execution_count": 158,
"id": "controversial-satin",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'frozenset' object has no attribute 'add'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-158-fac25e90e0d6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfrozen_x\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'frozenset' object has no attribute 'add'"
]
}
],
"source": [
"frozen_x.add(5)"
]
},
{
"cell_type": "code",
"execution_count": 153,
"id": "based-stress",
"metadata": {},
"outputs": [],
"source": [
"x = {1, 2}\n",
"x.add(3)"
]
},
{
"cell_type": "code",
"execution_count": 154,
"id": "adequate-planning",
"metadata": {},
"outputs": [],
"source": [
"x.remove(1)"
]
},
{
"cell_type": "code",
"execution_count": 155,
"id": "systematic-venezuela",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{2, 3}"
]
},
"execution_count": 155,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 160,
"id": "improving-psychology",
"metadata": {},
"outputs": [],
"source": [
"gradebook = {'Alice': {'Algebra': 4, 'Geometry': 5},\n",
" 'Bob': {'Algebra': 5, 'Geometry': 4}}"
]
},
{
"cell_type": "code",
"execution_count": 161,
"id": "rolled-polymer",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 161,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gradebook['Alice']['Geometry']"
]
},
{
"cell_type": "code",
"execution_count": 162,
"id": "architectural-prospect",
"metadata": {},
"outputs": [],
"source": [
"gradebook = {'Alice': {'Algebra': [4, 5], 'Geometry': [2, 5, 5]},\n",
" 'Bob': {'Algebra': [1, 2, 5], 'Geometry': []}}"
]
},
{
"cell_type": "code",
"execution_count": 163,
"id": "chicken-reducing",
"metadata": {},
"outputs": [],
"source": [
"my_tuple = ('Alice', [1, 4, 3])"
]
},
{
"cell_type": "code",
"execution_count": 164,
"id": "acceptable-gentleman",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Alice', [1, 4, 3])"
]
},
"execution_count": 164,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_tuple"
]
},
{
"cell_type": "code",
"execution_count": 165,
"id": "secondary-director",
"metadata": {},
"outputs": [],
"source": [
"my_tuple[1][0] = 5"
]
},
{
"cell_type": "code",
"execution_count": 166,
"id": "institutional-arthritis",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Alice', [5, 4, 3])"
]
},
"execution_count": 166,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_tuple"
]
},
{
"cell_type": "code",
"execution_count": 167,
"id": "outer-carol",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-167-3e5e94035bf6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m{\u001b[0m\u001b[0mmy_tuple\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: unhashable type: 'list'"
]
}
],
"source": [
"{my_tuple}"
]
},
{
"cell_type": "code",
"execution_count": 168,
"id": "occupational-hotel",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-2174745016731178947"
]
},
"execution_count": 168,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hash(\"Hello\")"
]
},
{
"cell_type": "code",
"execution_count": 169,
"id": "authorized-knock",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2327812432076005420"
]
},
"execution_count": 169,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hash(\"Hallo\")"
]
},
{
"cell_type": "code",
"execution_count": 181,
"id": "stupid-slope",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21969422862311469"
]
},
"execution_count": 181,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hash(2327812432076005420)"
]
},
{
"cell_type": "code",
"execution_count": 180,
"id": "appointed-fellowship",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 True\n",
"100 True\n",
"1000 True\n",
"10000 True\n",
"100000 True\n",
"1000000 True\n",
"10000000 True\n",
"100000000 True\n",
"1000000000 True\n",
"10000000000 True\n",
"100000000000 True\n",
"1000000000000 True\n",
"10000000000000 True\n",
"100000000000000 True\n",
"1000000000000000 True\n",
"10000000000000000 True\n",
"100000000000000000 True\n",
"1000000000000000000 True\n",
"10000000000000000000 False\n",
"100000000000000000000 False\n",
"1000000000000000000000 False\n",
"10000000000000000000000 False\n",
"100000000000000000000000 False\n",
"1000000000000000000000000 False\n",
"10000000000000000000000000 False\n",
"100000000000000000000000000 False\n",
"1000000000000000000000000000 False\n",
"10000000000000000000000000000 False\n",
"100000000000000000000000000000 False\n",
"1000000000000000000000000000000 False\n",
"10000000000000000000000000000000 False\n",
"100000000000000000000000000000000 False\n",
"1000000000000000000000000000000000 False\n",
"10000000000000000000000000000000000 False\n",
"100000000000000000000000000000000000 False\n",
"1000000000000000000000000000000000000 False\n",
"10000000000000000000000000000000000000 False\n",
"100000000000000000000000000000000000000 False\n",
"1000000000000000000000000000000000000000 False\n",
"10000000000000000000000000000000000000000 False\n",
"100000000000000000000000000000000000000000 False\n",
"1000000000000000000000000000000000000000000 False\n",
"10000000000000000000000000000000000000000000 False\n",
"100000000000000000000000000000000000000000000 False\n",
"1000000000000000000000000000000000000000000000 False\n",
"10000000000000000000000000000000000000000000000 False\n",
"100000000000000000000000000000000000000000000000 False\n",
"1000000000000000000000000000000000000000000000000 False\n",
"10000000000000000000000000000000000000000000000000 False\n"
]
}
],
"source": [
"for i in range(1, 50):\n",
" z = 10 ** i\n",
" print(z, z == hash(z))"
]
},
{
"cell_type": "code",
"execution_count": 182,
"id": "oriented-thread",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-3550055125485641917"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hash((1, 2))"
]
},
{
"cell_type": "code",
"execution_count": 183,
"id": "dried-problem",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-183-4b420d0158ba>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhash\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;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
],
"source": [
"hash([1, 2])"
]
},
{
"cell_type": "code",
"execution_count": 184,
"id": "green-diversity",
"metadata": {},
"outputs": [],
"source": [
"x = [1, 2, 3]\n",
"y = [1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": 185,
"id": "apparent-trigger",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x == y"
]
},
{
"cell_type": "code",
"execution_count": 186,
"id": "inside-membership",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 186,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x is y"
]
},
{
"cell_type": "code",
"execution_count": 187,
"id": "worth-infrared",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 187,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(x) == id(y)"
]
},
{
"cell_type": "code",
"execution_count": 189,
"id": "proud-simple",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 189,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 5\n",
"y = 5\n",
"x is y"
]
},
{
"cell_type": "code",
"execution_count": 190,
"id": "approximate-washington",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 190,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 12345\n",
"y = 12345\n",
"x is y"
]
},
{
"cell_type": "code",
"execution_count": 191,
"id": "approved-samba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 191,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x == y"
]
},
{
"cell_type": "code",
"execution_count": 192,
"id": "gentle-mentor",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"140581561766512"
]
},
"execution_count": 192,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(x)"
]
},
{
"cell_type": "code",
"execution_count": 193,
"id": "wireless-source",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"140581561766320"
]
},
"execution_count": 193,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adult-member",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"gist": {
"data": {
"description": "Lesson07",
"public": false
},
"id": ""
},
"kernelspec": {
"display_name": "Python 3.9 (system)",
"language": "python",
"name": "py39_system"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment