1 Hello friends and welcome to the tutorial on Tuples |
|
2 |
|
3 {{{ Show the slide containing title }}} |
|
4 |
|
5 {{{ Show the slide containing the outline slide }}} |
|
6 |
|
7 In this tutorial, we shall learn |
|
8 |
|
9 * what are tuples |
|
10 * their similarities and dissimilarities with lists |
|
11 * why are they needed |
|
12 |
|
13 Let`s get started by defining a tuple. A tuple is defined by enclosing |
|
14 parantheses around a sequence of items seperated by commas. It is similar to |
|
15 defining a list except that parantheses are used instead of square brackets. |
|
16 :: |
|
17 |
|
18 t = (1, 2.5, "hello", -4, "world", 1.24, 5) |
|
19 t |
|
20 |
|
21 defines a tuple. The items in the tuple are indexed using numbers and can be |
|
22 accessed by using their position. |
|
23 :: |
|
24 |
|
25 t[3] |
|
26 |
|
27 prints -4 which is the fourth item of the tuple. |
|
28 |
|
29 :: |
|
30 |
|
31 t[1:5:2] |
|
32 |
|
33 prints the corresponding slice |
|
34 |
|
35 This is the behaviour similar as to lists. But the difference can be seen when |
|
36 we try to change an element in the tuple. |
|
37 :: |
|
38 |
|
39 t[2] = "Hello" |
|
40 |
|
41 We can see that, it raises an error saying tuple does not support item |
|
42 assignment. It only implies that tuples are immutable or in simple words, |
|
43 tuples cannot be changed. |
|
44 |
|
45 But what is the use of tuples!!! |
|
46 |
|
47 We shall understand that soon. But let us look at a simple problem of swapping |
|
48 values. |
|
49 |
|
50 {{{ Pause here and try out the following exercises }}} |
|
51 |
|
52 %% 1 %% a = 5 and b = 7. swap the values of a and b |
|
53 |
|
54 {{{ continue from paused state }}} |
|
55 :: |
|
56 |
|
57 a = 5 |
|
58 b = 7 |
|
59 |
|
60 a |
|
61 b |
|
62 |
|
63 We define the two values |
|
64 :: |
|
65 |
|
66 temp = a |
|
67 a = b |
|
68 b = temp |
|
69 |
|
70 a |
|
71 b |
|
72 |
|
73 This is the traditional approach |
|
74 |
|
75 Now let us do it the python way |
|
76 :: |
|
77 |
|
78 a |
|
79 b |
|
80 |
|
81 a, b = b, a |
|
82 |
|
83 a |
|
84 b |
|
85 |
|
86 We see that the values are swapped. |
|
87 This idiom works for different datatypes also. |
|
88 :: |
|
89 |
|
90 a = 2.5 |
|
91 b = "hello" |
|
92 |
|
93 a |
|
94 b |
|
95 |
|
96 Moreover this type of behaviour is straight forward and what you would expect |
|
97 should happen naturally. |
|
98 |
|
99 This is possible because of the immutability of tuples. This process is called |
|
100 tuple packing and unpacking. |
|
101 |
|
102 Let us first see what is tuple packing. Type |
|
103 :: |
|
104 |
|
105 5, |
|
106 |
|
107 What we see is a tuple with one element. |
|
108 :: |
|
109 |
|
110 5, "hello", 2.5 |
|
111 |
|
112 Now it is a tuple with two elements. |
|
113 |
|
114 So when we are actually typing two or more elements seperated by commas, those |
|
115 elements are packed and a tuple is made from them. |
|
116 |
|
117 When you type |
|
118 :: |
|
119 |
|
120 a, b = b, a |
|
121 |
|
122 First the values of b and a are packed into a tuple on the right side and then |
|
123 unpacked into the variables a and b. |
|
124 |
|
125 Immutability of tuples ensures that the values are not changed during the |
|
126 packing and unpacking. |
|
127 |
|
128 {{{ Show summary slide }}} |
|
129 |
|
130 This brings us to the end of the tutorial. |
|
131 we have learnt |
|
132 |
|
133 * How to define tuples |
|
134 * The similarities of tuples with lists, like indexing and iterability |
|
135 * The immutability of tuples |
|
136 * The value swapping idiom in Python |
|
137 * packing and unpacking of tuples |
|
138 |
|
139 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
140 |
|
141 #[Nishanth]: Will add this line after all of us fix on one. |
|
142 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
143 |
|
144 Hope you have enjoyed and found it useful. |
|
145 Thankyou |
|
146 |
|
147 .. Author : Nishanth |
|
148 Internal Reviewer 1 : |
|
149 Internal Reviewer 2 : |
|
150 External Reviewer : |
|
151 |
|
152 Questions |
|
153 ========= |
|
154 |
|
155 1. Define a tuple containing two values. The first being integer 4 and second |
|
156 is a float 2.5 |
|
157 |
|
158 Answer: (4, 2.5) |
|
159 |
|
160 2. If ``a = (5, "Hello", 3.2)``. what is the value of a[2] |
|
161 |
|
162 Answer: 3.2 |
|
163 |
|
164 3. If ``a = 5,`` then what is the type of a |
|
165 |
|
166 a. int |
|
167 #. float |
|
168 #. tuple |
|
169 #. string |
|
170 |
|
171 Answer: tuple |
|
172 |
|
173 4. if ``a = (2, 3)``. What does ``a[0], a[1] = (3, 4)`` produce |
|
174 |
|
175 Answer: Error |
|
176 |
|
177 5. If ``a = ([2, 3], 4, 5)``. What is the value of ``a`` after doing |
|
178 ``a[0].append(6)`` |
|
179 |
|
180 a. ([2, 3, 6], 4, 5) |
|
181 #. Raises an error |
|
182 #. ([2, 3], 4, 5) |
|
183 #. [2, 3, 4, 5, 6] |
|
184 |
|
185 Answer: ([2, 3, 6], 4, 5) |
|
186 |
|
187 6. What does the following code produce:: |
|
188 |
|
189 a = 5 |
|
190 b = "Hello" |
|
191 a, b = b, a |
|
192 print a |
|
193 print b |
|
194 |
|
195 Answer: Hello |
|
196 5 |
|
197 |
|
198 7. ``a = ("hello", "world", 5, 6, 8)``. What is the value of a[1:4] |
|
199 |
|
200 Answer: ("world", 5, 6) |
|
201 |
|
202 8. ``a = (1, 2, 3, 4, 5, 6, 7, 8)``. What is the value of a[1::3] |
|
203 |
|
204 Answer: (2, 5, 8) |
|
205 |
|
206 |
|