Index page for Python:
http://mylearningcafe.blogspot.com/2015/08/python-index-page.html
Dictionaries in python are data structures which associate keys with values.
http://mylearningcafe.blogspot.com/2015/08/python-index-page.html
Dictionaries in python are data structures which associate keys with values.
>>> empl_name_id_dict = {"Nitin":1,"Jonathan":2,"Brien":3}
>>> briens_details = empl_name_id_dict["Brien"]
>>> briens_details
3
>>> briens_details = empl_name_id_dict["brien"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'brien'
Keys are case sensitive.
Get method helps to get a default value for a non-existing key rather than an exception
>>> briens_details = empl_name_id_dict.get("brien")
>>> briens_details
>>>
>>> briens_details = empl_name_id_dict.get("Brien")
>>> briens_details
3
//Adding a new element
>>> empl_name_id_dict ["Matt"] = 4
>>> empl_name_id_dict
{'Nitin': 1, 'Matt': 4, 'Jonathan': 2, 'Brien': 3}
//Get the list of keys or values
>>> list_of_keys = empl_name_id_dict.keys()
>>> list_of_keys
['Nitin', 'Matt', 'Jonathan', 'Brien']
>>>
>>> list_of_values = empl_name_id_dict.values()
>>> list_of_values
[1, 4, 2, 3]
>>> list_of_items = empl_name_id_dict.items()
>>> list_of_items
[('Nitin', 1), ('Matt', 4), ('Jonathan', 2), ('Brien', 3)]
Sets is a data structure that represents a collection of distinct elements.
>>> list_of_items = [1,100,200,3,100,200,5]
>>> list_of_items
[1, 100, 200, 3, 100, 200, 5]
>>>
>>> list_of_items_set = set(list_of_items)
>>> list_of_items_set
set([200, 1, 3, 100, 5])
Index page for Python:
Index page for Python:
No comments:
Post a Comment