{"id":654,"date":"2021-02-26T13:21:26","date_gmt":"2021-02-26T07:51:26","guid":{"rendered":"https:\/\/cbsepython.in\/?p=654"},"modified":"2024-03-15T19:43:49","modified_gmt":"2024-03-15T14:13:49","slug":"dictionary-in-python-class-11-notes","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/dictionary-in-python-class-11-notes\/","title":{"rendered":"Dictionary in Python Class 11 Notes"},"content":{"rendered":"<h2><span style=\"color: #000000;\">Dictionary in Python Class 11 Notes<\/span><\/h2>\n<p><span style=\"color: #000000;\">In this section students of class 11 and 12 having computer science or I.P. will learn about the dictionary in python programming.\u00a0 These notes are prepared as per the CBSE Syllabus and covers all the topics such as &#8211; accessing items in a dictionary using keys, mutability of a dictionary (adding a new term, modifying an existing item), traversing a dictionary, built-in functions\/methods \u2013 len(), dict(), keys(), values(), items(), get(), update(), del(), del, clear(),<\/span><br \/>\n<span style=\"color: #000000;\">fromkeys(), copy(), pop(), popitem(), setdefault(), max(), min(), sorted().<\/span><\/p>\n<h3><span style=\"color: #000000;\">What is Dictionary in Python?<\/span><\/h3>\n<p><span style=\"color: #000000;\">Python&#8217;s dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.<\/span><\/p>\n<p><span style=\"color: #000000;\">It is an unordered collection of items where each item consists of a key and a value. It is mutable (can modify its contents) but Key must be unique and immutable.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Creating Dictionary<\/span><\/h3>\n<p><span style=\"color: #000000;\">It is enclosed in curly braces { } and each item is separated from other item by a comma (,).Within each item, key and value are separated by a colon(:).<\/span><\/p>\n<p><span style=\"color: #000000;\">dictonary_name={key:value, key: value, &#8230;&#8230;..keyN:valueN}<\/span><\/p>\n<p><span style=\"color: #000000;\">Example :<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}\r\nprint day<\/pre>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<pre><span style=\"color: #000000;\"><code>{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}<\/code> <code>&gt;&gt;&gt;<\/code><\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Accessing Dictionary Item<\/span><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}\r\nprint day\r\nprint day[1]\r\nprint day[3]<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<pre><span style=\"color: #000000;\">{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}\r\nMonday\r\nWednesday\r\n&gt;&gt;&gt;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">In above code we are accessing dictionary using key.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}\r\nprint(day.get(1))\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Output<\/span><\/p>\n<pre><span style=\"color: #000000;\">Monday\r\n&gt;&gt;&gt;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Iterating Through A Dictionary<\/span><\/h3>\n<p><span style=\"color: #000000;\">Following example will show how dictionary items can be accessed through loop.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}\r\nfor i in day:\r\n    print(day[i])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<pre><span style=\"color: #000000;\">Monday\r\nTuesday\r\nWednesday\r\n&gt;&gt;&gt; <\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Updating Dictionary Elements<\/span><\/h3>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">We can change the individual element of dictionary.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}\r\nday[1]='Sunday'\r\nprint(day)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">OUTPUT<\/span><\/p>\n<pre><span style=\"color: #000000;\">{1: 'Sunday', 2: 'Tuesday', 3: 'Wednesday'}\r\n&gt;&gt;&gt; <\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Adding New Element in dictionary:<\/span><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}\r\nday[4]='Thursday'\r\nday[5]='Friday'\r\nday[6]='Saturday'\r\nday[7]='Sunday'\r\nprint day\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<pre><span style=\"color: #000000;\">{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'}\r\n&gt;&gt;&gt;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Deleting Dictionary Elements:<\/span><\/h3>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">del, pop() and clear() statement are used to remove elements from the dictionary.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Using del function:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">day {1:'Monday',2:'Tuesday',3:'Wednesday',4:'Thursday',5:'Friday'}\r\ndel day[1]\r\nprint day\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<pre><span style=\"color: #000000;\">{2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday'}\r\n&gt;&gt;&gt;<\/span><\/pre>\n<p><span style=\"color: #000000;\">In above code we deleted only single element.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Built-in Dictionary Functions<\/span><\/h3>\n<p><span style=\"color: #000000;\">\u2666 len(dict) Gives the total length of the dictionary. It is equal to the number of items in the dictionary.<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 str(dict) Return a printable string representation of a dictionary<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 type(variable) If variable is dictionary,then it would return a dictionary type.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Built-in Dictionary Methods<\/span><\/h3>\n<p><span style=\"color: #000000;\">\u2666 clear() Removes all elements of dictionary dict<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 copy() Returns a shallow copy of dictionary dict<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 items()Returns a list of dict&#8217;s (key, value) tuple pairs<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 keys()Returns list of dictionary dict&#8217;s keys<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 setdefault(key,default=None) Similar to get (), but will set dict [key] = default if key is not already in dict<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 update(dict2)Adds dictionary dict2&#8217;s key-values pairs to dict<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 values() Returns list of dictionary dict&#8217;s values<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Important points to remember<\/span><\/h2>\n<p><strong><span style=\"color: #000000;\">1. Dictionary is a collection of elements which is unordered, changeable and indexed.<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">2. Dictionary has keys and values.<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">3. Doesn\u2019t have index for values. Keys work as indexes.<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">4. Dictionary doesn\u2019t have duplicate member means no duplicate key.<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">5. Dictionaries are enclosed by curly braces { }<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">6. The key-value pairs are separated by commas ( , )<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">7. A dictionary key can be almost any Python type, but are usually numbers or strings.<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">8. Values can be assigned and accessed using square brackets [ ].<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">9. Keys of a dictionary must be of immutable types, such as string, number, tuple.<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">10. A dictionary operation that takes a key and finds the corresponding value, is called lookup.<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">11. There are two methods to delete elements from a dictionary: (i) using del statement (ii) using pop( ) method<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">12. To check the existence of a key in dictionary, two operators are used (i) in (ii) not in<\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dictionary in Python Class 11 Notes In this section students of class 11 and 12 having computer science or I.P. will learn about the dictionary in python programming.\u00a0 These notes are prepared as per the CBSE Syllabus and covers all the topics such as &#8211; accessing items in a dictionary using keys, mutability of a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-654","post","type-post","status-publish","format-standard","hentry","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=654"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/654\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}