diff options
-rw-r--r-- | basic_python/list_tuples.rst | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/basic_python/list_tuples.rst b/basic_python/list_tuples.rst index 14eff77..68019ad 100644 --- a/basic_python/list_tuples.rst +++ b/basic_python/list_tuples.rst @@ -1,6 +1,9 @@ Lists and Tuples ================ +Lists +----- + Python provides an intuitive way to represent a group items, called *Lists*. The items of a *List* are called its elements. Unlike C/C++, elements can be of any type. A *List* is represented as a list of comma-sepated elements with square @@ -12,9 +15,12 @@ brackets around them:: Common List Operations ----------------------- +~~~~~~~~~~~~~~~~~~~~~~ + +The following are some of the most commonly used operations on *Lists*. +~~~~~~~~ Indexing ~~~~~~~~ @@ -31,6 +37,7 @@ It is important to note here that the last element of the *List* has an index of -1. +~~~~~~~~~~~~~ Concatenating ~~~~~~~~~~~~~ @@ -42,6 +49,7 @@ Two or more *Lists* can be concatenated using the + operator:: [54, 75, 23, 'write', 67, 'read'] +~~~~~~~ Slicing ~~~~~~~ @@ -88,9 +96,11 @@ the second index:: [1, 5, 9] +~~~~~~~~~~~~~~ Multiplication ~~~~~~~~~~~~~~ + A *List* can be multiplied with an integer to repeat itself:: >>> [20] * 5 @@ -99,6 +109,7 @@ A *List* can be multiplied with an integer to repeat itself:: [42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54] +~~~~~~~~~~ Membership ~~~~~~~~~~ @@ -114,6 +125,7 @@ operator:: False +~~~~~~~~~~~~~~~~~~~~~~~~~~~ Length, Maximum and Minimum ~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -130,6 +142,7 @@ element with the smallest value:: 1 +~~~~~~~~~~~~~~~~~ Changing Elements ~~~~~~~~~~~~~~~~~ @@ -141,6 +154,7 @@ Unlike Strings *Lists* are mutable, i.e. elements of a *List* can be manipulated [1, 3, 9, 7] +~~~~~~~~~~~~~~~~~ Deleting Elements ~~~~~~~~~~~~~~~~~ @@ -155,6 +169,7 @@ An element or a slice of a *List* can be deleted by using the **del** statement: [1, 5, 7] +~~~~~~~~~~~~~~~~ Assign to Slices ~~~~~~~~~~~~~~~~ @@ -177,7 +192,7 @@ deletes a list of elements from the *List*. None, Empty Lists, and Initialization -------------------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ An *Empty List* is a *List* with no elements and is simply represented as []. A *None List* is one with all elements in it being **None**. It serves @@ -193,7 +208,7 @@ no value:: Nested Lists ------------- +~~~~~~~~~~~~ As mentioned earlier, a List can contain elements of any data type. This also implies a *List* can have a *Lists* themselves as its elements. These are @@ -203,7 +218,7 @@ called as *Nested Lists*. There is no limit on the depth of the *Nested Lists*:: List Methods ------------- +~~~~~~~~~~~~ A method is a function that is coupled to an object. More about objects and its methods are discussed in Advanced Python module. In general, a @@ -220,6 +235,7 @@ the *List* methods. Some of the most commonly used *List* methods are as follows: +~~~~~~ append ~~~~~~ @@ -233,6 +249,7 @@ The *append* method is used to append an object at the end of the list:: It is important to note that append changes the *List* in-place. +~~~~~ count ~~~~~ @@ -246,6 +263,7 @@ in a list:: 1 +~~~~~~ extend ~~~~~~ @@ -261,6 +279,7 @@ This is an in-place method. This method is equivalent to using the + operator, b using the + operator returns a new list. +~~~~~ index ~~~~~ @@ -272,6 +291,7 @@ specified as argument:: 3 +~~~~~~ insert ~~~~~~ @@ -286,6 +306,7 @@ argument to the list at the position specified by the first argument:: The *insert* method changes the *List* in-place. +~~~ pop ~~~ @@ -304,6 +325,7 @@ of the element to be removed can be specified as an argument to the The *pop* method changes the *List* in-place. +~~~~~~ remove ~~~~~~ @@ -316,6 +338,7 @@ parameter:: [1, 3, 4, 2, 5, 2] +~~~~~~~ reverse ~~~~~~~ @@ -329,6 +352,7 @@ thing:: ['tim', 'alex', 'guido'] +~~~~ sort ~~~~ @@ -353,7 +377,7 @@ a sorted copy of the list. However the original list is left intact:: List Comprehensions -------------------- +~~~~~~~~~~~~~~~~~~~ List Comprehension is a convenvience utility provided by Python. It is a syntatic sugar to create *Lists*. Using *List Comprehensions* one can create |