Python-3.4.3/C3/Getting-started-with-tuples/English-timed

From Script | Spoken-Tutorial
Revision as of 12:43, 4 June 2019 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time
Narration
00:01 Welcome to the spoken tutorial on Getting Started with tuples.
00:07 In this tutorial we will: Understand what tuples are
00:12 Compare tuples with lists
00:15 Know why they are needed and Learn to use them.
00:21 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

00:29 Python 3.4.3 and IPython 5.1.0
00:36 To practise this tutorial, you should know how to

run basic Python commands on the ipython console

and use lists

00:47 If not, see the relevant Python tutorials on this website.
00:53 First we will learn about tuples.
00:57 Tuple is a collection of elements similar to a list.
01:02 Tuple uses parentheses, whereas list uses square brackets.
01:08 Elements of a tuple cannot be changed once it is assigned.
01:13 But in a list, elements can be changed.
01:17 Here are few examples for declaring tuples.
01:21 Inside brackets 1, 2.5 is a tuple with two elements.
01:27 The same can be declared as 1, 2.5.
01:32 Inside brackets 1 comma is a tuple with one element.
01:37 The same can be declared as 1 comma
01:41

Inside brackets again inside brackets 1 comma and outside bracket a comma is a tuple with one tuple as element.

01:52 The same can be declared as inside brackets 1 comma and outside bracket a comma.
01:59 Let us start ipython. Open the terminal.
02:04 Type ipython3 and press Enter.
02:10 From here onwards, remember to press the Enter key after typing every command on the terminal.
02:17 Let's learn to create a tuple.

Type, t is equal to inside brackets 1 comma 2.5 comma inside double quotes hello comma minus 4 comma inside double quotes world comma 1.24 comma 5

02:38 Type, t

It is similar to list except that parentheses are used instead of square brackets.

02:47 At least one comma is mandatory for a tuple.
02:51 The brackets are optional, but should be added for clarity.
02:56 The items in the tuple can be accessed by their index positions.
03:02 Type, t inside square brackets 3
03:07 Type, t inside square brackets 1 colon 5 colon 2

It prints the corresponding slice.

03:16 Now we try to change an element in the tuple.
03:20 Type, t inside square brackets 2 is equal to inside double quotes Hello
03:28 We can see that, it raises an error saying 'tuple object does not support item assignment'.
03:35 It shows, elements of a tuple cannot be changed after it is created.

This is called immutability.

03:44 We can iterate over tuples like lists.
03:48 Type, for x in t colon

print inside brackets x

03:57 It prints each element of the tuple t.
04:01 Pause the video.

Try this exercise and then resume the video.

04:07 Let us look at a simple problem of swapping values.
04:12 Given, a is equal to 5 and b is equal to 7, swap the values of a and b.
04:20 Switch to the terminal for the solution.
04:24 Type, a is equal to 5
04:28 b is equal to 7
04:32 Then type, a

Type, b

04:38 We now create a variable say, temp and swap the values using this variable.


04:45 Type

temp is equal to a

04:49 a is equal to b
04:53 b is equal to temp
04:57 Then type, a

Type, b

05:02 We can see that the values are successfully swapped now.

But this is the traditional approach.

05:10 Now let us do it in the Python way.
05:14 Type

a is equal to 5

b is equal to 7

05:21 a, b is equal to b, a
05:27 Now we will check whether the values are swapped.
05:31 Type, a

Type, b

05:36 We can see that the values are swapped easily.
05:40 We can also do this for different datatypes.
05:44 Type, a is equal to 2.5
05:49 b is equal to inside double quotes hello

a, b is equal to b, a

05:59 Now to check the values, type, a

Type, b

06:07 This is possible because of the immutability of tuples.
06:12 This is called tuple packing and unpacking.
06:16 When you type, a comma b is equal to b comma a
06:21 First, the values of b and a are packed into a tuple on the right side. Then it is unpacked into the variables a and b.
06:32 Let us first see about tuple packing.
06:36 Type, 5 comma

We can see a tuple with one element.

06:43 Type, 5 comma inside double quotes hello comma 2.5
06:49 Now it is a tuple with three elements.
06:53 When we type two or more elements separated by commas, those elements are packed into a tuple.
07:01 Immutability of tuples ensures that the values are not changed during the packing and unpacking.
07:09 This brings us to the end of this tutorial. Let us summarize.
07:15 In this tutorial, we have learnt about, Tuples
07:21 Similarities of tuples with lists
07:25 Immutability of tuples and Concept of Packing and unpacking of tuples.
07:32 Here are some self assessment questions for you to solve.


07:36 First. Define a tuple containing two values as given below. The first value is integer 4 and the second value is float 2.5.
07:50 Second. If we type, a is equal to 5 comma then what is the datatype of a?
07:58 Third. If we type, a is equal to inside brackets 2 comma 3 a inside square brackets 0 comma a inside square brackets 1 is equal to inside brackets 3 comma 4

What is the output?

08:16 And the answers,

First. A tuple is defined by enclosing parentheses around a sequence of items separated by commas. Hence, we write our tuple as, inside brackets 4 comma 2.5

08:32 Second. Since the given data is 5 followed by a comma, it is a tuple
08:39 Third. The given operation will give a TypeError because tuples are immutable.
08:46 Please post your timed queries in this forum.
08:50 Please post your general queries on Python in this forum.
08:55 FOSSEE team coordinates the TBC project.
08:59 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

09:09 This is Priya from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

PoojaMoolya