Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created February 1, 2022 18:11
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/2e531bcdd0665feef8d7cc136915cd58 to your computer and use it in GitHub Desktop.
Save ischurov/2e531bcdd0665feef8d7cc136915cd58 to your computer and use it in GitHub Desktop.
Lesson06
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "## Наука о данных\n### Совместный бакалавриат ВШЭ-РЭШ, 2021-2022 учебный год\n_Илья Щуров_\n\n[Страница курса](http://math-info.hse.ru/s21/j)"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = [5, 4, 2, 10, 6, 1, 15]",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list.sort()",
"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": "[1, 2, 4, 5, 6, 10, 15]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = [5, 4, 2, 10, 6, 1, 15]",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "id(my_list)",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "140504351774400"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list.sort()",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "id(my_list)",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "140504351774400"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = [5, 4, 2, 10, 6, 1, 15]\nmy_list.sort(reverse=True)",
"execution_count": 9,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "[15, 10, 6, 5, 4, 2, 1]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "4 < \"Hello\"",
"execution_count": 11,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'<' not supported between instances of 'int' and 'str'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-11-43ab46f8d729>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m4\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: '<' not supported between instances of 'int' and 'str'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = [5, 4, 2, 10, 6, 1, 15, \"Hello\"]\nmy_list.sort(reverse=True)",
"execution_count": 12,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'<' not supported between instances of 'int' and 'str'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-12-e21a0c58d822>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmy_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m15\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreverse\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\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: '<' not supported between instances of 'int' and 'str'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = [\"One\", \"Two\", \"Three\", \"Four\", \"Five\"]\nmy_list.sort()\nmy_list",
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 14,
"data": {
"text/plain": "['Five', 'Four', 'One', 'Three', 'Two']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ord(\"F\")",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "70"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ord(\"Ф\")",
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "1060"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "\"F\" < \"Ф\"",
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "chr(123)",
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "'{'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "chr(70)",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "'F'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "\"Fiv\" < \"Five\"",
"execution_count": 29,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 29,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_list = [\"One\", \"Two\", \"Three\", \"Four\", \"Five\"]\nsorted_list = sorted(my_list, reverse=True)\nmy_list",
"execution_count": 32,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 32,
"data": {
"text/plain": "['One', 'Two', 'Three', 'Four', 'Five']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted_list",
"execution_count": 33,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 33,
"data": {
"text/plain": "['Two', 'Three', 'One', 'Four', 'Five']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "words = ['This', 'is', 'a', 'test']",
"execution_count": 34,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def subs2(f):\n return f(2)",
"execution_count": 35,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def plusodin(x):\n return x + 1",
"execution_count": 36,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "subs2(plusodin)",
"execution_count": 37,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 37,
"data": {
"text/plain": "3"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "plusodin(5)",
"execution_count": 38,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 38,
"data": {
"text/plain": "6"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from math import sqrt",
"execution_count": 39,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "subs2(sqrt)",
"execution_count": 40,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 40,
"data": {
"text/plain": "1.4142135623730951"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "len(\"Hello\")",
"execution_count": 41,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 41,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "words",
"execution_count": 43,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 43,
"data": {
"text/plain": "['This', 'is', 'a', 'test']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(words, key=len)",
"execution_count": 42,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 42,
"data": {
"text/plain": "['a', 'is', 'This', 'test']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "numbers = [-6, -3, 10, 3, -16, 1, -23]",
"execution_count": 53,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(numbers, key=abs)",
"execution_count": 54,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 54,
"data": {
"text/plain": "[1, -3, 3, -6, 10, -16, -23]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "table = [[4, 2, 123], \n [6, 4, 12], \n [876, 12, 123],\n [12, 231, 10]]",
"execution_count": 55,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "table[3]",
"execution_count": 56,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 56,
"data": {
"text/plain": "[12, 231, 10]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "table[3][1]",
"execution_count": 57,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 57,
"data": {
"text/plain": "231"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(table)",
"execution_count": 58,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 58,
"data": {
"text/plain": "[[4, 2, 123], [6, 4, 12], [12, 231, 10], [876, 12, 123]]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[3, 2] < [15, 32]",
"execution_count": 59,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 59,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[17, 2] < [15, 32]",
"execution_count": 60,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 60,
"data": {
"text/plain": "False"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[15, 2] < [15, 32]",
"execution_count": 61,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 61,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[15, 32, 15] < [15, 32, 15, 123]",
"execution_count": 62,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 62,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = [4]\na.append(a)\n\nb = [4]\nb.append(b)",
"execution_count": 65,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a < b",
"execution_count": 66,
"outputs": [
{
"output_type": "error",
"ename": "RecursionError",
"evalue": "maximum recursion depth exceeded in comparison",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRecursionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-66-8c747544e8c8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mRecursionError\u001b[0m: maximum recursion depth exceeded in comparison"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted([a, b])",
"execution_count": 67,
"outputs": [
{
"output_type": "error",
"ename": "RecursionError",
"evalue": "maximum recursion depth exceeded in comparison",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRecursionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-67-c78440b07860>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msorted\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\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;31mRecursionError\u001b[0m: maximum recursion depth exceeded in comparison"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(a)",
"execution_count": 68,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'<' not supported between instances of 'list' and 'int'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-68-ca63776b8466>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msorted\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\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: '<' not supported between instances of 'list' and 'int'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a",
"execution_count": 69,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 69,
"data": {
"text/plain": "[4, [...]]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = [[4]]\na.append(a)",
"execution_count": 70,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(a)",
"execution_count": 71,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'<' not supported between instances of 'list' and 'int'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-71-ca63776b8466>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msorted\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\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: '<' not supported between instances of 'list' and 'int'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "table = [[1, 2, 3],\n [1, 0, 7],\n [0, 5, 3]]\nsorted(table)",
"execution_count": 72,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 72,
"data": {
"text/plain": "[[0, 5, 3], [1, 0, 7], [1, 2, 3]]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# задача: отсортируйте table по первому столбцу\n# при совпадении элементов первого столбца строки должны сохранить свой порядок\n# нужно использовать key\n# подсказка: вам нужна функция, \n# с помощью которой можно получить первый элемент каждой строки",
"execution_count": 73,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def get_first_element(row):\n return row[0]\n\nsorted(table, key=get_first_element)",
"execution_count": 74,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 74,
"data": {
"text/plain": "[[0, 5, 3], [1, 2, 3], [1, 0, 7]]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(table, key=lambda row: row[0])",
"execution_count": 76,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 76,
"data": {
"text/plain": "[[0, 5, 3], [1, 2, 3], [1, 0, 7]]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(lambda x: x ** 2)(5)",
"execution_count": 78,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 78,
"data": {
"text/plain": "25"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "subs2(lambda x: x ** 3)",
"execution_count": 79,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 79,
"data": {
"text/plain": "8"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_funcs = []\nfor i in range(10):\n my_funcs.append(lambda x: x + i)",
"execution_count": 80,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_funcs[3](2)",
"execution_count": 81,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 81,
"data": {
"text/plain": "11"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "i = 100",
"execution_count": 82,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_funcs[3](2)",
"execution_count": 83,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 83,
"data": {
"text/plain": "102"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_funcs[7](2)",
"execution_count": 84,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 84,
"data": {
"text/plain": "102"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_funcs = []\nfor i in range(10):\n my_funcs.append(lambda x, i=i: x + i)\n # lambda x: i=3: x + i",
"execution_count": 85,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_funcs[3](2)",
"execution_count": 86,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 86,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_funcs",
"execution_count": 87,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 87,
"data": {
"text/plain": "[<function __main__.<lambda>(x, i=0)>,\n <function __main__.<lambda>(x, i=1)>,\n <function __main__.<lambda>(x, i=2)>,\n <function __main__.<lambda>(x, i=3)>,\n <function __main__.<lambda>(x, i=4)>,\n <function __main__.<lambda>(x, i=5)>,\n <function __main__.<lambda>(x, i=6)>,\n <function __main__.<lambda>(x, i=7)>,\n <function __main__.<lambda>(x, i=8)>,\n <function __main__.<lambda>(x, i=9)>]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from operator import itemgetter",
"execution_count": 88,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "itemgetter(0)([5, 1, 3, 2, 1])",
"execution_count": 89,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 89,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(table, key=get_first_element)",
"execution_count": 90,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 90,
"data": {
"text/plain": "[[0, 5, 3], [1, 2, 3], [1, 0, 7]]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(table, key=itemgetter(0))",
"execution_count": 91,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 91,
"data": {
"text/plain": "[[0, 5, 3], [1, 2, 3], [1, 0, 7]]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "itemgetter(1, 4)([0, 10, 1, 15, 14])",
"execution_count": 96,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 96,
"data": {
"text/plain": "(10, 14)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[x ** 2 for x in range(10)]",
"execution_count": 110,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 110,
"data": {
"text/plain": "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[x ** 2 for x in range(10) if x % 3 == 1]",
"execution_count": 99,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 99,
"data": {
"text/plain": "[1, 16, 49]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[x * y for x in range(1, 5) for y in range(0, 4) if x == y]",
"execution_count": 106,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 106,
"data": {
"text/plain": "[1, 4, 9]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "new_list = []\nfor x in range(1, 5):\n for y in range(0, 4):\n if x == y:\n new_list.append(x * y)\nnew_list",
"execution_count": 107,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 107,
"data": {
"text/plain": "[1, 4, 9]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list(map(lambda x: x ** 2, range(10)))",
"execution_count": 109,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 109,
"data": {
"text/plain": "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "numbers = [1, 2, 10, 4, 3, 2]\nlist(map(str, numbers))",
"execution_count": 112,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 112,
"data": {
"text/plain": "['1', '2', '10', '4', '3', '2']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "numbers = [1, 2, 10, 4, 3, 2]\n\", \".join(map(str, numbers))",
"execution_count": 113,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 113,
"data": {
"text/plain": "'1, 2, 10, 4, 3, 2'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "\", \".join([str(x) for x in numbers])",
"execution_count": 114,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 114,
"data": {
"text/plain": "'1, 2, 10, 4, 3, 2'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s = \"Hello\"",
"execution_count": 115,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s.index(\"e\")",
"execution_count": 116,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 116,
"data": {
"text/plain": "1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s.index(\"ll\")",
"execution_count": 117,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 117,
"data": {
"text/plain": "2"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s.isdigit()",
"execution_count": 118,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 118,
"data": {
"text/plain": "False"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 12\ns = f\"Variable = {x}, Square = {x ** 2}\"",
"execution_count": 121,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s",
"execution_count": 122,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 122,
"data": {
"text/plain": "'Variable = 12, Square = 144'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f\"Result: {4 + 5}\"",
"execution_count": 123,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 123,
"data": {
"text/plain": "'Result: 9'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f\"Result: {sqrt(2)}\"",
"execution_count": 124,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 124,
"data": {
"text/plain": "'Result: 1.4142135623730951'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f\"Result: {sqrt(2):.2f}\"",
"execution_count": 130,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 130,
"data": {
"text/plain": "'Result: 1.41'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for i in range(11):\n print(f\"{i:02}\")",
"execution_count": 140,
"outputs": [
{
"output_type": "stream",
"text": "00\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "\"10\" < \"02\"",
"execution_count": 134,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 134,
"data": {
"text/plain": "False"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# 2 → \"2\"\n# 2 → \"02\"",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 0\nif x > 0:\n print(\"Positive\")\nelse:\n print(\"Negative or zero\")",
"execution_count": 141,
"outputs": [
{
"output_type": "stream",
"text": "Negative or zero\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "result = \"Positive\" if x > 0 else \"Negative or zero\"\nresult",
"execution_count": 142,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 142,
"data": {
"text/plain": "'Negative or zero'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(lambda x: \"Positive\" if x > 0 else \"Negative or zero\")(12)",
"execution_count": 143,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 143,
"data": {
"text/plain": "'Positive'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 12\ns = f\"Result: {'Positive' if x > 0 else 'Negative or zero'}\"\ns",
"execution_count": 146,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 146,
"data": {
"text/plain": "'Result: Positive'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "1 if x > 0 else -1 if x < 0 else 0",
"execution_count": 147,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 147,
"data": {
"text/plain": "1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "py39_system",
"display_name": "Python 3.9 (system)",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.9.0",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "Lesson06",
"public": false
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment