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