PYTHON LIST

 LIST:

       1. Lists are used to store multiple items in a single variable.  

       2.It  is mutable. this means that once defined ,they can be changed. 

       3.List is the most versatile data-type available in Python that can be written as a collection of comma-separated values or items between square brackets.

       4.A single list can contain different Data-Types such as integers ,strings ,as well as objects .

   Example:

 A=[23,'Hello,23.4,'y'']

LIST  OPERATORS:

         1. '+'  OPERATOR  FOR  CONCATENATION:

                Concatenation of lists is an operation where the elements of one list are added at the end of another list

EXAMPLE:

>>>a=[1,2,3]

>>>b=[4,5,6]

>>>a+b

[1,2,3,4,5,6]

>>>

 2. '*'  OPERATOR  FOR REPEATING  THE ELEMENTS:

                           the * operator repeats the items in a list a given number of times.     

    EXAMPLE:

>>>a=[1,2,3]

>>>a*3

[1,2,3,1,2,3,1,2,3]

>>>

LIST   SLICING:

       List slicing refers to accessing a specific portion or a subset of the list for some operation while the original list remains unaffected.

    SYNTAX:

           variable[start : end : step]

    EXAMPLE:

>>>a=[1,2,3,4,5,6]

>>>a[0:6:2]

[1,3,5]

>>>

LIST   METHODS:

        1.APPEND ( ):

                  The append() method in Python adds a single item to the end of the existing list. After appending to the list, the size of the list increases by one.

              EXAMPLE:

>>>a=[a,b,c,d]

>>>a.append(e)

>>>a

[a,b,c,d,e]

>>>

2.INSERT ( ):

           insert() is an inbuilt function in Python that inserts a given element at a given index in a list. Parameters : index - the index at which the element has to be inserted.

          EXAMPLE:

>>>a=[1,2,3,4,5]

>>>a.insert(2,4)

>>>a

[1,2,4,3,4,5]

>>>

3.REMOVE ( ):

            remove() is an built-in function  that removes a given object from the list. The method does not return any value but removes the given object from the list.

         EXAMPLE:

>>>a=[a,b,c,d]

>>>a.remove(c)

>>>a

[a,b,d]

>>>

4.CLEAR ( ):

            The clear() method removes all the elements from a list.

          EXAMPLE:

>>>a=[1,2,3,4,5]

>>>a.clear()

>>>a

[]

>>>

  5.COUNT ( ):

             The count() is a built-in function . It will return the count of a given element in a list. The count() method returns an integer value. 

          EXAMPLE:

>>>a=[a,a,a,b,b,c,c]

>>>a.count(a)

3

>>>

 6.INDEX ( ):

              The index() method return the position value of a given  element.

          EXAMPLE:

>>>a=[1,2,3,4,5,6]

>>>a.index(4)

3

>>>

7.POP ( ):

                 The value at index is popped out and removed. If the index is not given, then the last element is popped out and removed.

          EXAMPLE:

>>>a=[1,2,3,4,5]

>>>a.pop(2)

3

>>>a

[1,2,4,5]

>>>

8.REVERSE ( ):

                  The reverse() method will reverse the list without creating a new list.

          EXAMPLE:

>>>a=[a,b,c,d,e]

>>>a.reverse()

>>>a

[e,d,c,b,a]

>>>

9.SORT ( ):

                    sort() method that modifies the list in-place.

          EXAMPLE:

>>>a=[45,78,93,0,58,54,21]

>>>a.sort()

>>>a

[0,21,45,54,58,78,93]

Comments

Popular posts from this blog

dictionary blog surya

Set blog surya