Python/C3/Getting-started-with-tuples/English
Visual Cue | Narration |
---|---|
Show Slide 1
Containing title, name of the production team along with the logo of MHRD |
Hello friends and welcome to the tutorial on "getting started with tuples". |
Show Slide 2
Learning objectives |
At the end of the tutorial, you will be able to,
|
Show Slide 3
Pre-requisite slide |
Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with lists". |
Open the terminal
ipython |
Let us start our ipython interpreter. |
t = (1, 2.5, "hello", -4, "world", 1.24, 5)
t |
Let's get started by defining a tuple. A tuple is defined by enclosing parentheses around a sequence of items separated by commas. It is similar to defining a list except that parentheses are used instead of square brackets. |
t[3] | The items in the tuple are indexed using numbers and can be accessed by using their position. For example, |
t[1:5:2] | It prints -4 which is the fourth item of the tuple Similarly, |
t[2] = "Hello" | 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. |
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. | |
Show Slide 4
Assignment 1 |
Given, a = 5 and b = 7. Swap the values of a and b. |
Continue from paused state Switch to the terminal
a = 5 b = 7 a b |
Switch to terminal of solution |
temp = a
a = b b = temp a b |
We now create a variable say, temp and swap the values using this variable |
a
b a, b = b, a a b |
This is the traditional approach Now let us do it the python way |
a = 2.5
b = "hello" a, b = b, a a b |
We see that the values are swapped. This idiom works for different data-types also. |
5, | 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 |
5, "hello", 2.5 | What we see is a tuple with one element. |
Now it is a tuple with three elements.
So when we are actually typing two or more elements separated 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. | |
Show Slide 5
Summary slide |
This brings us to the end of this tutorial. In this tutorial, we have learnt to,
|
Show Slide 6
Self assessment questions slide |
Here are some self assessment questions for you to solve
|
Show Slide 7
Solution of self assessment questions on slide |
And the answers,
Enumerated list ends without a blank line; unexpected unindent. (4, 2.5)
|
Show Slide 8
Acknowledgment slide |
Hope you have enjoyed this tutorial and found it useful. Thank you! |