Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1.  
  2. #example of removing data from a list
  3. my_list = [1,2,3,4,5]
  4. my_list.remove(3)
  5. print(my_list)
  6.  
  7. #example of modifying value in a list
  8. my_list = [1,2,3,4,5]
  9. my_list.insert(3,6)
  10. print(my_list)
  11. #OUTPUT
  12. #[1, 2, 3, 6, 4, 5]
  13.  
  14. #example of adding key-value pair
  15. fruits = {'apple':'red','grape':'purple','banana':'yellow'}
  16. fruits['blueberry']='blue'
  17. print(fruits)
  18. #OUTPUT
  19. #{'apple': 'red', 'grape': 'purple', 'banana': 'yellow', 'blueberry': 'blue'}
  20.  
  21. #example of removing key-value pair
  22. fruits = {'apple':'red','grape':'purple','banana':'yellow'}
  23. del fruits['grape']
  24. print(fruits)
  25. #OUTPUT
  26. #{'apple': 'red', 'banana': 'yellow'}
  27.  
  28. #example of accessing values using keys
  29. fruits = {'apple':'red','grape':'purple','banana':'yellow'}
  30. del fruits['grape']
  31. print(fruits.values())
  32. #OUTPUT
  33. #dict_values(['red', 'yellow'])
  34.  
  35. #another example of accessing values using keys
  36. fruits = {'apple':'red','grape':'purple','banana':'yellow'}
  37. del fruits['grape']
  38. print(fruits.get('apple'))
  39. #OUTPUT
  40. #red
  41.  
  42. #example of modifying value
  43. fruits = {'apple':'red','grape':'purple','banana':'yellow'}
  44. fruits['grape'] = 'violet'
  45. print(fruits)
  46. #OUTPUT
  47. #{'apple': 'red', 'grape': 'violet', 'banana': 'yellow'}
  48.  
  49. #example of item-based for loop
  50. vehicles = ['cars', 'trucks', 'minivans']
  51. for vehicle in vehicles:
  52. if vehicle == 'trucks':
  53. print('vroom')
  54. else:
  55. print('no truck')
  56. #OUTPUT
  57. #vroom
  58. #no truck
  59.  
  60. #example of index_based (Range) for loop
  61. vehicles = ['cars', 'trucks', 'minivans']
  62. for vehicle in vehicles:
  63. print(vehicle)
  64. for vehicle in range(len(vehicles)):
  65. print(vehicle,vehicles[vehicle])
  66. #OUTPUT
  67. #cars
  68. #trucks
  69. #minivans
  70. #0 cars
  71. #1 trucks
  72. #2 minivans
  73.  
  74. #example of while loop
  75. i = 1
  76. while i<=5:
  77. print("its hot")
  78. i=i+1
  79. #OUTPUT
  80. #its hot
  81. #its hot
  82. #its hot
  83. #its hot
  84. #its hot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement