-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_methods_notes_finished.py
101 lines (83 loc) · 2.6 KB
/
list_methods_notes_finished.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
"""
Name: Mr. C
Title: List Methods Notes
Description: Overview of the list methods you need to know and how to use them.
METHOD - an ability or behavior of an object.
Like strings and turtles, lists are objects that have their own methods.
"""
import random
row = ["Ford", "Audi", "BMW", "Lexus", "Mercedes", "Jeep"]
print (row)
# concatenation is one way to add cars to a list, but it does not mutate the list
row += ["Ford", "Tesla"]
print(row)
# LIST METHODS mutate lists:
# 1: Append Method
# adds an element to the end of a list
row.append("BMW")
print(row)
# we can only append one item at a time
#row.append("Toyota", "Ford")
# ERROR: TypeError: append() takes exactly one argument (2 given)
# 2: Pop method
# removes an element by index value from a list and returns it
# try to... remove the first car in the row
car1 = row.pop(0)
car2 = row.pop(0)
print(row)
print("Returned cars:", car1, car2)
# 3: Remove method
# removes the first occurence of an element from a list
# try to... remove the BMW
row.remove("BMW")
print(row)
row.remove("BMW")
print(row)
# 4: Sort method
# sorts a list in ascending order
# strings sort alphabetically, numbers numerically
# try to... sort the list in ascending order
row.sort()
print(row)
# 5: Reverse method
# reverses the order of a list
# try to... sort the list in descending order
row.reverse()
print(row)
# 6: Count method
# counts the number of occurances of an element
# try to... count the number of "Ford" cars in the row
row.append("Ford")
row.append("Ford")
print(row)
num_fords = row.count("Ford")
print("Number of Fords:", num_fords)
print(row)
# 7: Index method
# displays the index value of an element
# try to... display the index value of the "Jeep"
jeep_index = row.index("Jeep")
print("Jeep index is:", jeep_index)
# 8: Insert method
# inserts an element at a specific index value
# try to... insert a "Harley Davidson" in the second parking spot
print(row)
row.insert(1, "Harley Davidson")
print(row)
# Beware of bugs! Be careful looping through a mutable sequence and also modifying it.
print(row)
for car in row:
print(row.index(car), car)
row.remove(car)
print(row) # we missed 4 cars!
print()
print(row)
for i in range(len(row)):
print(i, row[0])
row.pop(0)
print(row) # removed all
# random.choice(sequence) function
# Return a random element from a sequence, as long as there are items left in the sequence.
rand_car = random.choice(row)
print("A random car:", rand_car)
print("A random vowel:", random.choice("AEIOU"))