forked from jstrickler/20230306JPMC-AM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
using_lists.py
123 lines (87 loc) · 2.61 KB
/
using_lists.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
list1 = list() # new, empty list
# x = list(iterable)
list2 = ['spam', 'ham', 'toast'] # list
t = 'spam', 'ham', 'toast' # tuple
list3 = [5, 1, 8, -2, 5.3]
list4 = ['spam', 42, True, None, [1, 2, 3]]
list5 = [] # empty list
# x = [obj, obj, obj, obj, ...]
cities = ['Delaware', 'Dallas', "Washington", "New York",
"Boston"]
print(f"cities: {cities}")
print(f"cities[3]: {cities[3]}")
cities[1] = "Austin"
print(f"cities: {cities}")
cities.insert(0, "Easton")
print(f"cities: {cities}")
cities.insert(3, "Milwaukee")
print(f"cities: {cities}")
cities.append("Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch")
print(f"cities: {cities}")
cities.append("Philadelphia")
print(f"cities: {cities}")
more_cities = ['Leicester', 'Worcester', 'Peabody', 'Reno']
cities.append(more_cities)
print(f"cities: {cities}")
cities.extend(more_cities)
print(f"cities: {cities}")
# LIST.insert(pos, obj) LIST.append(obj) LIST.extend(iterable)
cities.extend("fun")
print(f"cities: {cities}")
del cities[9]
print(f"cities: {cities}")
del cities[7]
print(f"cities: {cities}")
# del LIST[pos] del DICT[key] del any-obj
cities.remove('f')
cities.remove('u')
print(f"cities: {cities}")
item = cities.pop()
print(f"item: {item}")
print(f"cities: {cities}")
item = cities.pop(2)
print(f"item: {item}")
print(f"cities: {cities}")
# del LIST[pos] LIST.remove(value) LIST.pop(pos) LIST.pop()
city_to_find = "Milwaukee"
if city_to_find in cities:
index = cities.index(city_to_find)
print(f"index: {index}")
item = cities.pop(index)
print(f"item: {item}")
print(f"cities: {cities}")
# cities.remove('Milwaukee')
print(f"cities: {cities}")
print(f"cities[2]: {cities[2]}")
print(f"cities[9]: {cities[9]}")
print(f"cities[len(cities)-1]: {cities[len(cities)-1]}")
print(f"cities[-1]: {cities[-1]}")
print(f"cities[-2]: {cities[-2]}")
# STR[0] BYTES[0] LIST[0] TUPLE[0]
# print(f"cities[22]: {cities[22]}")
print(f"cities: {cities}")
print(f"cities[0:3]: {cities[0:3]}")
# start:stop :stop start: : start:stop:step
print(f"cities[3:7]: {cities[3:7]}")
print(f"cities[:4]: {cities[:4]}")
print(f"cities[4:]: {cities[4:]}")
print(f"cities[-3:]: {cities[-3:]}")
client = "J P Morgan Chase"
print(f"client[:10]: {client[:10]}")
print(f"client[-5:]: {client[-5:]}")
print('-' * 60)
for city in cities:
print(city)
print()
for i, city in enumerate(cities):
if (i != 0) and ((i % 3) == 0):
print("====")
print(i, city)
print()
print(f"list(enumerate(cities)): {list(enumerate(cities))}")
s = "garbanzo"
for char in s:
print(char)
print()
# for VAR ... in ITERABLE:
# ...