Page 71 - Modular v1.1 Pyhton
P. 71

07                           LIST IN PYTHON




















                         Your Aim

                         to learn about:
                                 Creating a List                         Traversing a List
                                  Operations on a List                    List Methods
                                   Python Functions                         Slicing the List
                                    Changing the List Element                  Adding an Element to a List
                                     Some More Programs



                 A list is a sequence of multiple values in a sequence. In a list, each element or value is called an
                 item. List is mutable, which means the items in a list can be modified by assigning new values.
                 It is homogeneous in nature which means a list can contain numeric as well as character data.
                 In this chapter, you will learn about creating a list, operations performed on the list and functions
                 of the list.

                     CREATING A LIST

                 To create a list you need to place all the elements separated by a comma inside a square bracket

                 [ ]. The list can contain any number of elements of different data types such as integer, float,
                 character and so on.
                 A list which contains another list in it is called as nested list.

                 Syntax to create a list:
                 <list_name> = [value1, value 2, value 3, ...]

                 Examples:
                 To create list of 4 integer elements: L1 = [1, 2, 3, 4]

                 To create list of 3 strings: L1 = ["Nikhil", "Dikshant", "Jatin"]

                 To create list of integer and float numbers: L1 = [1, 1.5, 2.5, 7]
                 To create list of characters: L1 = ['a', 'b', 'c']
                 To create empty list: L1 = []

                 To create nested list: L1 = ['Orange', 2.0, 5, [10,20]]

                 To create list with mixed data types: L1 = [7, "Hello", 3.14]

                                                                                                 List in Python   69
   66   67   68   69   70   71   72   73   74   75   76