Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #example of removing data from a list
- my_list = [1,2,3,4,5]
- my_list.remove(3)
- print(my_list)
- #example of modifying value in a list
- my_list = [1,2,3,4,5]
- my_list.insert(3,6)
- print(my_list)
- #OUTPUT
- #[1, 2, 3, 6, 4, 5]
- #example of adding key-value pair
- fruits = {'apple':'red','grape':'purple','banana':'yellow'}
- fruits['blueberry']='blue'
- print(fruits)
- #OUTPUT
- #{'apple': 'red', 'grape': 'purple', 'banana': 'yellow', 'blueberry': 'blue'}
- #example of removing key-value pair
- fruits = {'apple':'red','grape':'purple','banana':'yellow'}
- del fruits['grape']
- print(fruits)
- #OUTPUT
- #{'apple': 'red', 'banana': 'yellow'}
- #example of accessing values using keys
- fruits = {'apple':'red','grape':'purple','banana':'yellow'}
- del fruits['grape']
- print(fruits.values())
- #OUTPUT
- #dict_values(['red', 'yellow'])
- #another example of accessing values using keys
- fruits = {'apple':'red','grape':'purple','banana':'yellow'}
- del fruits['grape']
- print(fruits.get('apple'))
- #OUTPUT
- #red
- #example of modifying value
- fruits = {'apple':'red','grape':'purple','banana':'yellow'}
- fruits['grape'] = 'violet'
- print(fruits)
- #OUTPUT
- #{'apple': 'red', 'grape': 'violet', 'banana': 'yellow'}
- #example of item-based for loop
- vehicles = ['cars', 'trucks', 'minivans']
- for vehicle in vehicles:
- if vehicle == 'trucks':
- print('vroom')
- else:
- print('no truck')
- #OUTPUT
- #vroom
- #no truck
- #example of index_based (Range) for loop
- vehicles = ['cars', 'trucks', 'minivans']
- for vehicle in vehicles:
- print(vehicle)
- for vehicle in range(len(vehicles)):
- print(vehicle,vehicles[vehicle])
- #OUTPUT
- #cars
- #trucks
- #minivans
- #0 cars
- #1 trucks
- #2 minivans
- #example of while loop
- i = 1
- while i<=5:
- print("its hot")
- i=i+1
- #OUTPUT
- #its hot
- #its hot
- #its hot
- #its hot
- #its hot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement