Skip to content

Commit d4ed35e

Browse files
Caleb CurryCaleb Curry
authored andcommitted
lists
1 parent 1fdbcad commit d4ed35e

3 files changed

Lines changed: 267 additions & 0 deletions

File tree

‎python/01-lists.py‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#Welcome. We are going to be working through python
2+
#It should be pretty easy to follow along with but if you struggle
3+
#I have a beginner series as well
4+
5+
6+
########## APPEND TO LIST #########
7+
8+
9+
healthy = ["pizza", "frozen custard"]
10+
11+
healthy.append("apple crisp")
12+
13+
print(healthy)
14+
15+
16+
########## CHECKING IF ELEMENT IN LIST #########
17+
18+
19+
#Obviously on our health adventures we want to know if something is healthy.
20+
21+
print("chicken pot pie" in healthy)
22+
23+
#This returns True / False. Can use in keyword within if
24+
25+
backpack = ["pizza", "chicken pot pie", "kale chips"]
26+
27+
if("pizza" in healthy):
28+
print("eating it")
29+
30+
#We can make this more dynamic. Let's remove chicen pot pie from list.
31+
32+
33+
########## REMOVING FROM LIST ##########
34+
35+
36+
healthy = ["pizza", "frozen custard"]
37+
backpack = ["pizza", "chicken pot pie", "kale chips"]
38+
39+
if("pizza" in healthy):
40+
backpack.remove("pizza")
41+
42+
print(backpack)
43+
44+
45+
########## COUNTING ELEMENTS IN LIST ##########
46+
47+
healthy = ["pizza", "frozen custard"]
48+
backpack = ["pizza", "chicken pot pie", "kale chips"]
49+
50+
print(len(healthy), len(backpack))
51+
52+
#You may think of something like healthy.size() or healthy.length() or even healthy.count()
53+
#This is not quite what we are looking for.
54+
#count is used to count a particular element in a list. Up next!
55+
56+
57+
########## COUNT / CHECK IF ELEMENT EXISTS ##########
58+
59+
60+
print(backpack.count("pizza")) #number of pizza in list
61+
62+
backpack = ["pizza slice", "pizza slice", "pizza slice"]
63+
64+
print(backpack.count("pizza slice")) #3
65+
66+
#This can be used to prevent too many items:
67+
68+
if(backpack.count("pizza slice") < 3):
69+
backpack.append("pizza slice")
70+
print("You put a piece of pizza in your backpack")
71+
else:
72+
print("How about you go to the gym?")
73+
74+
75+
########## INSERT INTO MIDDLE OF LIST ##########
76+
77+
78+
#Lists are ordered, and this order may matter to you.
79+
80+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
81+
82+
#OVERTIME!
83+
work_days.insert(3, "Wednesday") #index, "data"
84+
85+
print(work_days)
86+
87+
#What if we want to do the opposite and take a day out?
88+
89+
90+
########## REMOVE ELEMENT FROM LIST BY INDEX ##########
91+
92+
93+
#We learned how to remove by value:
94+
95+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
96+
97+
#VACATION DAY!
98+
work_days.remove("Saturday")
99+
print(work_days)
100+
101+
#However, removing by index is also useful:
102+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
103+
104+
del work_days[0]
105+
print(work_days)
106+
107+
108+
########## REMOVE ELEMENT WITH POP ##########
109+
110+
111+
#The benefit here is the method returns the element
112+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
113+
popped = work_days.pop(1)
114+
115+
print("You removed " + popped)
116+
print(work_days)
117+
118+
119+
########## REMOVE ELEMENT FROM LIST USING DEL AND SLICE ##########
120+
121+
122+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
123+
124+
del work_days[0:2] #remove first 2
125+
print(work_days)
126+
127+
del work_days[-2:] #remove last 2 (start 2 from right and go to end)
128+
print(work_days)
129+
130+
131+
########## REMOVING ALL OCCURANCES IN LIST ##########
132+
133+
backpack = ["pizza slice", "button", "pizza slice", "fishing pole",
134+
"pizza slice", "nunchicks", "pizza slice", "sandwich from mcdonalds"]
135+
136+
backpack.remove("pizza slice")
137+
print(backpack) #SO MUCH PIZZA!
138+
139+
while(backpack.count("pizza slice") > 0):
140+
backpack.remove("pizza slice")
141+
142+
print(backpack)
143+
144+
#This may not be the most optimized solution as each removal requires
145+
#an iteration from backpack.count. But it's cleanest
146+
#You should also avoid modifying a list while iterating, so a for-in loop is bad
147+
148+
#for item in backpack:
149+
# if(item == "pizza slice"):
150+
# backpack.remove(item)
151+
152+
#The original solution is fine for removing data from reasonably sized lists
153+
#for very very large lists, look into list comprehension
154+
#Which we are not getting into quite yet.
155+
156+

‎python/02-sorting.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
########## REVERSE LIST ##########
2+
3+
4+
backpack = ["pizza slice", "button", "pizza slice", "fishing pole",
5+
"pizza slice", "nunchicks", "pizza slice", "sandwich from mcdonalds"]
6+
7+
print(backpack)
8+
backpack.reverse()
9+
print(backpack)
10+
11+
12+
########## SORT METHOD ##########
13+
14+
15+
#This is using python built in method and not custom built
16+
#You can easily sort a list:
17+
work_days = ["Monday", "Tuesday", "Thursday", "Friday", "Saturday"]
18+
19+
work_days.sort()
20+
21+
print(work_days)
22+
#Remember...no return. Modifies list
23+
24+
numbers = [1, 11, 115, 13, 1305, 43]
25+
print(numbers)
26+
#This is not possible because no return.
27+
# print(numbers.sort())
28+
29+
30+
numbers.sort()
31+
print(numbers)
32+
33+
#What if you want to sort without messing up original?
34+
#We can copy.
35+
36+
37+
########## SORTED - COPY A LIST FOR SORT ##########
38+
39+
numbers = [1, 11, 115, 13, 1305, 43]
40+
numbers_sorted = sorted(numbers)
41+
42+
#This returns a NEW list. so original is unaffected!
43+
44+
print(numbers) #original list
45+
print(numbers_sorted)
46+
47+
#This also allows us to assign a sorted listed immediately.
48+
49+
numbers = sorted([1, 11, 115, 13, 1305, 43])
50+
print(numbers)
51+
52+
53+
########## SORT IN REVERSE ##########
54+
55+
56+
#You can easily get a reverse sorted list.
57+
numbers = sorted([1, 11, 115, 13, 1305, 43])
58+
numbers.reverse()
59+
60+
print(numbers)
61+
62+
#However, sorted has an optional/default parameter
63+
#We can pass a value to it
64+
65+
print(sorted(numbers, reverse=True))
66+
67+
68+
69+
########## CASE INSENSITIVE SORT ##########
70+
71+
#When working with strings, 'a' and 'A' are different.
72+
73+
letters = ['a', 'A', 'abc', 'ABC', 'aBc', 'aBC', 'Abc']
74+
75+
print(sorted(letters)) #Capital is considered first
76+
print(sorted(letters, key=str.lower))
77+
78+
79+
########## SORT BY STRING LENGTH ##########
80+
81+
82+
random = ["a", "A", "aa", "AAA", "HELLO", "b", "c", "a"]
83+
print(sorted(random, key=len))
84+
#no () on function! len refers to function. len() invokes function
85+
86+
87+
########## SORT NUMBERS WITH LEXICOGRAPHICAL SORTING ##########
88+
89+
90+
#We can sort numbers similar to strings 1, 11, 111, 2, 22, 222
91+
numbers = [1, 54, 76, 12, 111, 4343, 6, 8888, 3, 222, 1, 0, 222, -1, -122, 5, -30]
92+
print(sorted(numbers, key=str))
93+
94+
#Basically, when we are working with strings,
95+
#"111" < "12" because we compare by character left to right
96+
#So we can cast each to a str using the str constructor
97+
98+
99+
########## COMPARE NUMERICALLY ##########
100+
101+
102+
#Just like we compared using strings in the previous section, we can do it with numbers
103+
#Talse is 0
104+
#True is 1
105+
#expression evaluates to true and maintains that value
106+
#No data is converted to a float in list. Strings are still strings. #bools are bools.
107+
108+
109+
age = 5
110+
stuff = [True, False, 0, -1, "0", "1", "10", age < 30, "20", "2", "5", "9001", "5.5", "6.0", 6]
111+
print(sorted(stuff, key=float))

‎python/03-list-input.py‎

Whitespace-only changes.

0 commit comments

Comments
 (0)