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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
.. Objectives
.. ----------
.. At the end of the tutorial, you will
.. #. have a clear understand of what tuples are
.. #. be able to compare them with lists
.. #. know why they are needed and where to use them
.. Prerequisites
.. -------------
.. 1. Getting started with lists
.. Author : Nishanth Amuluru
Internal Reviewer : Punch
External Reviewer :
Language Reviewer : Bhanukiran
Checklist OK? : <put date stamp here, not OK> [2010-10-05]
Script
------
.. L1
{{{ Show the first slide containing title, name of the production
team along with the logo of MHRD }}}
.. R1
Hello friends and welcome to the tutorial on "getting started with
tuples".
.. L2
{{{ Show the slide containing the objectives }}}
.. R2
At the end of the tutorial, you will be able to,
1. Understand of what tuples are.
#. Compare them with lists.
#. Know why they are needed and where to use them.
.. L3
{{{ Switch to the pre-requisite slide }}}
.. R3
Before beginning this tutorial,we would suggest you to complete the
tutorial on "Getting started with lists".
.. R4
Let us start our ipython interpreter.
.. L4
{{{ Open the terminal }}}
::
ipython
.. R5
Let's get started by defining a tuple. A tuple is defined by enclosing
parentheses around a sequence of items seperated by commas. It is
similar to defining a list except that parentheses are used instead of
square brackets.
.. L5
::
t = (1, 2.5, "hello", -4, "world", 1.24, 5)
t
.. R6
The items in the tuple are indexed using numbers and can be
accessed by using their position.For example,
.. L6
::
t[3]
.. R7
It prints -4 which is the fourth item of the tuple
Similarly,
.. L7
::
t[1:5:2]
.. R8
It prints the corresponding slice
This behaviour is similar to that of lists. But the difference can be
seen when we try to change an element in the tuple.
.. L8
::
t[2] = "Hello"
.. R9
We can see that, it raises an error saying 'tuple object does not support
item assignment'. Tuples are immutable, and hence cannot be changed after
creation.
Then, what is the use of tuples?
We shall understand that soon. But let us look at a simple problem of
swapping values.
Pause the video here, try out the following exercise and resume the video.
.. L9
.. L10
{{{ Show slide with exercise 1 }}}
.. R10
Given, a = 5 and b = 7. Swap the values of a and b.
.. R11
Switch to terminal fo solution
.. L11
{{{ continue from paused state }}}
{{{ Switch to the terminal }}}
::
a = 5
b = 7
a
b
.. R12
We now create a variable say, temp and swap the values using this variable
.. L12
::
temp = a
a = b
b = temp
a
b
.. R13
This is the traditional approach
Now let us do it the python way
.. L13
::
a
b
a, b = b, a
a
b
.. R14
We see that the values are swapped. This idiom works for different
data-types also.
.. L14
::
a = 2.5
b = "hello"
a, b = b, a
a
b
.. R15
Moreover this type of behaviour is something that feels natural and
you'd expect to happen.
This is possible because of the immutability of tuples. This process is
called tuple packing and unpacking.
Let us first see what is tuple packing. Type
.. L15
::
5,
.. R16
What we see is a tuple with one element.
.. L16
::
5, "hello", 2.5
.. R17
Now it is a tuple with three elements.
So when we are actually typing two or more elements seperated by commas,
those elements are packed into a tuple.
When you type
a, b = b, a
First the values of b and a are packed into a tuple on the right side
and then unpacked into the variables a and b.
Immutability of tuples ensures that the values are not changed during the
packing and unpacking.
.. L17
.. L18
{{{ Show summary slide }}}
.. R18
This brings us to the end of this tutorial.In this tutorial,
we have learnt to,
1. Define tuples.
#. Understand the similarities of tuples with lists, like indexing and
iterability.
#. Know about the immutability of tuples.
#. Swap values, the python way.
#. Understand the concept of packing and unpacking of tuples.
.. L19
{{{Show self assessment questions slide}}}
.. R19
Here are some self assessment questions for you to solve
1. Define a tuple containing two values. The first being integer 4 and
second is a float 2.5
2. If ``a = 5,`` then what is the type of a ?
- int
- float
- tuple
- string
3. if ``a = (2, 3)``. What does ``a[0], a[1] = (3, 4)`` produce
.. L20
{{{solution of self assessment questions on slide}}}
.. R20
And the answers,
1. A tuple is defined by enclosing parentheses around a sequence of
items seperated by commas.Hence, we write our tuple as,
::
(4, 2.5)
2. Since the given data is 5 followed by a comma, it means that it is
a tuple
3. The operation a[0], a[1] = (3, 4) will result in an error because
tuples are immutable.
.. L21
{{{ Show the thankyou slide }}}
.. R21
Hope you have enjoyed this tutorial and found it useful.
Thank you!
|