loading
open in
main.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
L = [4,9,7]
L.append(10) # adds 10 to the end
print(L) # [4,9,7,10]
print(L[0]) # Item at index 0 - 4
print(L[1]) # Item at index 1 - 9
print(L[-1]) # Python supports negative indexing, so l[-1] is the last item - 10
print(len(L)) # len is built in function that gets length: 4
L2 = [9,9,9]
L3 = L + L2 # adds l2 to the end of l
print(L3) # [4,9,7,10,9,9,9]
# You can also get segments of lists easily
L4 = L3[1:4] # l3 from index 1 to (not including) index 4
print(L4) # [9,7,10]
L5 = L3[1:-1] # L3 from index 1 to (not including) last item.
print(L5) # [9,7,10,9,9]
L6 = L3[1:] # L3 from index 1 to the end
print(L6) # [9,7,10,9,9,9]

# sum of each thing in L3
s = 0
for item in L3: # for each loop
	s += item
print(s) # 57

# but there's a built in function for that! and more!
print(sum(L3)) # sum of all items in L3 - 57
print(len(L3)) # length of L3 - 7
print(max(L3)) # largest item in L3 - 10
print(min(L3)) # smallest item in L3 - 4
print(sorted(L3)) # L3 but sorted (smallest to largest)

# testing for membership in list (no need to loop)
print(9 in L3) # True
print(11 in L3) # False

sorted_l3 = sorted(L3)
print(sorted_l3)