|
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 temp = a |
|
66 a = b |
|
67 b = temp |
|
68 |
|
69 a |
|
70 b |
|
71 |
|
72 This is the traditional approach |
|
73 |
|
74 Now let us do it the python way |
|
75 :: |
|
76 |
|
77 a |
|
78 b |
|
79 |
|
80 a, b = b, a |
|
81 |
|
82 a |
|
83 b |
|
84 |
|
85 We see that the values are swapped. |
|
86 This idiom works for different datatypes also. |
|
87 :: |
|
88 |
|
89 a = 2.5 |
|
90 b = "hello" |
|
91 |
|
92 a |
|
93 b |
|
94 |
|
95 Moreover this type of behaviour is straight forward and what you would expect |
|
96 should happen naturally. |
|
97 |
|
98 This is possible because of the immutability of tuples. This process is called |
|
99 tuple packing and unpacking. |
|
100 |
|
101 Let us first see what is tuple packing. Type |
|
102 :: |
|
103 |
|
104 5, |
|
105 |
|
106 What we see is a tuple with one element. |
|
107 :: |
|
108 |
|
109 5, "hello", 2.5 |
|
110 |
|
111 Now it is a tuple with two elements. |
|
112 |
|
113 So when we are actually typing two or more elements seperated by commas, those |
|
114 elements are packed and a tuple is made from them. |
|
115 |
|
116 When you type |
|
117 :: |
|
118 |
|
119 a, b = b, a |
|
120 |
|
121 First the values of b and a are packed into a tuple on the right side and then |
|
122 unpacked into the variables a and b. |
|
123 |
|
124 Immutability of tuples ensures that the values are not changed during the |
|
125 packing and unpacking. |
|
126 |
|
127 {{{ Show summary slide }}} |
|
128 |
|
129 This brings us to the end of the tutorial. |
|
130 we have learnt |
|
131 |
|
132 * How to define tuples |
|
133 * The similarities of tuples with lists, like indexing and iterability |
|
134 * The immutability of tuples |
|
135 * The value swapping idiom in Python |
|
136 * packing and unpacking of tuples |
|
137 |
|
138 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
139 |
|
140 #[Nishanth]: Will add this line after all of us fix on one. |
|
141 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
142 |
|
143 Hope you have enjoyed and found it useful. |
|
144 Thankyou |
|
145 |
|
146 .. Author : Nishanth |
|
147 Internal Reviewer 1 : |
|
148 Internal Reviewer 2 : |
|
149 External Reviewer : |