Python/C3/Getting-started-with-lists/English-timed

From Script | Spoken-Tutorial
Revision as of 12:51, 12 March 2013 by Sneha (Talk | contribs)

Jump to: navigation, search
Timing Narration
0:00 Hello friends and welcome to the tutorial on "Getting started with lists".
0:06 In this tutorial we will be getting acquainted with a python data structure called lists. At the end of this tutorial, you will be able to,
  1. Create lists
  2. Access list elements
  3. Append elements to lists
  4. Delete elements from lists
0:23 So now, List is a compound data type, it can contain data of mutually different data types.
0:32 List is also a sequence data type where all the elements are arranged in a specific order.
0:36 Start the ipython interpreter and first create an empty list with no elements.
0:41 In command ipython.Then type empty is equal to square brackets.
0:53 Then type within brackets empty.
0:59 This is an empty list without any elements.
1:03 Lets define a non-empty list as nonempty is equal to within square brackets and in single quotes spam comma within single quotes eggs comma 100 comma 1.234]
1:24 Thus, the simplest way of creating a list is typing out a sequence of comma-separated values (or items) between two square brackets.
1:34 As we can see, lists can contain different kinds of data.
1:37 In the previous example 'spam' and 'eggs' are strings whereas 100 and 1.234 are integer and float respectively.
1:48 Thus, we can put elements of different data types in lists including lists itself.
1:54 This property makes lists heterogeneous data structures.
1:59 Let us include a list within a list.
2:02 We access an element of a list using its corresponding index.
2:07 So type in command listinlist is equal to square brackets [4,2,3,4],'and', 1, 2, 3, 4.
2:41 So now we can access an element of a list using its corresponding index.
2:49 Index of the first element of a list is 0.
2:53 So for the list nonempty, nonempty[0] gives the first element, nonempty[1] the second element and so on and nonempty[3] the last element.
3:23 Pause the video here, try out the following exercise and resume the video.
3:28 What happens when you do nonempty[-1].
3:34 YOu can switch to terminal and type nonempty -1 in square brackets.
3:44 As you can see you get the data of last element which is 1.234.
3:53 In python negative indices are used to access elements from the end.
3:58 So, -1 gives the last element which is the 4th element , -2 gives second element to last and -4 gives the fourth from the last which, in this case, is the first element.
4:15 So type nonempty -1 nonempty[-2] then nonempty -4.
4:24 We can also append elements to the end of a list using the append function.
4:28 So type nonempty dot append within brackets and single quotes onemore.
4:41 We can see an error
4:46 Then type nonempty ; then type nonempty dot append within brackets 6.
5:09 then again nonempty.
5:15 Please, pause the video here, do the exercise and then continue.
5:21 1. What is the syntax to get the element 'and' in the list,listinlist ?
5:29 2. How would you get 'and' using negative indices?


5:34 So, the solution is on your screen
5:38 As we can see nonempty is appended with 'onemore' and 6 at the end.
5:45 Let us move further.
5:47 We can use len function to check the number of elements in the list.
5:50 Let us find out the length of the list 'nonempty'.
5:54 So type in command len within brackets nonempty .
6:05 Just like we can append elements to a list, we can also remove them.
6:08 There are two ways of doing it. One is by using index.
6:12 So type del within brackets nonempty and square brackets 1.
6:26 The function del deletes the element at index 1, i.e the second element of the list, 'eggs'.
6:34 The other way is removing element by content.
6:37 Lets say one wishes to delete 100 from nonempty list.
6:41 For this, one could use the function remove.
6:55 But what if there were two 100's.
6:57 To check that lets do a small experiment.
7:01 Now, in command nonempty dot append within brackets and single quotes spam.
7:11 Then type nonempty.
7:15 Then type nonempty dot remove within brackets and single quotes spam; then type nonempty for the output.
7:29 If we now check, we will see that the first occurence 'spam' is removed and therefore the function remove removes the first occurence of the element in the sequence and leaves others untouched.
7:39 One should remember this, that while del removes by index number, remove removes on the basis of content being passed on.
7:50 Let us take an example.
7:53 del gives us [1,2,3].
7:59 So type on the terminal k is equal to 1,2 ,1,3 and then type del within brackets and square brackets k and square brackets 2.
8:25 remove will give us [2,1,3].
8:29 Since it deletes the first occurrence of what is returned by x[2] which is 1.
8:37 So type k dot remove and in brackets x and square brackets 2.
8:59 As we have seen an error we have to change x of 2 to k of 2.
9:09 Pause the video here, try out the following exercise and resume the video.
9:14 1. Remove the third element from the list, listinlist.
9:19 2. Remove 'and' from the list, listinlist.


9:24 The solution is on your screen.
9:29 This brings us to the end of this tutorial.
9:30 In this tutorial, we have learnt to, Create lists, Access lists using their index numbers, Append elements to list using the function append
9:40 Delete Element from lists by specifying the index number of the element to be deleted in the del function.
9:47 Then,Delete element from list by content using remove function and final one is Find out the list length using len function.
9:59 Here are some self assessment questions for you to solve
10:02 1. How do you create an empty list?
10:05 2. Can you have a list inside a list ?
10:09 3. How would you access the end of a list without finding its length?


10:15 And the answers,
10:18 1. We create an empty list just by leaving the space inside the square brackets empty.empty=[]
10:30 2. Yes.
10:34 List can contain all the other data types, including list.
10:44 Here is an example list_in_list=[2.3,[2,4,6],'string,'all datatypes can be there']
10:56 3. Using negative indices, we can access the list from the end using negative indices.
11:04 This is an example nonempty = ['spam', 'eggs', 100, 1.234] nonempty[-1]
11:15 Hope you have enjoyed this tutorial and found it useful.
11:19 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha