rezamalik15

Python - Dictionary

Nov 9th, 2023 (edited)
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. # dicionary => key : value
  2. # tanda kurung { }
  3. # object
  4.  
  5. student = {"name": "Jaka", "address": "Jakarta Barat"}
  6.  
  7. car = {
  8.     "model": "truck",
  9.     "brand": "ford",
  10.     "year": 2010,
  11.     "color1": ["red", "blue", "black"],
  12.    
  13. }
  14.  
  15. # display dict keys
  16. print(car.keys())
  17.  
  18. # access item
  19. print(car["brand"])
  20.  
  21. # add new item
  22. car["owner"] = "Jaka"
  23. print(car)
  24.  
  25. # modify item
  26. car["year"] = 2011
  27. print(car)
  28.  
  29. # modify item
  30. car["color"] = ["red", "black"]
  31. print(car)
  32.  
  33. # remove item
  34. car.pop("color")
  35. print(car)
  36.  
  37. # remove the last item
  38. car.popitem()
  39. print(car)
  40.  
Advertisement
Add Comment
Please, Sign In to add comment