Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created January 12, 2021 17:35
Show Gist options
  • Save ischurov/a1745742ce6bd432325ecc6d6db5a874 to your computer and use it in GitHub Desktop.
Save ischurov/a1745742ce6bd432325ecc6d6db5a874 to your computer and use it in GitHub Desktop.
lecture01.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "2 + 3",
"execution_count": 1,
"outputs": [
{
"data": {
"text/plain": "5"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(2 + 5) * 3 + 7",
"execution_count": 2,
"outputs": [
{
"data": {
"text/plain": "28"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "4 / 2",
"execution_count": 3,
"outputs": [
{
"data": {
"text/plain": "2.0"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "1 * 2",
"execution_count": 4,
"outputs": [
{
"data": {
"text/plain": "2"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "4 / 3",
"execution_count": 5,
"outputs": [
{
"data": {
"text/plain": "1.3333333333333333"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 12\ny = 32",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "4 + x",
"execution_count": 7,
"outputs": [
{
"data": {
"text/plain": "16"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 8,
"outputs": [
{
"data": {
"text/plain": "12"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 15",
"execution_count": 9,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 10,
"outputs": [
{
"data": {
"text/plain": "15"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = x + 1",
"execution_count": 11,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 12,
"outputs": [
{
"data": {
"text/plain": "16"
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "%whos",
"execution_count": 14,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Variable Type Data/Info\n--------------------------------------\nblack_reformat function <function black_reformat at 0x10d511820>\nx int 16\ny int 32\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "3",
"execution_count": 15,
"outputs": [
{
"data": {
"text/plain": "3"
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "3.0",
"execution_count": 16,
"outputs": [
{
"data": {
"text/plain": "3.0"
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s = \"Hello, world!\"",
"execution_count": 17,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s",
"execution_count": 18,
"outputs": [
{
"data": {
"text/plain": "'Hello, world!'"
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s = 'Корпорация \"Майкрософт\"'",
"execution_count": 20,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s",
"execution_count": 21,
"outputs": [
{
"data": {
"text/plain": "'Корпорация \"Майкрософт\"'"
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s = \"It's me\"",
"execution_count": 22,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s",
"execution_count": 23,
"outputs": [
{
"data": {
"text/plain": "\"It's me\""
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "type(s)",
"execution_count": 24,
"outputs": [
{
"data": {
"text/plain": "str"
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "type(\"Hello\")",
"execution_count": 25,
"outputs": [
{
"data": {
"text/plain": "str"
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "type(2)",
"execution_count": 26,
"outputs": [
{
"data": {
"text/plain": "int"
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "type(2.0)",
"execution_count": 27,
"outputs": [
{
"data": {
"text/plain": "float"
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = float(\"12.34\")",
"execution_count": 29,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 30,
"outputs": [
{
"data": {
"text/plain": "12.34"
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x + 0.2",
"execution_count": 31,
"outputs": [
{
"data": {
"text/plain": "12.54"
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "\"12.34\" + 0.2",
"execution_count": 32,
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate str (not \"float\") to str",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-32-690faa7a9d66>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m\"12.34\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m0.2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"float\") to str"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "float(\"Hello\")",
"execution_count": 33,
"outputs": [
{
"ename": "ValueError",
"evalue": "could not convert string to float: 'Hello'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-33-ff6885467a56>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfloat\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[0m",
"\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'Hello'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s = input(\"Как вас зовут? \")\nprint(\"Здравствуйте,\", s)",
"execution_count": 34,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Как вас зовут? Илья\nЗдравствуйте, Илья\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(1, 2, 3)",
"execution_count": 35,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1 2 3\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"Hello\", \"World\")",
"execution_count": 36,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello World\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Напишите код, который запрашивает с клавиатуры целое число\n# и возвращает его, умноженное на два\n# например, если пользователь ввёл 3, программа должна напечатать 6",
"execution_count": 38,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "number = input(\"Введите число: \")\n\nprint(number * 2)",
"execution_count": 39,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Введите число: 123\n123123\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "numberber",
"execution_count": 40,
"outputs": [
{
"data": {
"text/plain": "'123'"
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = \"Hello\"\nb = \"World\"\na + b",
"execution_count": 41,
"outputs": [
{
"data": {
"text/plain": "'HelloWorld'"
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "\"Hello\" * 3",
"execution_count": 42,
"outputs": [
{
"data": {
"text/plain": "'HelloHelloHello'"
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "user_input = input(\"Введите число: \")\nnumber = int(user_input)\nprint(number * 2)",
"execution_count": 43,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Введите число: 12\n24\n"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "`TypeError: 'str' object is not callable`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "input = \"Hello!\"",
"execution_count": 44,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "input",
"execution_count": 45,
"outputs": [
{
"data": {
"text/plain": "'Hello!'"
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "user_input = input(\"Введите число\")",
"execution_count": 46,
"outputs": [
{
"ename": "TypeError",
"evalue": "'str' object is not callable",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-46-75b2586fd7d5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0muser_input\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Введите число\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'str' object is not callable"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "3 + 2",
"execution_count": 47,
"outputs": [
{
"data": {
"text/plain": "5"
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sqrt(10)",
"execution_count": 48,
"outputs": [
{
"ename": "NameError",
"evalue": "name 'sqrt' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-48-0c2ba68b944d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'sqrt' is not defined"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import math",
"execution_count": 49,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "math.sqrt(10)",
"execution_count": 50,
"outputs": [
{
"data": {
"text/plain": "3.1622776601683795"
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import math as m",
"execution_count": 51,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "m.sqrt(10)",
"execution_count": 52,
"outputs": [
{
"data": {
"text/plain": "3.1622776601683795"
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from math import sqrt",
"execution_count": 53,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sqrt(10)",
"execution_count": 54,
"outputs": [
{
"data": {
"text/plain": "3.1622776601683795"
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "input",
"execution_count": 55,
"outputs": [
{
"data": {
"text/plain": "'Hello!'"
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "del input",
"execution_count": 56,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "input",
"execution_count": 57,
"outputs": [
{
"data": {
"text/plain": "<bound method Kernel.raw_input of <ipykernel.ipkernel.IPythonKernel object at 0x10d424fd0>>"
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 58,
"outputs": [
{
"data": {
"text/plain": "12.34"
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "del x",
"execution_count": 59,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 60,
"outputs": [
{
"ename": "NameError",
"evalue": "name 'x' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-60-6fcf9dfbd479>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'x' is not defined"
]
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Задача\nЧисла Фибоначчи:\n$$x_1=1,\\quad x_2=1,\\quad x_{k+1} = x_k + x_{k-1}, \\quad k = 2, 3, \\ldots$$\n\n$$1, 1, 2, 3, 5, 8, 13,\\ldots$$"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "prev = 1\ncur = 1",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "new = cur + prev\nprint(new)\nprev = cur\ncur = new",
"execution_count": 21,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "610\n"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "**Пример.**"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 1",
"execution_count": 63,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = x * 2\nprint(x)",
"execution_count": 74,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "2048\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "z = 12",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "py38_system",
"display_name": "Python 3.8 (system)",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.6",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "lecture01.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment