1 """
2 Python uses square brackets to enclose B{lists}.
3
4 >>> X = [24, 3.14, 'w', 7] # List with 4 items
5 >>> Y = [100] # list with one item
6 >>> Z = [] #empty list
7
8 Lists are sequences of items which may be of different data types.
9
10 >>> L = list('hi!') # make a string a list
11 >>> L
12 ['h', 'i', '!']
13 >>> M = list()
14 >>> M
15 []
16
17 The following examples illustrate references to list elements
18 or list subsequences:
19
20 >>> X[0] # 1st element
21 24
22 >>> X[1] # 2nd element
23 3.1400000000000001
24 >>> X[-1] # last element
25 7
26 >>> X[0:2] # list of 1st and 2nd elements
27 [24, 3.1400000000000001]
28 >>> X[:-1] # list excluding last element
29
30 References to subsequences of a list are called
31 X{slices}. The following raises an IndexError
32
33 >>> X[4] # Raises exception!
34 ...
35 IndexError: list index out of range
36 >>> X + Y # Concatenation!
37 [24, 3.1400000000000001, 'w', 7, 100]
38
39 Lists allow X{value assignments}, which change the value of a reference
40 in place (X{in place assignment}).
41
42 >>> X[2] = 5
43 >>> X
44 [24, 3.1400000000000001, 5, 7]
45 >>> X[0:2] = [1,3]
46 >>> X
47 [1, 3, 5, 7]
48
49 Only list-values can be assigned to slices.
50 """
51
52
54 global X, Y, Z, L, M
55
56 X = [24, 3.14, 'w', 7]
57 print X
58 Y = [100]
59 Z = []
60
61 L = list('hi!')
62 M = list()
63
64 X[0]
65 X[1]
66 X[-1]
67 X[0:2]
68 X[:-1]
69
70
71 X + Y
72 X[2] = 5
73 print X
74
75 if __name__ == '__main__':
76 demo_lists()
77