See More

{ "cells": [ { "cell_type": "markdown", "id": "5a9e7395", "metadata": {}, "source": [ "### data sturctre" ] }, { "cell_type": "markdown", "id": "97d22b9e", "metadata": {}, "source": [ "## LIST" ] }, { "cell_type": "code", "execution_count": null, "id": "42597811", "metadata": {}, "outputs": [], "source": [ "#### squar bracket\n", "#### we can retrieve elemts using index and index strats from 0" ] }, { "cell_type": "code", "execution_count": 1, "id": "00137f6a", "metadata": { "scrolled": true }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax. Perhaps you forgot a comma? (167970467.py, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn[1], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m sample_list[1,2,3,5,9,4,5,6,7\"hi\"]\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax. Perhaps you forgot a comma?\n" ] } ], "source": [ "sample_list[1,2,3,5,9,4,5,6,7\"hi\"]" ] }, { "cell_type": "code", "execution_count": 2, "id": "dc3e2df2", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax. Perhaps you forgot a comma? (1880050870.py, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn[2], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m sample_list=[1,2,3,5,9,4,5,6,7\"hi\"]\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax. Perhaps you forgot a comma?\n" ] } ], "source": [ "sample_list=[1,2,3,5,9,4,5,6,7\"hi\"]" ] }, { "cell_type": "code", "execution_count": 3, "id": "84e5eb65", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'sample_list' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43msample_list\u001b[49m[\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m9\u001b[39m,\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m6\u001b[39m,\u001b[38;5;241m7\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhi\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n", "\u001b[1;31mNameError\u001b[0m: name 'sample_list' is not defined" ] } ], "source": [ "sample_list[1,2,3,5,9,4,5,6,7,\"hi\"]" ] }, { "cell_type": "code", "execution_count": 6, "id": "d95995cc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 5, 9, 4, 5, 6, 7, 'hi']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list=[1,2,3,5,9,4,5,6,7,\"hi\"]\n", "sample_list" ] }, { "cell_type": "code", "execution_count": 7, "id": "c2dc3a0d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(sample_list)" ] }, { "cell_type": "code", "execution_count": 8, "id": "4bd4037b", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'list' object is not callable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[8], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43msample_list\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[1;31mTypeError\u001b[0m: 'list' object is not callable" ] } ], "source": [ "sample_list(0)" ] }, { "cell_type": "code", "execution_count": 9, "id": "bcfe968c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list[0]" ] }, { "cell_type": "code", "execution_count": 10, "id": "4929921c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list[8]" ] }, { "cell_type": "code", "execution_count": 11, "id": "a237e219", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list[8]" ] }, { "cell_type": "code", "execution_count": 12, "id": "7c377725", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hi'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list[9]" ] }, { "cell_type": "code", "execution_count": 13, "id": "a7d49605", "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)", "Cell \u001b[1;32mIn[13], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43msample_list\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m]\u001b[49m\n", "\u001b[1;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "sample_list[10]" ] }, { "cell_type": "code", "execution_count": null, "id": "980268a0", "metadata": {}, "outputs": [], "source": [ "#### we can retrieve elemts using index and index strats from 0" ] }, { "cell_type": "code", "execution_count": null, "id": "191a2acf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 15, "id": "20db71d5", "metadata": {}, "outputs": [], "source": [ "sample_list[0]=25\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "5ac6c58c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[25, 2, 3, 5, 9, 4, 5, 6, 7, 'hi']" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_list[0]=25\n", "sample_list" ] }, { "cell_type": "code", "execution_count": null, "id": "a01be825", "metadata": {}, "outputs": [], "source": [ "#### Mutable-> we can meke changes" ] }, { "cell_type": "markdown", "id": "83807efb", "metadata": {}, "source": [ "## Tuple" ] }, { "cell_type": "code", "execution_count": 18, "id": "2f52aaf3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 5, 9, 4, 5, 6, 7, 'hi')" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_tuple=(1,2,3,5,9,4,5,6,7,\"hi\")\n", "sample_tuple" ] }, { "cell_type": "code", "execution_count": 19, "id": "6dbce9ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(sample_tuple)" ] }, { "cell_type": "code", "execution_count": 20, "id": "4be3388b", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object is not callable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[20], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43msample_tuple\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[1;31mTypeError\u001b[0m: 'tuple' object is not callable" ] } ], "source": [ "sample_tuple(0)" ] }, { "cell_type": "code", "execution_count": 21, "id": "513e7717", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_tuple[0]" ] }, { "cell_type": "code", "execution_count": 22, "id": "93cb6764", "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)", "Cell \u001b[1;32mIn[22], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m sample_tuple[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m25\u001b[39m\n", "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "sample_tuple[0]=25" ] }, { "cell_type": "markdown", "id": "c743ba80", "metadata": {}, "source": [ "#### tuple elements are Immutable->we cannot make the change\n", "#### elements are placed within the paranthesis \n", "#### in LIST and TUPLE duplicates are printed" ] }, { "cell_type": "code", "execution_count": null, "id": "4322f98a", "metadata": {}, "outputs": [], "source": [ "###### differece between list and tuple important interview question" ] }, { "cell_type": "markdown", "id": "ed5a4921", "metadata": {}, "source": [ "## SET" ] }, { "cell_type": "code", "execution_count": 26, "id": "87c90e29", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 12, 2, 3, 32, 44, 52, 63, 75, 'hi'}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_set={1,2,3,52,44,63,32,12,'hi',75,3,44}\n", "sample_set" ] }, { "cell_type": "code", "execution_count": null, "id": "52aa0ea2", "metadata": {}, "outputs": [], "source": [ "#### {}\n", "#### Duplicate not allowed\n", "#### arranged according to first place\n", "#### set is mutable\n" ] }, { "cell_type": "code", "execution_count": 27, "id": "7731fa22", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 12, 2, 3, 32, 44, 52, 63, 75, 'hi'}" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_set={1,2,3,52,44,63,32,12,'hi',75,3,44}\n", "sample_set" ] }, { "cell_type": "code", "execution_count": 28, "id": "5d4493a3", "metadata": { "scrolled": true }, "outputs": [ { "ename": "TypeError", "evalue": "'set' object is not subscriptable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[28], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43msample_set\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n", "\u001b[1;31mTypeError\u001b[0m: 'set' object is not subscriptable" ] } ], "source": [ "sample_set[0]" ] }, { "cell_type": "code", "execution_count": 31, "id": "b0f9614d", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'set' 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)", "Cell \u001b[1;32mIn[31], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m sample_set[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m200\u001b[39m\n", "\u001b[1;31mTypeError\u001b[0m: 'set' object does not support item assignment" ] } ], "source": [ "sample_set[0]=200" ] }, { "cell_type": "code", "execution_count": 32, "id": "36edba85", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 100, 12, 2, 3, 32, 44, 52, 63, 75, 'hi'}" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_set.add(100)\n", "sample_set" ] }, { "cell_type": "code", "execution_count": 30, "id": "465b300f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 100, 12, 2, 3, 32, 44, 52, 63, 75, 'hi'}" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_set" ] }, { "cell_type": "code", "execution_count": 33, "id": "4f5b04bb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 12, 2, 3, 32, 44, 52, 63, 75, 'hi'}" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_set.remove(100)\n", "sample_set" ] }, { "cell_type": "code", "execution_count": 34, "id": "9e9a7843", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'hi' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[34], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m sample_set\u001b[38;5;241m.\u001b[39mremove(\u001b[43mhi\u001b[49m)\n\u001b[0;32m 2\u001b[0m sample_set\n", "\u001b[1;31mNameError\u001b[0m: name 'hi' is not defined" ] } ], "source": [ "sample_set.remove(hi)\n", "sample_set" ] }, { "cell_type": "code", "execution_count": 35, "id": "b6962af6", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 12, 32, 44, 52, 63, 75}" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_set.remove(\"hi\")\n", "sample_set" ] }, { "cell_type": "markdown", "id": "3a3ff728", "metadata": {}, "source": [ "####### set is mutable\n", "### duplicates not print" ] }, { "cell_type": "code", "execution_count": null, "id": "da24369a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "89b84dc5", "metadata": {}, "source": [ "## DICTIONARY " ] }, { "cell_type": "code", "execution_count": null, "id": "ce09b6a0", "metadata": {}, "outputs": [], "source": [ "#### dictionary contain key along with value pair data structre\n", "#### key is alway unique: should not be repeated value of key if it repeated it take only last key value\n", "#### values can be duplicated\n", "#### value can retrived using key \n", "#### key cannot be retrived using value\n", "#### it is mutable " ] }, { "cell_type": "code", "execution_count": 1, "id": "6b474f67", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'apple', 2: 'hi', 3: 'data', 6: 35.5, 7: 35.5}" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_dict={1:'apple', 2:35, 3:'data', 2:'every', 6:35.5, 7:35.5, 2:'hi'}\n", "sample_dict" ] }, { "cell_type": "code", "execution_count": 2, "id": "b1d6a782", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'data'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_dict[3]" ] }, { "cell_type": "code", "execution_count": 3, "id": "c534ce9d", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'sampel_dict' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43msampel_dict\u001b[49m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mapple\u001b[39m\u001b[38;5;124m'\u001b[39m]\n", "\u001b[1;31mNameError\u001b[0m: name 'sampel_dict' is not defined" ] } ], "source": [ "sampel_dict['apple']" ] }, { "cell_type": "code", "execution_count": 5, "id": "67370773", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'apple', 2: 'hi', 3: 'data', 6: 200, 7: 35.5}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_dict[6]=200\n", "sample_dict" ] }, { "cell_type": "code", "execution_count": 6, "id": "bc48f3de", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'apple', 2: 'hi', 3: 'data', 6: 200, 7: 35.5, 10: 'pawan'}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_dict[10]='pawan'\n", "sample_dict" ] }, { "cell_type": "code", "execution_count": null, "id": "ad072cf5", "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }