[go: up one dir, main page]

0% found this document useful (0 votes)
11 views28 pages

Overview of Data Structures

The document discusses data structures, specifically lists in Python, highlighting their characteristics such as being ordered, mutable, and capable of storing heterogeneous data types. It provides examples of creating lists, accessing elements, and manipulating them through methods like append and remove. Additionally, it demonstrates the use of nested lists and addresses potential errors like index out of range.

Uploaded by

shyamjha0324
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views28 pages

Overview of Data Structures

The document discusses data structures, specifically lists in Python, highlighting their characteristics such as being ordered, mutable, and capable of storing heterogeneous data types. It provides examples of creating lists, accessing elements, and manipulating them through methods like append and remove. Additionally, it demonstrates the use of nested lists and addresses potential errors like index out of range.

Uploaded by

shyamjha0324
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 28

{

"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "fbc93ad8-6ccd-4d87-9c66-3f04e2861dac",
"metadata": {},
"outputs": [],
"source": [
"#data structure>way of organizing and storing data so that it can be acccessed
and manipulated effeciently"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2fe47ef1-7a82-45c2-a470-f8a96cd4e1da",
"metadata": {},
"outputs": [],
"source": [
"#data structure>> string, list, set, tuples, dictionary, array"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6b181695-cb5a-4620-b9f2-c99a9f303f1d",
"metadata": {},
"outputs": [],
"source": [
"#list>> you can store anything in list>>can store heterogenous data\n",
"#list is an ordered collection of elements that can be of any data type\n",
"#lists are mutable"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9bcc9df6-af56-4115-a4f4-eea62de13fcd",
"metadata": {},
"outputs": [],
"source": [
"num = [1, 2, 3, 4, 5]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "cbad7b6e-47fd-42ce-bb96-f981ad204640",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d67d2a23-16f4-4070-bdfc-0c928c7d3a57",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(num)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "d49ca9aa-28aa-4c1d-b173-5d0dc98ba779",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num[0]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f341df24-5a81-4776-a98b-ce6e847d4024",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num[1]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "58079a10-4919-4a8b-b3cc-79f51539c9fb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num[-1]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f52dca6e-9f2b-4118-8d68-9f61e6d034c6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num[4]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "9e9fd579-a277-4bf6-851c-be8e173a3d2d",
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback
(most recent call last)",
"Cell \u001b[0;32mIn[11], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \
u001b[43mnum\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\
u001b[43m]\u001b[49m \u001b[38;5;66;03m#an error list index out of range\
u001b[39;00m\n",
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"num[5] #an error list index out of range"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "195ca55e-534b-466b-9b29-e5474cccaa94",
"metadata": {},
"outputs": [],
"source": [
"a = [1, 2, 3.3, \"baingan\", \"tomato\", \"banana\", True, False, 3+7j]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "10e0c578-befd-49ea-9a48-ba5a37ec2e84",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 5, 8, 10, 10, 15, 36, 91]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a #list can store any data type>> heterogenous data structure\n",
"b = [10,10,15,36,5,4,8,91]\n",
"b.sort()\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "deff5e00-9a95-4691-94cf-41fd38babc0b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "0fc34ed2-82a8-40ff-9fa3-0c05128d7297",
"metadata": {},
"outputs": [],
"source": [
"#adding element to the list"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "9ceb3ad8-d32f-4640-a0df-2e56afd0eb8e",
"metadata": {},
"outputs": [],
"source": [
"a.append(\"orange\")"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "7399497f-1904-4d0b-87a8-a946be8c9142",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3.3, 'baingan', 'tomato']"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[2:5]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "30bd63c9-2f35-428b-aad6-b4cc4008bd99",
"metadata": {},
"outputs": [],
"source": [
"a.append(\"pwskills\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "e300b6fe-c833-4964-808f-13965b412eb8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1,\n",
" 2,\n",
" 3.3,\n",
" 'baingan',\n",
" 'tomato',\n",
" 'banana',\n",
" True,\n",
" False,\n",
" (3+7j),\n",
" 'orange',\n",
" 'pwskills']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "3020fcc1-7465-414e-80c0-ed847b6cf453",
"metadata": {},
"outputs": [],
"source": [
"a.remove(\"pwskills\")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "adb1a9a4-2ffb-42f8-a2cb-b82180e221b8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3.3, 'baingan', 'tomato', 'banana', True, False, (3+7j), 'orange']"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "452ed51d-026e-4fa5-bf7a-889fbf577f79",
"metadata": {},
"outputs": [],
"source": [
"a.remove('baingan')"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "83622366-311c-4200-be91-f9322c137ea3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3.3, 'tomato', 'banana', True, False, (3+7j), 'orange']"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1f042964-ac05-4514-bc1d-2a2c7153a8aa",
"metadata": {},
"outputs": [],
"source": [
"lis = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "faa47362-0a9e-4a78-a43d-78e638fb2d8d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lis"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "23c74396-8c93-4732-b19c-b7179cc3e261",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(lis)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "6602ba67-2ca7-4979-af4d-f4f5ec9d1ebd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lis[0][1]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "f8f90f6f-15fc-498b-9774-79d0748b81be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lis[1][1]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "bef63cf9-4cae-402b-b548-13bb53060e8c",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'lis' is not defined",
"output_type": "error",
"traceback": [
"\
u001b[31m--------------------------------------------------------------------------
-\u001b[39m",
"\u001b[31mNameError\u001b[39m Traceback
(most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\
u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \
u001b[43mlis\u001b[49m[\u001b[32m2\u001b[39m][\u001b[32m2\u001b[39m]\n",
"\u001b[31mNameError\u001b[39m: name 'lis' is not defined"
]
}
],
"source": [
"lis[2][2]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "3fa1add6-ea53-4deb-832d-0fd3b36d28d7",
"metadata": {},
"outputs": [],
"source": [
"#list is mutable\n",
"shopping_list = [\"Baingan\", \"Apple\", 2, 3.4, True]\n",
"shopping_list[0] = \"Potato\""
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "2ed1a380-e7e2-41ee-9b8c-a5537aa89bac",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Potato', 'Apple', 2, 3.4, True]"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"shopping_list"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "7985545a-af03-4dc5-aafe-9e2bb658ef11",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Potato Apple 2 3.4 True "
]
}
],
"source": [
"for item in shopping_list:\n",
" print(item, end = \" \")\n",
" \n",
"#use of list >> can store any data and it is mutable"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "0d2a9852-00f2-4560-8a95-11cbe485d90a",
"metadata": {},
"outputs": [],
"source": [
"##Tuples >> Immutable\n",
"point = (2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "50f4a3ed-d57f-4a1f-8a2e-c8025b755cfe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 3)"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"point"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "086b512d-121d-479c-b293-79a99955eba5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(point)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "135c1f4f-f7f5-4cf6-954f-4bf8b9071869",
"metadata": {},
"outputs": [],
"source": [
"#use case>>adhar card, atm, empid>> you dont want to change, you will use
tuple to store the data"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "bf6855c9-033f-417c-979c-9aef4824f549",
"metadata": {},
"outputs": [],
"source": [
"t = (1, 2, 2.3, True, 3+5j, \"Ajay\") #tuple can store heterogenous data"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "5929a9e0-83a3-44a1-80f4-c1a38051366c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(t)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "00123923-cac3-4b55-b4ff-171782c5cecb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 2, 2.3, True, (3+5j), 'Ajay')"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "68c9eea5-add1-45d4-87fa-7ff4fb9d3929",
"metadata": {},
"outputs": [],
"source": [
"emp_name = (\"Ajay\", \"Ajay1\", \"Bijay\", \"Sanjay\")"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "40e52e6e-1533-427a-a046-dd8dd7134415",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(emp_name)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "135d9e27-1e24-498d-be3f-ddfccede2ed6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Ajay'"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"emp_name[0]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "bc15f438-3cd0-4c0f-9d39-82f9a7356024",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Ajay1'"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"emp_name[1]"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "1ae3724e-efb3-42c7-b803-22dc7276527d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Sanjay'"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"emp_name[-1]"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "5b50b09f-f757-48ba-bd75-3a15241efaa4",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback
(most recent call last)",
"Cell \u001b[0;32mIn[54], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \
u001b[43memp_name\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\
u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\
u001b[38;5;124mRahul\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item
assignment"
]
}
],
"source": [
"emp_name[2] = \"Rahul\" #tuples are immutable"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "e5e84096-0e35-48ef-80a7-6a975cff89e5",
"metadata": {},
"outputs": [],
"source": [
"box1 = (\"a\", \"b\")\n",
"box2 = (\"c\", \"d\")\n",
"choc_bag = (box1, box2)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "fbd79650-d697-48e9-9292-135fb0c7e306",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(choc_bag)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "1267c071-dcb7-4e6f-9ac6-18ff842c6a99",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(('a', 'b'), ('c', 'd'))"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"choc_bag"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "3fc6c36d-64e3-4121-b900-21897f112d49",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n",
"b\n",
"c\n",
"d\n"
]
}
],
"source": [
"for box in choc_bag:\n",
" for choc in box:\n",
" print(choc)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "3d84fb15-e22a-431d-aeca-273aba933fc6",
"metadata": {},
"outputs": [],
"source": [
"#sets >> are unordered collection of unique element"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "d6dcf003-f39a-4bdc-aad6-b2a416245610",
"metadata": {},
"outputs": [],
"source": [
"s = {\"red\", \"orange\"}"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "f1cd08e7-b1af-4c4a-afcd-bc98a392c14e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"set"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(s)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "ead0ba67-4d21-429b-8c53-817af1bfe0d9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'blue', 'orange', 'red'}"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = {\"red\", \"orange\", \"red\", \"blue\", \"orange\"}\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "9ca30488-da57-4228-aecd-38cd8592a70e",
"metadata": {},
"outputs": [],
"source": [
"s = {\"a\", \"a\", \"a\", 1, 1.1, 1.1, 1.1, 2, 2, 2}"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "f10af92a-defc-4eaf-9ec3-28c97caf092d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 1.1, 2, 'a'}"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "7f73ebc5-c6f3-4be8-879f-1943c2262812",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'set' object is not subscriptable",
"output_type": "error",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback
(most recent call last)",
"Cell \u001b[0;32mIn[66], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \
u001b[43ms\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\
u001b[43m]\u001b[49m \u001b[38;5;66;03m#error\u001b[39;00m\n",
"\u001b[0;31mTypeError\u001b[0m: 'set' object is not subscriptable"
]
}
],
"source": [
"s[0] #error>> no concept of indexing as sets are unordered and unique"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "9bdb5a2d-9fd7-47c6-819a-ca81b764f44e",
"metadata": {},
"outputs": [],
"source": [
"s.add(\"pwskills\") #mutable sets"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "aec2417e-7592-4747-ae0d-184903323bb7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 1.1, 2, 'a', 'pwskills'}"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "dce5c1b8-be6b-4e8b-872f-54a8ec7c2b23",
"metadata": {},
"outputs": [],
"source": [
"s.remove(\"pwskills\")"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "608240ad-a202-405e-9eb0-6e4d56157d62",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 1.1, 2, 'a'}"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "b34b2349-f3f1-49d9-bd1d-439ba2cddd75",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'apple', 'banana', 'egg'}"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#use case of sets>> to get unique elements\n",
"grocery_list = {\"apple\", \"banana\", \"banana\", \"egg\", \"egg\"}\n",
"grocery_list"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "1465c502-fc74-426d-9061-078f209507cd",
"metadata": {},
"outputs": [],
"source": [
"#dictionaries>>which stores the data in the form of key value pair\n",
"my_dict = {}"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "c4dfe068-e929-4a34-a061-e35c5f85dd9b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(my_dict)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "d9f7b8b5-1d3f-4c17-a524-7d980ad76eb3",
"metadata": {},
"outputs": [],
"source": [
"my_dict = {\"key_course\" : \"data science\", \"duration\":200}"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "47b1bd89-e5bd-4840-bb36-5d047423b8b3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(my_dict)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "7cd17f20-8e36-44c9-ab79-73bbb4664b3f",
"metadata": {},
"outputs": [],
"source": [
"#use cases\n",
"phonebook = {\"Dad\": 1234, \"Mom\":456}"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "63df313b-4098-4bb6-8b2d-9fc196a1fd28",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Dad': 1234, 'Mom': 456}"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phonebook"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "057f3dc4-f9d7-488e-ae46-bb0ea61a4fab",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(phonebook)"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "1fcbb5e2-31b1-4fcb-af5f-28111484bcef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1234"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phonebook[\"Dad\"]"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "832742fb-06cf-4010-b580-a2dd42deaac6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"456"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phonebook[\"Mom\"]"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "bd3a583f-1f50-4fe9-a204-446099fb4164",
"metadata": {},
"outputs": [],
"source": [
"phonebook = {\"Dad\": 1234, \"Mom\":456, \"Dad\": 780}"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "6ac5fc9e-c8c3-4dff-b032-0371f3563d54",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"780"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phonebook[\"Dad\"]"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "b64f4a89-f45a-4c6d-a2c4-2a5f41c6ac29",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'paneer_kofta': 480, 'chicken_handi': 590}"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rest_menu = {\"paneer_kofta\": 480, \"chicken_handi\": 590}\n",
"rest_menu"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "1cdae285-2688-4440-a9bb-52c2ed7e5275",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"480"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rest_menu['paneer_kofta']"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "630c9593-c551-4467-9236-fa4599cd5c79",
"metadata": {},
"outputs": [],
"source": [
"rest_menu['paneer_kofta'] = 790"
]
},
{
"cell_type": "code",
"execution_count": 90,
"id": "9631c00d-662f-4ac6-a6b0-44f1452e675f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'paneer_kofta': 790, 'chicken_handi': 590}"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rest_menu"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "c7317841-a435-46a9-8c36-a5c719bf31f2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Dad': 780, 'Mom': 456}"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phonebook"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "5c94feba-1bfc-4144-a88c-2e77da5419d4",
"metadata": {},
"outputs": [],
"source": [
"phonebook[\"Dad\"] = \"I have a taken a new no again, I dont want to tell
you\""
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "e63d8d73-c036-43d0-8a7d-5d0386bed187",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Dad': 'I have a taken a new no again, I dont want to tell you', 'Mom':
456}"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phonebook"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "077192a0-e4a0-4aa8-a5e0-eb2b27946eb5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'paneer_kofta': 790, 'chicken_handi': 590}"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rest_menu"
]
},
{
"cell_type": "code",
"execution_count": 95,
"id": "72de994b-bb24-4625-a21b-23ba0a0d442c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['paneer_kofta', 'chicken_handi'])"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rest_menu.keys()"
]
},
{
"cell_type": "code",
"execution_count": 96,
"id": "1163f289-ff1e-4cf5-b219-41335011f18d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['Dad', 'Mom'])"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phonebook.keys()"
]
},
{
"cell_type": "code",
"execution_count": 97,
"id": "e45c2e0d-d3de-4f83-8093-74ac6f740024",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([790, 590])"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rest_menu.values()"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "025ed81f-bf7b-4f1c-b30f-bb1683455014",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values(['I have a taken a new no again, I dont want to tell you',
456])"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phonebook.values()"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "d0a361d6-dfad-4dec-a6e1-a4fa25dea205",
"metadata": {},
"outputs": [],
"source": [
"from array import array "
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "7f2e5b3b-ab3d-4812-95de-55402b641352",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array('i', [1, 2, 3, 4])"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#array is similar to list>> homogenous data\n",
"array('i', [1, 2, 3, 4])"
]
},
{
"cell_type": "code",
"execution_count": 101,
"id": "6cbc8b51-03ff-437c-9c28-1fca795a03fa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#string\n",
"s = 'Ajay'\n",
"type(s)"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "56045906-f747-434d-af87-503eb7fb8a71",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A\n",
"j\n",
"a\n",
"y\n"
]
}
],
"source": [
"for i in s:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4527e2bc-622b-4710-95a0-7e1edc769478",
"metadata": {},
"outputs": [],
"source": [
"a"
]
}
],
"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.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like