Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. # Syntax: D.method()
  2. # Where D = dictionary, dict
  3.  
  4. # D.clear() -> None.
  5. # Remove all items from D.
  6. car = {
  7. 'brand': 'Telsla',
  8. 'model': 'Model S',
  9. 'year': '2012'
  10. }
  11. print(car)
  12. # {'brand': 'Telsla', 'model': 'Model S', 'year': '2012'}
  13. print(car.clear())
  14. {}
  15.  
  16. # D.copy -> a shallow copy of D
  17. car = {
  18. 'brand': 'Telsla',
  19. 'model': 'Model S',
  20. 'year': '2012'
  21. }
  22. print(car)
  23. # {'brand': 'Telsla', 'model': 'Model S', 'year': '2012'}
  24. x = car.copy()
  25. print(x)
  26. # {'brand': 'Telsla', 'model': 'Model S', 'year': '2012'}
  27.  
  28. # D.fromkeys(iterable, [value=None, /])
  29. # '/' marks preceding args as positional-only.
  30. # Create a new dictionary with keys from iterable and values set to value.
  31. keys = ('key1', 'key2', 'key3')
  32. values = 0
  33. thisdict = dict.fromkeys(keys, values)
  34. print(thisdict)
  35. # ['key1': 0, 'key2': 0, 'key3': 0]
  36.  
  37. thisdict = dict.fromkeys(keys)
  38. print(thisdict)
  39. # ['key1': None, 'key2': None, 'key3': None]
  40.  
  41. # D.get(key, [default=None, /])
  42. # '/' marks preceding args as positional-only.
  43. # Return the value for key if key is in the dictionary, else default.
  44. car = {
  45. 'brand': 'Telsla',
  46. 'model': 'Model S',
  47. 'year': '2012'
  48. }
  49. get_key = car.get("model")
  50. print(get_key)
  51. # model
  52. get_value = car.get("price", 38990)
  53. print(get_value)
  54. # 38990
  55.  
  56. # D.items() -> a set-like object providing a view on dict's items
  57. car = {
  58. 'brand': 'Telsla',
  59. 'model': 'Model X',
  60. 'year': 2012
  61. }
  62. thisdict_items = car.items()
  63. print(thisdict_items)
  64. # dict_items([('brand', 'Telsla'), ('model', 'Model S'), ('year', 2012)])
  65. car["year"] = 2019
  66. print(thisdict_items)
  67. # dict_items([('brand', 'Telsla'), ('model', 'Model X'), ('year', 2019)])
  68.  
  69. # D.keys() -> a set-like object providing a view on dict's keys
  70. thisdict_keys = car.keys()
  71. print(thisdict_keys)
  72. # dict_keys(['brand', 'model', 'year'])
  73. car["color"] = "White"
  74. print(thisdict_keys)
  75. # dict_keys(['brand', 'model', 'year', 'color'])
  76.  
  77. # D.values() -> an object providing a view on D's values
  78. car = {
  79. 'brand': 'Telsla',
  80. 'model': 'Model S',
  81. 'year': 2019
  82. }
  83. thisdict_values = car.values()
  84. print(thisdict_values)
  85. # dict_values(['Telsla', 'Model S', 2019])
  86. thisdict_values = car.values()
  87. car['year'] = 2020
  88. print(thisdict_values)
  89. # dict_values(['Telsla', 'Model S', 2020])
  90.  
  91. # D.pop(dict.pop(k[,d]) -> v, remove specified key and return the corresponding value.
  92. # If key is not found, d is returned if given, otherwise keyError is raised
  93. car = {
  94. 'brand': 'Telsla',
  95. 'model': 'Model X',
  96. 'year': 2019
  97. }
  98. car.pop('model')
  99. print(car)
  100. # {'brand': 'Telsla', 'year': 2019}
  101.  
  102. car = {
  103. 'brand': 'Telsla',
  104. 'model': 'Model X',
  105. 'year': 2019
  106. }
  107. car.pop('model', 'Model X')
  108. print(car)
  109. # {'brand': 'Telsla', 'year': 2019}
  110.  
  111. # D.popitem() -> (k, v), remove and return som (key, value) pair as a
  112. # 2-tuple; but raise KeyError if D is empty.
  113.  
  114. car = {
  115. 'brand': 'Telsla',
  116. 'model': 'Model X',
  117. 'year': 2019
  118. }
  119. car.popitem()
  120. print(car)
  121. # {'brand': 'Telsla', 'model': 'Model X'}
  122.  
  123. # D.setdefault(key, [default=None, /])
  124. # '/' marks preceding args as positional-only.
  125. # Insert key with a value of default if key is not in the dictionary.
  126.  
  127. car = {
  128. 'brand': 'Telsla',
  129. 'model': 'Model S',
  130. 'year': 2019
  131. }
  132. set_default_value = car.setdefault("model", "Model X")
  133. print(set_default_value)
  134. # Model S
  135. print(car)
  136. # {'brand': 'Telsla', 'model': 'Model S', 'year': 2019}
  137.  
  138. # D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
  139. # If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
  140. # If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
  141. # In either case, this is followed by: for k in F: D[k] = F[k]
  142.  
  143. car = {
  144. 'brand': 'Telsla',
  145. 'model': 'Model S',
  146. 'year': 2019
  147. }
  148. car.update({"color": "White"})
  149. print(car)
  150. # {'brand': 'Telsla', 'model': 'Model S', 'year': 2019, 'color': 'White'}
  151.  
  152. '''To get a list of all built-in dictionary
  153. methods or objects in Python you will need to use the
  154. 'Tab' or 'control + Space' key on your keyboard after the
  155. two quotation marks and period (.)
  156. like this {}. {} is used to dictionary.
  157. -->''.'wait a few seconds to see the methods' or ''.'Tab' or ''.'control + Space'<--
  158. You can replace "{}" with 'dict'.
  159. Syntax: dict.method() where dict is the dictionary objects
  160.  
  161. Python dictionary methods takes the form dict.method(). Where
  162. dict is the dictionary object.
  163. The period (.) on the method makes a function become a method because
  164. the function now
  165. runs on the object, dict.
  166. This means that a function can be a method but a method can't be a function.
  167. To see the complete methods on a list type 'help(dict)' or 'help('')'.
  168. Syntax: dict.method() or dict.method(object) in some cases.
  169. Where dict is the dict object.
  170. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement