-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collections.py
38 lines (31 loc) · 1.32 KB
/
Collections.py
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
35
36
37
38
def DICT():
person = {"name": "Lazzzer", "age": 15, "city": "New York"}
person = dict(name="Lazzzer", age=15, city="New York") # these are the ways to difine the same dictionary
person = dict([("name", "Lazzzer"), ("age", 15), ("city", "New York")])
assert person["name"] == "Lazzzer"
assert person["age"] == 15
assert person["city"] == "New York"
def LIST(): # lists and arrays are the same, but lists can be heterogeneous, while arrays are homogeneous
fruitSet = {"banana", "apple", "orange"};
fruits = list(fruitSet);
assert fruits[0] in fruitSet
assert len(fruits) == 3;
assert list("String") == ["S", "t", "r", "i", "n", "g"]
def TUPLE(): # the same as a list, but cannot be changed
coordinates = tuple([3, 4])
coordinates = (3, 4)
assert coordinates[0] == 3
assert coordinates[1] == 4
# coordinates[0] = 5 !ERROR!
def SET(): # sets cannot contain duplicates
fruitList = ["apple", "cherry", "apple", "banana"]
fruits = set(fruitList)
fruits = {"apple", "cherry", "banana"}
assert len(fruits) == 3
assert "banana" in fruits
def FROZENSET(): # the same as a set, but cannot be changed
colorList = ["red", "blue", "green", "green"]
colors = frozenset(colorList)
assert len(colors) == 3
assert "red" and "blue" in colors
# colors.add("yellow") !ERROR!