{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# Язык программирования Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Особенности синтаксиса" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "source": [ "import this" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'dgdfgd\\ndfgdfg\\ndgdfg\\ndfgdfg'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# комментарий однострочный\n", "x = 1 # fghfdghf\n", "'''\n", "комментарий \n", "многострочный\n", "'''\n", "x = 6\n", "\"\"\"dgdfgd\n", "dfgdfg\n", "dgdfg\n", "dfgdfg\"\"\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 3\n" ] } ], "source": [ "x = 2; y = 3 # так лучше не делать!\n", "print(x, y)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "if x == 1 or x == 2 \\\n", "or x == 3 or x == 4:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "if (x == 2 or x == 3\n", " or x == 4):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "if x==2: print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Переменные" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "s = 'something'\n", "n = 258657346587346589374\n", "v = 4.56" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(s)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(n)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(v)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "258657346587346589378\n" ] } ], "source": [ "n = n + 1\n", "print(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Типы данных" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Числа" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "5.6\n" ] } ], "source": [ "n = 5\n", "k = 5.6\n", "print(n)\n", "print(k)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.0\n", "5\n" ] } ], "source": [ "n = float(n)\n", "k = int(k)\n", "print(n)\n", "print(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Операции" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "x + y\n", "\n", "x − y\n", "\n", "x ∗ y\n", "\n", "x/y\n", "\n", "x//y\n", "\n", "x%y\n", "\n", "x∗∗y\n", "\n", "−x\n", "\n", "*Функции:*\n", "\n", "abs()\n", "\n", "pow() \n", "\n", "divmod() \n", "\n", "round() \n", "\n", "*Модули* math, random" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12.5\n", "12.195121951219512\n" ] } ], "source": [ "print(100 / 8)\n", "print(100 / 8.2)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n", "8.0\n", "8.144\n" ] } ], "source": [ "print(100 // 8)\n", "print(101.8 // 12.5)\n", "print(101.8 / 12.5)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 % 3" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1024" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** 10" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.509400928686412" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2.1**2.3" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 2)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "divmod(17, 5)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(12.6)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(12.5)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(11.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Логические переменные" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* True, False\n", "* Операции и выражения:\n", "- '>'\n", "- <\n", "- ==\n", "- !=\n", "- not x\n", "- x and y\n", "- x or y\n", "- x in A\n", "- a < x < b\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 < 3 < 4 < 5" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What is your name?\n", "Sasha\n", "Hello, Sasha!\n" ] } ], "source": [ "print('What is your name?')\n", "k = input()\n", "print('Hello,', k + '!')" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "5^2 = 25\n" ] } ], "source": [ "n = int(input())\n", "print(f'{n}^2 = {n**2}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Ветвление и оператор выбора" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is positive\n", "x is even\n" ] } ], "source": [ "if x > 0: \n", " print ('x is positive')\n", "\n", "if x % 2 == 0: \n", " print('x is even') \n", "else: \n", " print('x is odd')" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is less than y\n" ] } ], "source": [ "if x < y: \n", " print('x is less than y')\n", "elif x > y: \n", " print('x is greater than y') \n", "else:\n", " print('x and y are equal')" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is less than y\n" ] } ], "source": [ "if x == y: \n", " print('x and y are equal')\n", "else: \n", " if x < y: \n", " print('x is less than y')\n", " else: \n", " print('x is greater than y')" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "X = 1\n", "Y = 2\n", "Z = 3\n", "\n", "if X != 0:\n", " A = Y\n", "else:\n", " A = Z\n", "\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "X = 1\n", "Y = 2\n", "Z = 3\n", "\n", "A = Y if X else Z # Трехместное выражение if/else\n", "\n", "print(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Проверка истинности в Python\n", "•\tОперации сравнения возвращают True или False\n", "•\tЛюбое число, не равное 0, или непустой объект – истина\n", "•\tЧисла, равные 0, пустые объекты и значение None – ложь\n", "•\tОперации сравнения применяются к структурам данных рекурсивно" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Цикл while" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "7\n", "9\n", "11\n", "13\n" ] } ], "source": [ "i = 5\n", "while i < 15:\n", " print(i)\n", " #i = i + 2\n", " i += 2" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "7\n", "9\n", "11\n", "13\n", "!\n" ] } ], "source": [ "# имитация until\n", "i = 5\n", "while True:\n", " print(i)\n", " i += 2\n", " if i >= 15: \n", " break\n", "print('!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Цикл for" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hheelllloo wwoorrlldd" ] } ], "source": [ "for i in 'hello world':\n", " print(i * 2, end = '')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hheelllloo wwoorrlldd" ] } ], "source": [ "s = 'hello world'\n", "for i in range(len(s)):\n", " print(s[i] * 2, end = '')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "5\n", "7\n", "9\n", "11\n", "13\n" ] } ], "source": [ "for i in range(3, 15, 2):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for i in [1, 2, 3]:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 6\n", "1 2\n", "2 8\n" ] } ], "source": [ "list = [6, 2, 8]\n", "for index, el in enumerate(list):\n", " print(index, el)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 6\n", "2 2\n", "3 8\n" ] } ], "source": [ "list = [6, 2, 8]\n", "for index, el in enumerate(list, start = 1):\n", " print(index, el)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Оператор continue" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hheellll wwrrlldd" ] } ], "source": [ "for i in 'hello world':\n", " if i == 'o':\n", " continue\n", " print(i * 2, end='')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Оператор break" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hheellll" ] } ], "source": [ "for i in 'hello world':\n", " if i == 'o':\n", " break\n", " print(i * 2, end='')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Еlse в цикле" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "for i in 'hello woarld':\n", " if i == 'a':\n", " break\n", "else: # если выход из цикла произошел без помощи break\n", " print('Буквы a в строке нет')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Инструкция pass " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Не выполняет никаких действий и используется в случаях, когда синтаксис языка требует наличия инструкции, но никаких полезных действий в этой точке программы выполнять нельзя.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yes\n" ] } ], "source": [ "x = 4\n", "if x==5:\n", " pass\n", "else:\n", " print('yes')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.13" } }, "nbformat": 4, "nbformat_minor": 4 }