diff -r 430035b678f7 -r a9b71932cbfa getting-started-with-lists/script.rst.orig --- a/getting-started-with-lists/script.rst.orig Wed Nov 10 12:23:40 2010 +0530 +++ b/getting-started-with-lists/script.rst.orig Wed Nov 10 17:19:54 2010 +0530 @@ -1,361 +1,224 @@ - - - - - - - - - - -
+As you can see you get the last element which is 1.234. -
-

Objective Questions

- -
    -
  1. How do you create an empty list?

    -
    -empty=[]
    -
    -
  2. -
  3. What is the most important property of sequence data types like lists?

    -

    The elements are in order and can be accessed by index numbers.

    -
  4. -
  5. Can you have a list inside a list ?

    -

    Yes,List can contain all the other data types, including list.

    -

    Example: -list_in_list=[2.3,[2,4,6],'string,'all datatypes can be there']

    -
  6. -
  7. What is the index number of the first element in a list?

    -

    0 -nonempty = ['spam', 'eggs', 100, 1.234] -nonempty[0]

    -
  8. -
  9. How would you access the end of a list without finding its length?

    -

    Using negative indices. We can the list from the end using negative indices.

    -

    :: -nonempty = ['spam', 'eggs', 100, 1.234] -nonempty[-1]

    -
  10. -
  11. What is the function to find the length of a list?

    -

    len

    -
  12. -
  13. Delete the last element from list sq=[5,4,3,2,1,0]

    -

    del(sq[-1])

    -
  14. -
  15. How many will you have to use remove function to remove all 6's from the given list sq=[2,5,6,7,6,4,6]?

    -

    3

    -
  16. -
-
-
-

Larger Questions

- -

1. Add all elemets of seq1=['e','f','g','h'] -to the sequence seq=['a','b','c','d']

-
    -
  1. Delete all elements of seq1=[3,5,6] from sequence -seq=[1,2,3,4,5,6,7,8,9]
  2. -
-
-
- - +In python negative indices are used to access elements from the end:: + + nonempty[-1] + nonempty[-2] + nonempty[-4] + +-1 gives the last element which is the 4th element , -2 second to last +and -4 gives the fourth from last element which is first element. + +We can append elements to the end of a list using append command. :: + + nonempty.append('onemore') + nonempty + nonempty.append(6) + nonempty + +Following are exercises that you must do. + +%% %% What is the syntax to get the element 'and' +in the list,listinlist ? + + +%% %% How would you get 'and' using negative indices? + +Please, pause the video here. Do the exercise and then continue. + +The solution is on your screen + + +As we can see non empty appends 'onemore' and 6 at the end. + +Using len function we can check the number of elements in the list +nonempty. In this case it 6 :: + + len(nonempty) + + + +Just like we can append elements to a list we can also remove them. +There are two ways of doing it. One is by using index. :: + + del(nonempty[1]) + + + +deletes the element at index 1, 'eggs' which is the second element of +the list. The other way is removing element by content. Lets say one +wishes to delete 100 from nonempty list the syntax of the command +should be + +.. #[[Anoop: let x = [1,2,1,3] + now x.remove(x[2]) + still x is [2,1,3] so that is not the way to remove + element by index, it removed first occurrence of 1(by + content) and not based on index, so make necessary + changes]] + +:: + + nonempty.remove(100) + +but what if there were two 100's. To check that lets do a small +experiment. :: + + nonempty.append('spam') + nonempty + nonempty.remove('spam') + nonempty + +If we check now we will see that the first occurence 'spam' is removed +thus remove removes the first occurence of the element in the sequence +and leaves others untouched. + + + + + +.. #[[Anoop: does it have two spams or two pythons?]] + +.. #[[Anoop: there are no exercises/solved problems in this script, + add them]] + +Following are exercises that you must do. + +%% %% Remove the third element from the list, listinlist. + +%% %% Remove 'and' from the list, listinlist. + +Please, pause the video here. Do the exercise and then continue. + + + +{{{Slide for Summary }}} + + +In this tutorial we came across a sequence data type called lists. :: + + * We learned how to create lists. + * How to access lists. + * Append elements to list. + * Delete Element from list. + * And Checking list length. + + + +{{{ show Sponsored by Fossee Slide }}} + +This tutorial was created as a part of FOSSEE project. + +I hope you found this tutorial useful. + +Thank You + +.. + * Author : Amit Sethi + * First Reviewer : + * Second Reviewer : Nishanth