Python/C3/Getting-started-with-tuples/English-timed
From Script | Spoken-Tutorial
Timing | Narration |
---|---|
0:00 | Hello friends and welcome to the tutorial on "getting started with tuples". |
0:05 | At the end of the tutorial, you will be able to,
|
0:15 | Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with lists". |
0:21 | Let us start our ipython interpreter. |
0:23 | Type ipython and hit Enter. |
0:27 | Let's get started by defining a tuple. |
0:29 | A tuple is defined by enclosing parentheses around a sequence of items separated by commas. |
0:39 | It is similar to defining a list except that parentheses are used instead of square brackets. |
0:45 | So type t is equal to within brackets 1, 2.5, "hello",-4, "world", 1.24,5. |
1:02 | The items in the tuple are indexed using numbers and can be accessed by using their position. |
1:10 | For example, |
1:11 | First you must type t |
1:18 | Then t within square brackets 3. |
1:24 | It prints -4 which is the fourth item of the tuple. |
1:29 | Similarly type t within square brackets 1 colon 5 colon 2 and hit Enter. |
1:40 | It prints the corresponding slice. |
1:42 | This behaviour is similar to that of lists. |
1:46 | But the difference can be seen when we try to change an element in the tuple. |
1:51 | So type t within square brackets 2 is equal to within double quotes Hello, H is capital. |
2:05 | We can see that, it raises an error saying 'tuple object does not support item assignment'. |
2:10 | Tuples are immutable, and hence cannot be changed after creation. |
2:13 | Then, what is the use of tuples? |
2:16 | We shall understand that soon. |
2:19 | But let us look at a simple problem of swapping values. |
2:24 | Pause the video here, try out the following exercise and resume the video. |
2:30 | Given, a is equal to 5 and b is equal to 7. |
2:33 | Swap the values of a and b. |
2:38 | Switch to terminal of solution |
2:40 | Type a is equal to 5, then b is equal to 7 , then type a and then b, you can see the values. |
2:50 | We now create a variable say, temp and swap the values using this variable. |
2:56 | So type temp is equal to a.Then a is equal to b ;then type b is equal to temp. |
3:08 | Then type a. |
3:10 | Then b. |
3:13 | This is the traditional approach |
3:16 | So let us do it the python way. |
3:21 | So type a; then b; then a comma b is equal to b comma a. |
3:33 | then a ; then b gives the output. |
3:38 | We see that the values are swapped. |
3:43 | This idiom works for different data-types also. |
3:46 | So lets type a is equal to 2 point 5; then b within double quotes hello where h is small letter. |
3:59 | Then a comma b is equal to b comma a. |
4:09 | Then a; then b. |
4:13 | Moreover this type of behaviour is something that feels natural and you'd expect to happen. |
4:19 | This is possible because of the immutability of tuples. |
4:22 | This process is called tuple packing and unpacking. |
4:26 | So type 5 comma and hit Enter. |
4:37 | What we see in the tuple is 1 element. |
4:41 | So type 5 comma within double quotes hello where h is small letter comma 2.5. |
4:57 | Now, It is a tuple with 3 elements. |
5:03 | So when we are actually typing 2 or more elements separated by comma the elements are packed into a tuple. |
5:10 | When we type a comma b is equal to b comma a first the value of b and a are packed into a tuple from the right side then unpack into the variables a and b. |
5:21 | Immutability of tuples ensure that values are not changed during the packing and unpacking. |
5:29 | So This brings us to the end of this tutorial. |
5:33 | In this tutorial, we have learnt to, |
5:35 | 1. Define tuples. |
5:36 | 2. Understand the similarities of tuples with lists, like indexing and iterability. |
5:44 | 3. Know about the immutability of tuples. |
5:48 | 4. Swap values, the python way. |
5:52 | 5. Understand the concept of packing and unpacking tuples. |
5:57 | Here are some self assessment questions for you to solve. |
6:01 | 1. Define a tuple containing two values. |
6:04 | The first being integer 4 and second is a float 2.5 |
6:08 | 2. If a = 5, then what is the type of a ? |
6:13 | The options are int , float, tuple, string. |
6:19 | The final question the third one is if a = (2, 3) |
6:25 | What does a[0], a[1] = (3, 4) produce. |
6:34 | And the answers, |
6:38 | 1. A tuple is defined by enclosing parentheses around a sequence of items separated by commas. |
6:44 | Hence, we write our tuple as,within brackets 4 comma 2.5. |
6:53 | 2. Since the given data is 5 followed by a comma, it means that it is a tuple |
7:01 | 3. The operation a within square brackets 0, a within square brackets 1 is equal to within brackets 3 comma 4 will result in an error because tuples are immutable.
|
7:14 | Hope you have enjoyed this tutorial and found it useful. |
7:17 | Thank you! |