1 """
2 The following are all instances of X{container types}.
3 - lists
4 - tuples
5 - strings
6 - dictionaries
7 - files
8
9 What unifies containers is that they contain a well-defined
10 body of data. Something is either in a container or
11 not.
12
13 The C{in} relation is a well-defined test for all containers:
14
15 >>> S = 'abc'
16 >>> 'b' in S
17 True
18 >>> 'd' in S
19 False
20
21 >>> X = [24, 3.14, 'w', 7]
22 >>> 'w' in X
23 True
24
25 The elements that are 'in' a dictionary are its keys.
26
27 >>> D = {'x':42, 'y': 3.14, 'z': 7}
28 >>> 'x' in D
29 True
30
31 The most common use of C{in} in Python is to define the set
32 of container elements to be iterated through in a C{for}-loop:
33
34 >>> for e in X:
35 ... print e
36 ...
37 24
38 3.14
39 w
40 7
41
42 >>> for k in D:
43 ... print k
44 y
45 x
46 z
47
48 Note there are no guarantees as to which order dictionary keys
49 will be accessed in. The elements contained in a file
50 are its lines.
51
52 >>> file_handle = open('hello.txt','r')
53 >>> for line in file_handle:
54 ... print line
55 Hi there!
56 This is line 2!
57 But this here is line 3!
58
59 >>> file_handle.close()
60
61 """
62
63
65 global X, D, file_handle
66
67 X = [24, 3.14, 'w', 7]
68 print 'X: ', X
69
70 for x in X:
71 print x
72 D = {'x':42, 'y': 3.14, 'z': 7}
73 print 'D: ', D
74
75 for k in D:
76 print k
77
78 import os
79 os.chdir('/Users/jeanmarkgawron/python/intro/intro_lecture_files/')
80 print os.getcwd()
81 file_handle = open('hello.txt','r')
82 print 'hello.txt', file_handle
83 for line in file_handle:
84 print line
85
86
87 if __name__ == '__main__':
88 demo_containers()
89