Package basics :: Module tuples
[hide private]
[frames] | no frames]

Source Code for Module basics.tuples

 1  """ 
 2  Python uses parentheses to enclose B{tuples}. 
 3   
 4     >>> X = (24, 3.14, 'w', 7) #  Tuple with 4 items 
 5     >>> Y = (100,)    # tuple with one item 
 6     >>> Z = () #empty tuple 
 7   
 8  The following is not a tuple: 
 9   
10     >>> Q = (7) 
11     >>> Q == 7 
12     True 
13   
14     >>> L = tuple('hi!')  # make a string a tuple 
15     >>> L 
16     ('h', 'i', '!') 
17     >>> M = tuple() 
18     >>> M 
19     () 
20      
21     >>> X(0)   # 1st element 
22     24 
23     >>> X(1)   # 2nd element 
24     3.1400000000000001 
25     >>> X(-1)   # last element 
26     7 
27     >>> X(0:2) # tuple of 1st and 2nd elements 
28     (24, 3.1400000000000001) 
29     >>> X(:-1) # tuple excluding last element 
30   
31  The following raises an IndexError 
32   
33     >>> X[4]   # Raises exception! 
34     ... 
35     IndexError: tuple index out of range 
36   
37  Concatenation of tuples: 
38   
39     >>> X + Y  
40     (24, 3.1400000000000001, 'w', 7, 100) 
41   
42  """ 
43   
44   
45 -def demo_tuples():
46 global X, Y, Z, L, M 47 48 X = (24, 3.14, 'w', 7) # Tuple with 4 items 49 Y = (100,) # tuple with one item 50 Z = () #empty tuple 51 52 L = tuple('hi!') # make a string a tuple 53 M = tuple() 54 55 X[0] # 1st element 56 X[1] # 2nd element 57 X[-1] # last element 58 X[0:2] # tuple of 1st and 2nd elements 59 X[:-1] # tuple excluding last element 60 61 62 X + Y # Concatenation!
63 64 if __name__ == '__main__': 65 demo_tuples() 66