-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL11- collections.py
More file actions
34 lines (31 loc) · 1.24 KB
/
Copy pathL11- collections.py
File metadata and controls
34 lines (31 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# collection = a single variable that holds multiple values
# list = [] ordered and changeable. Allows duplicates
veggies = ["carrot", "broccoli", "spinach"]
print("Length of veggies list:", len(veggies))
print(veggies[0])
veggies.append("cauliflower")
print("After adding cauliflower:", veggies)
veggies.remove("broccoli")
print("After removing broccoli:", veggies)
veggies.insert(1, "asparagus")
print("After inserting asparagus:", veggies)
veggies.sort()
print("After sorting veggies:", veggies)
veggies.reverse()
print("After reversing veggies:", veggies)
# set = {} unordered and unindexed. add/remove ok. No duplicates
fruits = {"apple", "banana", "cherry","cherry", "apple", "dragonfruit","orange"}
print("Fruits set:", fruits)
fruits.add("kiwi")
print("After adding kiwi:", fruits)
fruits.remove("banana")
print("After removing banana:", fruits)
fruits.pop()
print("After popping an element:", fruits)
# tuple = () ordered and unchangeable. Allows duplicates. faster than sets and lists
colours = ("red", "green", "blue", "red", "black")
print("Colours tuple:", colours)
print("Length of colours tuple:", len(colours))
print(colours[0])
print(colours.count("red"))
# dictionary = a collection which is unordered, changeable and indexed. No duplicate members.