{"id":64,"date":"2025-10-21T04:23:59","date_gmt":"2025-10-21T04:23:59","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=64"},"modified":"2025-12-17T07:47:15","modified_gmt":"2025-12-17T07:47:15","slug":"lesson-10-dictionaries-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=64","title":{"rendered":"Lesson 10: Dictionaries in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p>To understand how to create, access, and manipulate <strong>dictionaries<\/strong> \u2014 one of Python\u2019s most powerful data structures for storing key-value pairs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Is a Dictionary?<\/strong><\/h2>\n\n\n\n<p>A <strong>dictionary<\/strong> is a <strong>collection of key-value pairs<\/strong> enclosed in <strong>curly braces <code>{}<\/code><\/strong>.<br>Each <strong>key<\/strong> is unique, and it is used to access its corresponding <strong>value<\/strong>.<\/p>\n\n\n\n<p>\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>student = {\n    \"name\": \"Sameer\",\n    \"age\": 25,\n    \"city\": \"Warangal\"\n}\n\nprint(student)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'name': 'Sameer', 'age': 25, 'city': 'Warangal'}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Accessing Dictionary Elements<\/strong><\/h2>\n\n\n\n<p>You can access values using their <strong>keys<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(student&#91;\"name\"])      # Using key directly\nprint(student.get(\"age\"))   # Using get() method\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\n25\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>3. Adding and Updating Items<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>Add a New Key-Value Pair<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>student&#91;\"course\"] = \"Python\"\nprint(student)\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Update Existing Value<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>student&#91;\"age\"] = 26\nprint(student)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'name': 'Sameer', 'age': 26, 'city': 'Warangal', 'course': 'Python'}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u274c <strong>4. Removing Items<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Method<\/th><th>Description<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>pop(key)<\/code><\/td><td>Removes key and returns value<\/td><td><code>student.pop(\"city\")<\/code><\/td><\/tr><tr><td><code>del<\/code><\/td><td>Deletes key-value pair<\/td><td><code>del student[\"age\"]<\/code><\/td><\/tr><tr><td><code>clear()<\/code><\/td><td>Removes all items<\/td><td><code>student.clear()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>student.pop(\"city\")\nprint(student)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'name': 'Sameer', 'age': 25}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd01 <strong>5. Looping Through a Dictionary<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>Loop Through Keys:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for key in student:\n    print(key)\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Loop Through Values:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for value in student.values():\n    print(value)\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Loop Through Both:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for key, value in student.items():\n    print(key, \":\", value)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name : Sameer\nage : 25\ncity : Warangal\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddee <strong>6. Checking for Keys<\/strong><\/h2>\n\n\n\n<p>You can verify if a key exists using the <code>in<\/code> operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if \"name\" in student:\n    print(\"Name exists!\")\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name exists!\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udce6 <strong>7. Dictionary Methods<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Method<\/th><th>Description<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>keys()<\/code><\/td><td>Returns all keys<\/td><td><code>student.keys()<\/code><\/td><\/tr><tr><td><code>values()<\/code><\/td><td>Returns all values<\/td><td><code>student.values()<\/code><\/td><\/tr><tr><td><code>items()<\/code><\/td><td>Returns key-value pairs<\/td><td><code>student.items()<\/code><\/td><\/tr><tr><td><code>copy()<\/code><\/td><td>Returns a shallow copy<\/td><td><code>student.copy()<\/code><\/td><\/tr><tr><td><code>update()<\/code><\/td><td>Adds or updates multiple items<\/td><td><code>student.update({\"country\": \"India\"})<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>student.update({\"country\": \"India\", \"language\": \"English\"})\nprint(student)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'name': 'Sameer', 'age': 25, 'city': 'Warangal', 'country': 'India', 'language': 'English'}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd17 <strong>8. Nested Dictionaries<\/strong><\/h2>\n\n\n\n<p>You can store dictionaries <strong>inside<\/strong> other dictionaries.<\/p>\n\n\n\n<p>\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>students = {\n    \"s1\": {\"name\": \"Ali\", \"age\": 22},\n    \"s2\": {\"name\": \"Sara\", \"age\": 21},\n    \"s3\": {\"name\": \"John\", \"age\": 23}\n}\n\nprint(students&#91;\"s2\"]&#91;\"name\"])\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sara\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>9. Dictionary Comprehension<\/strong><\/h2>\n\n\n\n<p>Create a dictionary in one line using comprehension.<\/p>\n\n\n\n<p>\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>squares = {x: x*x for x in range(5)}\nprint(squares)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>10. Real-Life Example \u2013 Contact Book<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>contacts = {\n    \"Ali\": \"9876543210\",\n    \"Sara\": \"9123456789\",\n    \"John\": \"9988776655\"\n}\n\nname = input(\"Enter name: \")\nif name in contacts:\n    print(f\"{name}'s number is {contacts&#91;name]}\")\nelse:\n    print(\"Contact not found.\")\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter name: Sara\nSara's number is 9123456789\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand how to create, access, and manipulate dictionaries \u2014 one of Python\u2019s most powerful data structures for storing key-value pairs. \ud83e\udde9 1. What Is a Dictionary? A dictionary is a collection of key-value pairs enclosed in curly braces {}.Each key is unique, and it is used to access its corresponding value. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}