{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

Day 09 Lists and Tuples in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recap\n", "\n", "- Strings\n", "- String Methods\n", "- List Intro..\n", "-------\n", "\n", "\n", "### Todays Objectives\n", "\n", "- Accessing Elements of List\n", " - Indexing\n", " - Slicing\n", "- List Methods\n", "- Tuple\n", " - Accessing Elements of Tuple\n", " - Indexing\n", " - Slicing\n", " - Tuple Methods" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "li = [1, 2, 5, 4, 8, 85]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 85 1 105\n" ] } ], "source": [ "print(len(li), max(li), min(li), sum(li))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] }, { "ename": "TypeError", "evalue": "'<' not supported between instances of 'str' and 'int'", "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[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mli2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mli2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: '<' not supported between instances of 'str' and 'int'" ] } ], "source": [ "li2 = [1, 2, 5, 4, 8, 85, 'Python']\n", "\n", "\n", "print(len(li2))\n", "print(min(li2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing Elements from the list\n", "\n", "\n", "- Indexing\n", " - +ve\n", " - -ve\n", "- Slicing \n", " - +ve\n", " - -ve" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(li[0])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "85\n" ] } ], "source": [ "print(li[5])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "print(li[-2])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(li[-6])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\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[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mli\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "print(li[7])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "list indices must be integers or slices, not float", "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[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mli\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1.2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not float" ] } ], "source": [ "print(li[1.2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Syntax\n", "\n", "list_var[SI: EI]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] [1, 2] [1, 2] [1, 2, 5, 4, 8, 85]\n" ] } ], "source": [ "print(li[0:1], li[0: 2], li[: 2], li[0:])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 5, 4] [] [8, 85] [8, 85] [1, 2, 5]\n" ] } ], "source": [ "print(li[:-2], li[-1:-3], li[4:], li[len(li) - 2:], li[:-3])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[8, 85] [4, 8, 85] [4, 8]\n" ] } ], "source": [ "print(li[-2: ], li[-3: ], li[-3: -1])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[85, 8, 4]\n" ] } ], "source": [ "print(li[ : 2: -1])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[85, 8, 4, 5, 2, 1]\n" ] } ], "source": [ "print(li[::-1])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 5, 8]\n" ] } ], "source": [ "print(li[0::2])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[85, 8, 4]\n" ] } ], "source": [ "print(li[:2:-1])" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 5, 4, 8, 85]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "li" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[85, 8, 4]\n" ] } ], "source": [ "print(li[:2:-1])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[85, 8, 4, 5, 2, 1]\n" ] } ], "source": [ "print(li[: :-1])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 5, 8]\n" ] } ], "source": [ "print(li[: :2])" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[85, 4, 2]\n" ] } ], "source": [ "print(li[: :-2])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 5, 4, 8, 85]\n" ] } ], "source": [ "print(li)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 5, 4, 8, 85, 'Python']\n" ] } ], "source": [ "print(li2)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "print(len(li2))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 85\n" ] } ], "source": [ "print(li2[0], li2[5])" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python 8\n" ] } ], "source": [ "print(li2[-1], li[-2])" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'PYTHON'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "li2[-1].upper()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Python'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "li2[-1].strip()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 5, 4, 8] [1, 2, 5, 4, 8]\n" ] } ], "source": [ "print(li2[0:5], li2[ :5])" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[8, 85, 'Python']\n" ] } ], "source": [ "print(li2[-3:])" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 5, 8, 'Python']\n" ] } ], "source": [ "print(li2[0: :2])" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python', 8, 5, 1]\n" ] } ], "source": [ "print(li2[: :-2])" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python', 85, 8, 4, 5, 2, 1]\n" ] } ], "source": [ "print(li2[::-1])" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 2, 3], [4, 5, 6], [6, 7, 8]]\n" ] } ], "source": [ "matrix = [[1,2,3],[4,5,6], [6,7,8]]\n", "\n", "print(matrix)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] } ], "source": [ "print(matrix[0])" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(matrix[0][0])" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(matrix[1][1])" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3] [2]\n" ] } ], "source": [ "print(matrix[0][1:], matrix[0][1:2])" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[6, 5, 4]\n" ] } ], "source": [ "print(matrix[1][::-1])" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 8\n" ] } ], "source": [ "print(len(matrix[2]), max(matrix[2]))" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2]\n" ] } ], "source": [ "print(matrix[0][1:2])" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "[4, 5, 6]\n", "[6, 7, 8]\n" ] } ], "source": [ "for ele in matrix:\n", " print(ele)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "5\n", "4\n", "8\n", "85\n" ] } ], "source": [ "for ele in li:\n", " print(ele)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "5\n", "7\n" ] } ], "source": [ "for i in range(3):\n", " for j in range(3):\n", " if j==1:\n", " print(matrix[i][j])" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "5\n", "7\n" ] } ], "source": [ "for ele in matrix:\n", " print(ele[1])" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 2, 3], [4, 5, 6], [6, 7, 8]] [6, 7, 8]\n" ] } ], "source": [ "print(matrix[:], matrix[:][2])" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3]\n", "[5, 6]\n", "[7, 8]\n", "2 3\n", "5 6\n", "7 8\n" ] } ], "source": [ "for ele in matrix:\n", " print(ele[1:])\n", " \n", "for ele in matrix:\n", " print(ele[1], ele[2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List Methods" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "empty = []" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 55, [1, 2, 3]]\n" ] } ], "source": [ "empty.append(5)\n", "empty.append(55)\n", "empty.append([1,2,3])\n", "\n", "print(empty)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter elements to add to list53\n", "Enter elements to add to listgdf\n", "Enter elements to add to listdgd\n", "Enter elements to add to list343\n", "Enter elements to add to list5\n" ] } ], "source": [ "for i in range(5):\n", " empty.append(input(\"Enter elements to add to list\"))" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 55, [1, 2, 3], '53', 'gdf', 'dgd', '343', '5']\n" ] } ], "source": [ "print(empty)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "ERROR:root:Internal Python error in the inspect module.\n", "Below is the traceback from this internal error.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"C:\\Users\\Jesus\\anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 3343, in run_code\n", " exec(code_obj, self.user_global_ns, self.user_ns)\n", " File \"\", line 3, in \n", " a.append(i.upper())\n", "KeyboardInterrupt\n", "\n", "During handling of the above exception, another exception occurred:\n", "\n", "Traceback (most recent call last):\n", " File \"C:\\Users\\Jesus\\anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 2044, in showtraceback\n", " stb = value._render_traceback_()\n", "AttributeError: 'KeyboardInterrupt' object has no attribute '_render_traceback_'\n", "\n", "During handling of the above exception, another exception occurred:\n", "\n", "Traceback (most recent call last):\n", " File \"C:\\Users\\Jesus\\anaconda3\\lib\\site-packages\\IPython\\core\\ultratb.py\", line 1169, in get_records\n", " return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)\n", " File \"C:\\Users\\Jesus\\anaconda3\\lib\\site-packages\\IPython\\core\\ultratb.py\", line 316, in wrapped\n", " return f(*args, **kwargs)\n", " File \"C:\\Users\\Jesus\\anaconda3\\lib\\site-packages\\IPython\\core\\ultratb.py\", line 350, in _fixed_getinnerframes\n", " records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))\n", " File \"C:\\Users\\Jesus\\anaconda3\\lib\\inspect.py\", line 1503, in getinnerframes\n", " frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)\n", " File \"C:\\Users\\Jesus\\anaconda3\\lib\\inspect.py\", line 1461, in getframeinfo\n", " filename = getsourcefile(frame) or getfile(frame)\n", " File \"C:\\Users\\Jesus\\anaconda3\\lib\\inspect.py\", line 708, in getsourcefile\n", " if getattr(getmodule(object, filename), '__loader__', None) is not None:\n", " File \"C:\\Users\\Jesus\\anaconda3\\lib\\inspect.py\", line 746, in getmodule\n", " f = module.__file__\n", "KeyboardInterrupt\n" ] }, { "ename": "TypeError", "evalue": "object of type 'NoneType' has no len()", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", " \u001b[1;31m[... skipping hidden 1 frame]\u001b[0m\n", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mKeyboardInterrupt\u001b[0m: ", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py\u001b[0m in \u001b[0;36mshowtraceback\u001b[1;34m(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)\u001b[0m\n\u001b[0;32m 2043\u001b[0m \u001b[1;31m# in the engines. This should return a list of strings.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2044\u001b[1;33m \u001b[0mstb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_render_traceback_\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2045\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mAttributeError\u001b[0m: 'KeyboardInterrupt' object has no attribute '_render_traceback_'", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", " \u001b[1;31m[... skipping hidden 1 frame]\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py\u001b[0m in \u001b[0;36mshowtraceback\u001b[1;34m(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)\u001b[0m\n\u001b[0;32m 2044\u001b[0m \u001b[0mstb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_render_traceback_\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2045\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2046\u001b[1;33m stb = self.InteractiveTB.structured_traceback(etype,\n\u001b[0m\u001b[0;32m 2047\u001b[0m value, tb, tb_offset=tb_offset)\n\u001b[0;32m 2048\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\IPython\\core\\ultratb.py\u001b[0m in \u001b[0;36mstructured_traceback\u001b[1;34m(self, etype, value, tb, tb_offset, number_of_lines_of_context)\u001b[0m\n\u001b[0;32m 1433\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1434\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1435\u001b[1;33m return FormattedTB.structured_traceback(\n\u001b[0m\u001b[0;32m 1436\u001b[0m self, etype, value, tb, tb_offset, number_of_lines_of_context)\n\u001b[0;32m 1437\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\IPython\\core\\ultratb.py\u001b[0m in \u001b[0;36mstructured_traceback\u001b[1;34m(self, etype, value, tb, tb_offset, number_of_lines_of_context)\u001b[0m\n\u001b[0;32m 1333\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mmode\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mverbose_modes\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1334\u001b[0m \u001b[1;31m# Verbose modes need a full traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1335\u001b[1;33m return VerboseTB.structured_traceback(\n\u001b[0m\u001b[0;32m 1336\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0metype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtb\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtb_offset\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnumber_of_lines_of_context\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1337\u001b[0m )\n", "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\IPython\\core\\ultratb.py\u001b[0m in \u001b[0;36mstructured_traceback\u001b[1;34m(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)\u001b[0m\n\u001b[0;32m 1190\u001b[0m \u001b[1;34m\"\"\"Return a nice text document describing the traceback.\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1191\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1192\u001b[1;33m formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,\n\u001b[0m\u001b[0;32m 1193\u001b[0m tb_offset)\n\u001b[0;32m 1194\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\IPython\\core\\ultratb.py\u001b[0m in \u001b[0;36mformat_exception_as_a_whole\u001b[1;34m(self, etype, evalue, etb, number_of_lines_of_context, tb_offset)\u001b[0m\n\u001b[0;32m 1148\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1149\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1150\u001b[1;33m \u001b[0mlast_unique\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrecursion_repeat\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfind_recursion\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0morig_etype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mevalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrecords\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1151\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1152\u001b[0m \u001b[0mframes\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat_records\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrecords\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlast_unique\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrecursion_repeat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\IPython\\core\\ultratb.py\u001b[0m in \u001b[0;36mfind_recursion\u001b[1;34m(etype, value, records)\u001b[0m\n\u001b[0;32m 449\u001b[0m \u001b[1;31m# first frame (from in to out) that looks different.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 450\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_recursion_error\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0metype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrecords\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 451\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrecords\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 452\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 453\u001b[0m \u001b[1;31m# Select filename, lineno, func_name to track frames with\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: object of type 'NoneType' has no len()" ] } ], "source": [ "a=['a']\n", "for i in a:\n", " a.append(i.upper())\n", " \n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "IOPub data rate exceeded.\n", "The notebook server will temporarily stop sending output\n", "to the client in order to avoid crashing it.\n", "To change this limit, set the config variable\n", "`--NotebookApp.iopub_data_rate_limit`.\n", "\n", "Current values:\n", "NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)\n", "NotebookApp.rate_limit_window=3.0 (secs)\n", "\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "li = [1,2,3,4]\n", "\n", "empty = [5, 55, [1, 2, 3], '53', 'gdf', 'dgd', '343', '5']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 55, [1, 2, 3], '53', 'gdf', 'dgd', '343', '5', 1, 2, 3, 4]\n" ] } ], "source": [ "empty.extend(li)\n", "\n", "print(empty)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 55, [1, 2, 3], '53', 'gdf', 'dgd', '343', '5', 1, 2, 3, 4, 1, 2, 3, 4]\n" ] } ], "source": [ "print(empty + li)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 55, [1, 2, 3], '53', 'gdf', 'dgd', '343', '5', 1, 2, 3, 4, 1, 2, 3, 4]\n" ] } ], "source": [ "emp2 = empty + li\n", "\n", "print(emp2)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] } ], "source": [ "print(emp2[2])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 55, [1, 2, 3, 4], '53', 'gdf', 'dgd', '343', '5', 1, 2, 3, 4, 1, 2, 3, 4]\n" ] } ], "source": [ "emp2[2] = li\n", "\n", "print(emp2)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 55, 1, 2, 3, 4, 'gdf', 'dgd', '343', '5', 1, 2, 3, 4, 1, 2, 3, 4]\n" ] } ], "source": [ "emp2[2:4] = li\n", "\n", "print(emp2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 10, 55, 1, 2, 3, 4, 'gdf', 'dgd', '343', '5', 1, 2, 3, 4, 1, 2, 3, 4]\n" ] } ], "source": [ "emp2.insert(1, 10)\n", "\n", "print(emp2)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 10, 55, 1, 2, 3, 4, 'gdf', 'dgd', '343', '5', 1, 2, 3, 4, 1, 2, 3]\n" ] } ], "source": [ "emp2.pop()\n", "\n", "print(emp2) # LIFO -> Last In First Out" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 10, 1, 2, 3, 4, 'gdf', 'dgd', '343', '5', 1, 2, 3, 4, 1, 2, 3]\n" ] } ], "source": [ "emp2.pop(2)\n", "\n", "print(emp2)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "pop index out of range", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\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[0memp2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0memp2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mIndexError\u001b[0m: pop index out of range" ] } ], "source": [ "emp2.pop(100)\n", "\n", "print(emp2)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "emp2.remove('gdf')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3, 4, 1, 2, 3]\n" ] } ], "source": [ "print(emp2)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "list.remove(x): x not in list", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\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[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0memp2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'gdf'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ], "source": [ "print(emp2.remove('gdf'))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "[5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3, 4, 1]\n" ] } ], "source": [ "print(emp2.pop())\n", "\n", "\n", "print(emp2)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3, 4, 1] [5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3, 4, 1]\n" ] } ], "source": [ "emp3 = emp2\n", "\n", "print(emp3, emp2)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1944735317312 1944735317312\n" ] } ], "source": [ "print(id(emp2), id(emp3))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "[5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3, 4] [5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3, 4]\n" ] } ], "source": [ "print(emp3.pop())\n", "\n", "print(emp2, emp3)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1944735317312 1944736306368\n", "[5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3, 4] [5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3, 4]\n" ] } ], "source": [ "emp3 = emp2.copy()\n", "\n", "\n", "print(id(emp2), id(emp3))\n", "print(emp2, emp3)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "[5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3, 4] [5, 10, 1, 2, 3, 4, 'dgd', '343', '5', 1, 2, 3]\n" ] } ], "source": [ "print(emp3.pop())\n", "\n", "print(emp2, emp3)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "print(emp2.count(1))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "print(emp2.index('dgd'))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "'dgda' is not in list", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\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[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0memp2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'dgda'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: 'dgda' is not in list" ] } ], "source": [ "print(emp2.index('dgda'))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 3, 2, 1, '5', '343', 'dgd', 4, 3, 2, 1, 10, 5]\n" ] } ], "source": [ "emp2.reverse()\n", "\n", "print(emp2)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'<' not supported between instances of 'str' and 'int'", "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[0memp3\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0memp3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: '<' not supported between instances of 'str' and 'int'" ] } ], "source": [ "emp3.sort()\n", "\n", "\n", "print(emp3)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3, 4, 5, 6, 9, 22, 66, 77, 345]\n" ] } ], "source": [ "li = [5,3,6,22,77,4,66,9,345]\n", "li.sort()\n", "\n", "print(li)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[345, 77, 66, 22, 9, 6, 5, 4, 3]\n" ] } ], "source": [ "li = [5,3,6,22,77,4,66,9,345]\n", "li.sort(reverse=True)\n", "\n", "print(li)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 2, 3], [4, 5, 6], [6, 7, 8]]\n" ] } ], "source": [ "matrix = [[1,20,3],[4,5,6], [6,75,8]]\n", "\n", "print(matrix)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[6, 75, 8], [35, 5, 6], [40, 20, 3]]\n" ] } ], "source": [ "matrix = [[40,20,3],[35,5,6],[6,75,8]]\n", "\n", "\n", "matrix.sort()\n", "\n", "\n", "print(matrix)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[35, 5, 6], [40, 20, 3], [6, 75, 8]]\n" ] } ], "source": [ "matrix = [[40,20,3],[35,5,6], [6,75,8]]\n", "\n", "\n", "matrix.sort(key = lambda x :x[1])\n", "\n", "\n", "print(matrix)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[6, 75, 8], [35, 5, 6], [40, 20, 3]]\n", "[[35, 5, 6], [40, 20, 3], [6, 75, 8]]\n" ] } ], "source": [ "print(sorted(matrix))\n", "\n", "\n", "print(matrix)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[6, 75, 8], [40, 20, 3], [35, 5, 6]]\n" ] } ], "source": [ "print(list(reversed(matrix)))" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "emp3.clear()\n", "\n", "\n", "print(emp3)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'emp3' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0memp3\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0memp3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'emp3' is not defined" ] } ], "source": [ "del emp3\n", "\n", "print(emp3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuples\n", "\n", "\n", "Storing the Group of non-homogenous group of data\n", "\n", "- Is is created using `()`\n", "- It is immutable \n", "- it is iterable\n", "- it is ordered" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "t1 = ()\n", "t2 = tuple()" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "t1 = (1,2,3,4, 'Python', (1,2,3), [1,2,3])" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 [1, 2, 3] 3 (1, 2, 3)\n" ] } ], "source": [ "print(t1[0], t1[-1], t1[2], t1[-2])" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2)\n" ] } ], "source": [ "print(t1[0:2])" ] }, { "cell_type": "code", "execution_count": 48, "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[0mt1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m55\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": [ "t1[0] = 55" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "t1 = (1,2,3,4, 'Python', (1,2,3), [1,2,3])\n", "\n", "t1[-1].append(5)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 4, 'Python', (1, 2, 3), [1, 2, 3, 5])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "Python\n", "(1, 2, 3)\n", "[1, 2, 3]\n" ] } ], "source": [ "for ele in t1:\n", " print(ele)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "print(len(t1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuple Methods" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(t1.count(1))" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "\n", "print(t1.index('Python'))" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yo\n", "yyyyy\n" ] } ], "source": [ "print('y' + 'o')\n", "\n", "print('y' * 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tasks\n", "\n", "s = 'Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 19 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 19 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective'" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "s = '''Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 19 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 19 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective'''" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python', 'is', 'an', 'interpreted', 'high-level', 'general-purpose', 'programming', 'language.', \"Python's\", 'design', 'philosophy', 'emphasizes', 'code', 'readability', 'with', 'its', 'notable', 'use', 'of', 'significant', 'indentation.', 'Wikipedia', 'Developer:', 'Python', 'Software', 'Foundation', 'Stable', 'release:', '3.9.5', '/', '3', 'May', '2021;', '19', 'days', 'ago', 'Preview', 'release:', '3.10.0b1', '/', '3', 'May', '2021;', '19', 'days', 'ago', 'Typing', 'discipline:', 'Duck,', 'dynamic,', 'strong', 'typing;', 'gradual', '(since', '3.5,', 'but', 'ignored', 'in', 'CPython)', 'First', 'appeared:', 'February', '1991;', '30', 'years', 'ago', 'Paradigm:', 'Multi-paradigm:', 'object-oriented,', 'procedural', '(imperative),', 'functional,', 'structured,', 'reflective']\n" ] } ], "source": [ "print(s.split())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tasks\n", "\n", "1. Reverse every element in list and update them in another list\n", "- extract lower-case alpha from each element and update in another list\n", "- remove special characters from the element and update in another list\n", "- get the middle character of every element and update in another list" ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }