Python-3.4.3/C3/Sequence-datatypes/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time
Narration
00:01 Welcome to the tutorial on Sequence data types.
00:06 In this tutorial, we will learn about-

Sequence Data types such as List , String and Tuple

00:16 Accessing the above data types using index
00:20 Convert list to tuple and vice-versa and
00:24 Convert string to list and vice-versa.
00:28 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

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

run basic Python commands on the ipython console

00:50 If not, see the relevant Python tutorials on this website.
00:55 Sequence data types are those in which elements are kept in a sequential order.
01:01 The elements are accessed using index numbers.
01:05 The sequence data types in Python are

list , string and tuple

01:14 First let us understand what is list.
01:18 List is a container that holds a number of objects in the given order.
01:24 For example: num underscore list is equal to inside square brackets 1, 2, 3, 4
01:32 A list can store data of any type.
01:36 List is most useful when storing data of identical type.

Examples: Name of books in a library and Passengers on a flight

01:49 Let us start ipython3 pylab.

Open the terminal.

01:54 Type ipython3 space hyphen hyphen pylab and press Enter.
02:02 From here onwards, remember to press the Enter key after typing every command on the terminal.
02:09 Now we will create our first list.

Type, num underscore list is equal to inside square brackets 1, 2, 3, 4

02:20 Type, num underscore list
02:24 Comma-separated items enclosed in square brackets constitute a list.
02:30 We can have a list something like this.
02:34 Type, var underscore list is equal to inside square brackets 1, 1.2, inside square brackets 1,2
02:45 Type, var underscore list
02:48 Now let us look at another sequence data type, strings.
02:53 Type greeting underscore string is equal to inside double quotes hello
03:00 Type greeting underscore string
03:04 It is now a string variable with the value hello.
03:08 Python strings can actually be defined in three different ways as follows.
03:14 k is equal to inside single quotes Single quote
03:19 l is equal to inside double quotes Let's see how to include a single quote
03:26 m is equal to inside triple quotes This is another “example” for string
03:33 Here single, double and triple quotes are used as delimiters.
03:40 The last in the list of sequence data types is tuple.
03:45 Type person underscore tuple is equal to inside brackets 17 comma inside double quotes Ram comma 56.8
03:56 This tuple contains age,name and weight of a person.
04:01 To create a tuple we use normal brackets unlike square brackets for list.
04:07 Next, let us see how to access the list using index numbers.
04:12 Type, num underscore list inside square brackets 2
04:18 num underscore list inside square brackets -1
04:23 Recall that, the index of the elements starts with 0 instead of 1.
04:29 Now let us access the string elements.
04:33 Type, greeting underscore string inside square brackets 1
04:39 greeting underscore string inside square brackets 3
04:44 greeting underscore string inside square brackets -2
04:49 We can see that the output is displayed as per the index value.
04:54 Next let us access tuple elements.
04:58 Type person underscore tuple inside square brackets 2
05:04 person underscore tuple inside square brackets -3
05:10 The output shows the weight and age of the person as per the index value.
05:16 Next, we will see about how to add sequences.
05:21 Type num underscore list plus var underscore list
05:27 Addition gives a new sequence containing both the sequences.
05:32 Likewise, we will do for string data type.
05:36 Type a underscore string is equal to inside double quotes space another string
05:44 greeting underscore string plus a underscore string
05:49 We can see that both the strings are added now.
05:53 Next we will see for tuple.
05:56 Type, t2 is equal to inside brackets inside double quotes Student comma
06:04 Adding a comma after Student is important to make t2 a tuple.
06:10 Type, person underscore tuple plus t2
06:15 Now we can see that Student is added to the tuple.
06:20 Next let us see how to find the length of a variable.
06:25 We use len function for that.

Type, len inside brackets num underscore list

06:34 The output shows the number of objects of the list.
06:38 Type, len inside brackets greeting underscore string
06:44 len inside brackets person underscore tuple
06:49 Next we will learn to check the presence of an element using the 'in' keyword
06:55 Type, 3 in num underscore list
06:59 Inside single quotes H in greeting underscore string
07:04 Inside double quotes Sita in person underscore tuple
07:09 We can see that the output displays True and False accordingly.
07:14 Next we will find the maximum and minimum values.
07:19 We use max function to find the maximum value.
07:23 Type, max inside brackets num underscore list
07:28 The min function is used to find minimum value.
07:33 Type, min inside brackets greeting underscore string
07:38 For string data type, ASCII value is used to get the maximum and minimum values.
07:45 Next we will find the sum of a list.

Type, sum inside brackets num underscore list

07:54 So far we have talked about many similar features of list, string and tuple.
08:01 But there are many important features in list that differ from string and tuple.
08:08 Let us see them using some examples.
08:12 Type, num underscore list inside square brackets 1 is equal to 9
08:20 num underscore list

Here the value at index 1 is assigned to 9.

08:28 Type, greeting underscore string inside square brackets 1 is equal to inside single quotes k
08:37 The first command executes without a problem.

But there is an error in the second one.

08:45 Now let us try it in tuple.

Type person underscore tuple inside square brackets 0 is equal to 23

08:56 We can see the same error again.

This is because strings and tuples cannot change the value at a particular index.

09:05 But we can convert list to tuple and tuple to list.
09:10 First let us learn to convert list to tuple.

Type as shown

09:19 t is equal to tuple inside brackets list underscore tuple

Tuple function converts the list to tuple.

09:29 Type, t

Now the list is successfully converted to tuple.

09:36 Next let us learn to convert tuple to list.

Type as shown.

09:44 Type, l is equal to list inside brackets tuple underscore list
09:51 List function converts the tuple to list.
09:55 Type, l
09:58 Now the tuple is successfully converted to list.
10:02 Next let us learn to convert string to list and list to string.

Let us say we have the following string.

10:12 Type otherstring dot split inside brackets inside single quotes comma
10:19 This produces a list with the string split at comma.
10:23 join function does the opposite.

It joins a list to make a string.

10:29 Let us say we have the following list.
10:33 Type, inside single quotes comma dot join inside brackets otherlist
10:41 Thus we get a list joined on commas.
10:45 Similarly we can do on spaces.
10:49 Type as shown.
10:52 Note that the list has to be a list of strings to apply join operation.
10:58 Type, inside single quotes space dot join inside brackets spacestring
11:06 We can see that the list is joined on spaces.
11:10 Pause the video.

Try this exercise and then resume the video.

11:16 Check if 3 is an element of the following list.
11:21 Change the third element in the list to 21.
11:25 Switch to the terminal for the solution.
11:29 Type, l is equal to inside square brackets 1,7,5,3,4
11:37 3 in l

The output is True. So the element 3 is in the list.

11:45 Type, l inside square brackets 3 is equal to 21
11:51 Type, l

We can see that the element 3 is changed to 21.

11:58 Pause the video.

Try this exercise and then resume the video.

12:04 Convert the string "Elizabeth is queen of England" to "Elizabeth is queen"
12:10 Switch to the terminal for the solution.
12:14 Type s is equal to inside double quotes Elizabeth is queen of england
12:21 stemp is equal to s.split open and close brackets
12:28 s is equal to inside single quotes space dot join inside brackets stemp inside square brackets colon 3
12:40 Type, s

We got the required output.

12:46 This brings us to the end of this tutorial.
12:50 In this tutorial, we have learnt to,

Use the sequence data types such as List, String and Tuple

12:59 Split and join a list using split and join functions respectively
13:05 Convert list to tuple and vice-versa and
13:09 Convert string to tuple and vice-versa
13:13 Here are some self assessment questions for you to solve
13:17 First. What is the major difference between tuple and list?
13:22 Second. Split the below string on whitespaces
13:27 And the answers,

First. The major difference between tuple and list is that,

13:34 Tuple cannot change the value at a particular index
13:38 But list can change the value
13:41 Tuple is used to store data related to an item
13:46 But list is typically used to store items of identical type
13:51 Second. To split the string on whitespace, we use the split function without any argument
13:58 Please post your timed queries in this forum.
14:02 Please post your general queries on Python in this forum.
14:07 FOSSEE team coordinates the TBC project.
14:11 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

14:19 This is Priya from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

PoojaMoolya