forked from huangsam/ultimate-python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.py
More file actions
Latest commit
104 lines (85 loc) · 3.62 KB
/
Copy pathlist.py
File metadata and controls
104 lines (85 loc) · 3.62 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
"""
Lists are a sequence of values that can be modified at runtime. This
module shows how lists are created, iterated, accessed, extended
and shortened.
"""
defmain() ->None:
# This is a list of strings where
# "a" is a string at index 0 and
# "e" is a string at index 4
letters= ["a", "b", "c", "d", "e"]
assertletters[0] =="a"
assertletters[4] ==letters[-1] =="e"
forletterinletters:
# Each of the strings is one character
assertlen(letter) ==1
# Each of the strings is a letter
assertletter.isalpha()
# We can get a subset of letters with range slices
assertletters[1:] == ["b", "c", "d", "e"]
assertletters[:-1] == ["a", "b", "c", "d"]
assertletters[1:-2] == ["b", "c"]
assertletters[0:3:2] == ["a", "c"]
assertletters[::2] == ["a", "c", "e"]
assertletters[::-2] == ["e", "c", "a"]
assertletters[::-1] == ["e", "d", "c", "b", "a"]
# This is a list of integers where
# 1 is an integer at index 0 and
# 5 is an integer at index 4
numbers= [1, 2, 3, 4, 5]
assertnumbers[0] ==1
assertnumbers[4] ==numbers[-1] ==5
# Note that a list is ordered and mutable. If we want to reverse the order
# of the `numbers` list, we can start at index 0 and end halfway. At each
# step of the `for` loop, we swap a value from the first half of the list
# with a value from the second half of the list
forix_frontinrange(len(numbers) //2):
ix_back=len(numbers) -ix_front-1
numbers[ix_front], numbers[ix_back] =numbers[ix_back], numbers[ix_front]
# Let's check that `numbers` is in reverse order
assertnumbers== [5, 4, 3, 2, 1]
# Suppose that we want to go back to the original order, we can use the
# builtin `reverse` method in lists
numbers.reverse()
# Let's check that `numbers` is in the original order
assertnumbers== [1, 2, 3, 4, 5]
# Print letters and numbers side-by-side using the `zip` function. Notice
# that we pair the letter at index 0 with the number at index 0, and
# do the same for the remaining indices. To see the indices and values
# of a list at the same time, we can use `enumerate` to transform the
# list of values into an iterator of index-value pairs
forindex, (letter, number) inenumerate(zip(letters, numbers)):
assertletters[index] ==letter
assertnumbers[index] ==number
# The `for` loop worked because the lengths of both lists are equal
assertlen(letters) ==len(numbers)
# Lists can be nested at arbitrary levels
matrix= [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
assertmatrix[0][0] ==1
assertmatrix[0][1] ==2
assertmatrix[1][0] ==4
assertmatrix[1][1] ==5
# This matrix just so happens to be a square so that the length of each
# row is the same as the number of rows in the matrix
forrowinmatrix:
assertlen(matrix) ==len(row)
# Notice that lists have variable length and can be modified to have
# more elements. Lists can also be modified to have fewer elements
lengthy= []
foriinrange(5):
lengthy.append(i) # add 0..4 to the back
assertlengthy== [0, 1, 2, 3, 4]
lengthy.pop() # pop out the 4 from the back
assertlengthy== [0, 1, 2, 3]
# Let's sort this list in ascending order
numbers= [5, 4, 3, 2, 1]
numbers.sort()
assertnumbers== [1, 2, 3, 4, 5]
# Let's check if these lists are empty
assertlen(numbers) ==5
empty_list: list[object] = []
assertlen(empty_list) ==0
assertnotempty_list
assertlen([None]) ==1
if__name__=="__main__":
main()