-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter 03.py
More file actions
143 lines (120 loc) · 5.11 KB
/
Chapter 03.py
File metadata and controls
143 lines (120 loc) · 5.11 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# This chapter focuses on lists. Includes:
# index, append, insert, delete, remove, pop, sort, sorted, reverse, last, length, comprehension
# 3-1: List index.
names = ['Tyler', 'Rachel', 'Hector']
print(names[0])
print(names[1])
print(names[2])
# 3-2: List index and string.
print('Hello ' + names[0] + ', it is nice meeting you.')
print('Hello ' + names[1] + ', it is nice meeting you.')
print('Hello ' + names[2] + ', it is nice meeting you.')
# 3-3: Title and upper case.
favorite_transportation = ['toyota', 'bmw', 'audi']
print('\nI would like to drive a cheap ' + favorite_transportation[0].title() + ' to work.')
print('I would like to own an expensive ' + favorite_transportation[1].upper() + ' car when I am older.')
print('I think ' + favorite_transportation[2].title() + ' cars are too expensive for me right now.')
# 3-4: List index.
guests = ['John', 'Robert', 'Sam']
print('\n' + guests[0] + ' is invited to dinner.')
print(guests[1] + ' is invited to dinner.')
print(guests[2] + ' is invited to dinner.')
# 3-5: Remove and insert.
print('\nActually... ' + guests[1] + ' cannot make it to dinner. Jimmy will replace him.')
guests.remove('Robert')
guests.insert(1, 'Jimmy')
print(guests[0] + ' is invited to dinner.')
print(guests[1] + ' is invited to dinner.')
print(guests[2] + ' is invited to dinner.')
# 3-6: Insert and append.
print('\nOh my, I found a bigger dinner table to hold 6 people instead. Let us invite 3 more people.')
guests.insert(0, 'Hanna')
guests.insert(2, 'Jill')
guests.append('Morgana')
print(guests[0] + ' is invited to dinner.')
print(guests[1] + ' is invited to dinner.')
print(guests[2] + ' is invited to dinner.')
print(guests[3] + ' is invited to dinner.')
print(guests[4] + ' is invited to dinner.')
print(guests[5] + ' is invited to dinner.')
# 3-7: Pop.
print('\nOops. The table never arrived so we only have a coffee table now. I only have space for 2 people.')
uninvited = guests.pop(0)
print('Sorry ' + uninvited + ', we have to remove you.')
uninvited = guests.pop(0)
print('Sorry ' + uninvited + ', we have to remove you.')
uninvited = guests.pop(0)
print('Sorry ' + uninvited + ', we have to remove you.')
uninvited = guests.pop(0)
print('Sorry ' + uninvited + ', we have to remove you.')
print(guests[0] + ' you are still invited.')
print(guests[1] + ' you are still invited.')
# Delete.
del guests[0]
del guests[0]
print('\nActually, nobody is invited. I caught the flu! The guest list array looks like this: ' + str(guests))
# 3-8: Temporarily sort.
locations = ['Paris', 'Rome', 'Tokyo', 'Chicago', 'Los Angles']
print('\n' + str(locations) + ' Normal')
print(str(sorted(locations)) + ' Temporarily Sorted')
# Temporarily reverse-sort.
print(str(locations) + ' Normal')
print(str(sorted(locations, reverse=True)) + ' Temporarily Reverse Sorted')
print(str(locations) + ' Normal')
# Permanently reverse.
locations.reverse()
print(str(locations) + ' Permanently Reversed')
locations.reverse()
print(str(locations) + ' Permanently Reversed (again)')
# Permanently sort.
locations.sort()
print(str(locations) + ' Permanently Sort')
# Permanently reverse-sort.
locations.sort(reverse=True)
print(str(locations) + ' Permanently Reverse Sort')
# 3-9 Length.
print('\nExamining the length of the list of dinner guests, I am inviting ' + str(len(guests)) + ' guests.')
print("Examining the length of the list of locations, I have " + str(len(locations)) + " locations")
# 3-10.
# Titled, upper, and lower.
Maps = ['castlwars', 'Rooftops', 'ORBITZ', 'sp00nage', 'melon fields']
print('\nI have a list of Sassilization maps: ' + str(Maps))
print(Maps[0] + ' titled is: ' + Maps[0].title())
print(Maps[1] + ' upper-cased is: ' + Maps[1].upper())
print(Maps[2] + ' lower-cased is: ' + Maps[2].lower())
# Last element.
print('The last element accessed by Maps[3]: ' + Maps[4])
print('The last element accessed by Maps[-1]: ' + Maps[-1])
# Replace, append, and delete.
Maps[0] = 'castlebase'
print('\nReplace castlwars with castlebase: ' + str(Maps))
Maps.append('Hidden Temple')
print('Append Hidden Temple to the end of the map list: ' + str(Maps))
del Maps[2]
print('Delete ORBITZ by index from the map list: ' + str(Maps))
# Pop last and pop index.
popped = Maps.pop()
print('\nPopping ' + popped + ' by variable yields: ' + str(Maps))
print('Popping ' + Maps.pop() + ' in the same print statement yields: ' + str(Maps))
print('Popping ' + Maps.pop(1) + ' by element yields: ' + str(Maps))
# Remove.
remove_map = 'sp00nage'
Maps.remove(remove_map)
print('Remove ' + remove_map + ' from the map list by variable and string: ' + str(Maps))
# Temp sort and reverse-sort.
Maps = ['castlwars', 'rooftops', 'orbitz', 'sp00nage', 'melon fields']
print("\nOriginal map list in lowercase: " + str(Maps))
print("Sorting temporarily: " + str(sorted(Maps)))
print("Reverse sorting temporarily: " + str(sorted(Maps, reverse=True)))
# Permanent sort and reverse-sort.
Maps.reverse()
print('Reverse the list: ' + str(Maps))
Maps.sort()
print('Sort permanently: ' + str(Maps))
Maps.sort(reverse=True)
print('Reverse sort permanently: ' + str(Maps))
# Length.
print('\nThe length of the map list above is: ' + str(len(Maps)))
# 3-11: Index error.
# Maps = []
# print(Maps[-1])