{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Кортежи" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "упорядоченный неизменяемый набор объектов, в который могут одновременно входить объекты разных типов (числа, строки и другие структуры, в том числе и кортежи)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "использует меньше памяти, чем список" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "вместо квадратных скобок использует круглые (можно совсем без скобок)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "не допускает изменений, в него нельзя добавить новый элемент, хотя он может содержать объекты, которые можно изменить" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = 1, 2 \n", "t " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, [2, 3])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = 1, [2,3] \n", "t " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "t[0] = 2 " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, [2, 3, 4])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1].append(4)\n", "t " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('a', 'b', 'c')" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuple('abc') " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n", "4\n", "(12, 'b', 34.6, 'derevo')\n" ] } ], "source": [ "t = (x, s1, y, s2) = (12, 'b', 34.6, 'derevo')\n", "print(x)\n", "x = 4\n", "print(x)\n", "print(t)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = ()\n", "t" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "divmod(5, 3)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 'a', 'b')" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1 = (1, 2, 3)\n", "t2 = ('a', 'b') \n", "t3 = t1 + t2\n", "t3" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('one', 'two', 'one', 'two', 'one', 'two')" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1 = ('one', 'two')\n", "t4 = t1 * 3\n", "t4" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t3[2]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t3[-2]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 'a')" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t3[1::2]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min(t4)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'two'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(t4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }