Posts

Dictionary and Sets Session 6

Dictionary  Dictionary is a collection of key-value pairs. Syntax: ''' a = {“key”: “value”, “hey”: “code”, “marks” : “100”, “list”: [1,2,9]} a[“key”] # Prints value a[“list”] # Prints [1,2,9] ''' Copy Properties of Python Dictionaries It is unordered It is mutable It is indexed It cannot contain duplicate keys Dictionary Methods Consider the following dictionary, a = { “name” : “Codes4u” , “ from ” : “Pakistan” , “marks” : [ 92 , 98 , 96 ] } Copy items()  : returns a list of (key,value) tuple. keys()  : returns a list containing dictionary’s keys. update({“friend”: “Iqra”})  : updates the dictionary with supplied key-value pairs. get(“name”)  : returns the value of the specified keys (and value is returned e.g., “Iqra” is returned here) More methods are available on docs.python.org Sets in Python Set is a collection of non-repetitive elements. S = Set ( ) # No repetition allowed! S . add ( 1 ) S . add ( 2 ) # or Set = {1,2} Copy If you...