Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 147 - 2620_Birla Open Mind C-8
P. 147

    Program1: To print the different types of lists

                         Program1.py
                      File  Edit  Format   Run   Options   Window     Help

                      new_list1=[5, 6, 7, 8]                     #Homogeneous data elements
                      print(new_list1)

                      #changing the elements of list
                      new_list1=[5, "Orange", 15.5]              #Heterogeneous data elements
                      print(new_list1)

                     On running the above program, you will get the following output:
                         Output

                      [5, 6, 7, 8]
                      [5, 'Orange', 15.5]

                     Nested List

                     A list that contains another list is called a nested list. The example of the nested list is given as follows:

                     To create nested list: L1 = [’Orange’, 2.0, 5, [10,20]]
                     Program 2: To print the nested list

                         Program2.py
                      File  Edit  Format  Run   Options  Window    Help

                      L1 = ['Orange', 2.0, 5, [10,20]]
                                                                                      Output
                      print(L1)                                                    ['Orange', 2.0, 5, [10, 20]]

                              Double Tap                                                 Century   #Information Literacy
                                                                                            21 st
                                                                                           Skills

                            1.  Write the syntax to create a list.

                            2.  Write an example of nested list.

                          MODIFYING THE LIST ELEMENTS

                     Lists are mutable, it means you can change the elements after the list is created. You can do this by
                     using an assignment operator (=).

                                                                                                         #List in Python 145

| 142 143 144 145 146 147 148 149 150 151 152 |