Guest User

Untitled

a guest
Nov 29th, 2017
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. # Dictionary in Python
  2.  
  3. # Creating a dictionary
  4.  
  5. dict = {
  6. 'John': 'john.smith@gmail.com',
  7. 'Mary': 'mary.johnson@gmail.com',
  8. 'Richard': 'richard.jackson@gmail.com',
  9. 'Cindy': 'cindy.mcgregor@gmail.com',
  10. 'Jessica': 'jessica.jones@gmail.com'
  11. }
  12.  
  13. # Accessing a single element
  14.  
  15. print("John's address is", dict['John'])
  16.  
  17. # Deleting a key-value
  18.  
  19. del dict['Jessica']
  20.  
  21. # Adding a key-value pair
  22.  
  23. dict['Mark'] = 'mark.zuckerberg@gmail.com'
  24.  
  25. # Find the count of dict elements
  26.  
  27. print('\nThere are {} contacts in the address-book\n'.format(len( dict)))
  28.  
  29. # Accessing the whole dictionary
  30.  
  31. for name, address in dict.items():
  32. print('Contact {} at {}'.format(name, address))
  33.  
  34. # Checking if a value is the dictionary
  35.  
  36. if 'Cindy' in dict:
  37. print("\nCindy's address is", dict['Cindy'])
Add Comment
Please, Sign In to add comment