Page 80 - tp_Modula_v2.0
P. 80

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 can be defined as a sequence of objects arranged in an organised form. 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 can be either homogeneous or heterogenous 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]

                   78     Touchpad MODULAR (Version 2.0)
   75   76   77   78   79   80   81   82   83   84   85