Untitled Document
Dictionary
1. Program - To Create Dictionary Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program To Create Dictionary       
       # Dictionary - Dictionary is represented in curly braces  {}
       # Dictionary represents data in key-value pair format 
       # Here is the simple example to create list
       my_dict = {'name': "Mangesh Pande", 'age': 25}
       print(my_dict)

       
2. Program - Printing values from Dictionary Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Printing values from Dictionary
       
       # Approach -1 ; using key from a Dictionary
       my_dict = {'name': "Mangesh Pande", 'age': 25}
       print(len(country))
       
       
       # Approach -2 ;One more way to print dictionary values is using get method 
       my_dict = {'name': "Mangesh Pande", 'age': 25}
       print(my_dict.get('age'))
       
       # Approach -3 ;You can print default values for the keys which are not present in list 
       # Just Run below code and you will understand by seeing output .
       
       my_dict = {'name': "Mangesh Pande", 'age': 25}
       print(my_dict.get('phone','Not Found'))

     
3. Program - To add new key to Dictionary Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To add new key to Dictionary 
       # adding new key to the dictionary
       
       my_dict = {'name': "Mangesh Pande", 'age': 25}   
       my_dict['phone']="999-654"
       print(my_dict)       
       
4. Program - To Update Dictionary values Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Update Dictionary values
       
       # updating Single dictionary record       
       my_dict = {'name': "Mangesh Pande", 'age': 25}   
       my_dict['name'] ="Mohan"
       print(my_dict)       
       
       # Updating multiple keys (Values) from a dictionary       
       my_dict = {'name': "Mangesh Pande", 'age': 25, 'phone':'999-999'}   
       my_dict.update({'name': "Mrunmayee", 'age':2, 'phone':'123-456'})
       print(my_dict)      
       
5. Program - To delete elements from a dictionary Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To delete elements from a dictionary
       
       #Delete element(Key) from a dictionary
       my_dict = {'name': "Mangesh Pande", 'age': 25, 'phone':'999-999'}              
       del my_dict['age']            
       print(my_dict)
       
       # another way to delete dictionary element is using pop method 
       my_dict = {'name': "Mangesh Pande", 'age': 25, 'phone':'999-999'}
       age=my_dict.pop('age')
       print(age)
       print(my_dict)
       
6. Program - To Demonstrate different methods in dictionary Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Demonstrate different methods in dictionary
       # How to get length of a dictionary i.e number of keys in a dictionary
       
       # 1. len() Method --> How to get length of a dictionary i.e number of keys in a dictionary 
       
       my_dict = {'name': "Mangesh Pande", 'age': 25, 'phone':'999-999'}
       print(len(my_dict))            
              
       # 2. keys() Method --> How to print keys ( Only keys) from a dictionary
       
       my_dict = {'name': "Mangesh Pande", 'age': 25, 'phone':'999-999'}
       print(my_dict.keys())
       
       # 3. values() Method --> How to print values ( Only values) from adictionary
       
       my_dict = {'name': "Mangesh Pande", 'age': 25, 'phone':'999-999'}
       print(my_dict.values())
       
       # 4. items() Method --> # How to loop through dictionary to print both key and value 
       
       my_dict = {'name': "Mangesh Pande", 'age': 25, 'phone':'999-999'}
       for key, value in my_dict.items():
       	print(key, value)
       
Untitled Document