Page 156 - Plus_V2.2_C8_Flipbook
P. 156

To create empty list:
                  List1 = [ ]

                  Some other examples of the list are given as follows:
                  To create list of 4 integer elements: L1 = [1, 2, 3, 4]
                  To create list of characters: L1 = [’a’, ’b’, ’c’]

                  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 with mixed data types: L1 = [7, “Hello”, 3.14]

                   Program 11: To print the different types of lists.












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






                       Modifying the List Element

                  Lists are mutable, which means you can change the elements after the list is created without creating
                  a new space for storage of the changed list. You can do this by using an assignment operator (=).

                   Program 12: To change the elements of a list.















                       Traversing a List

                  Traversing means accessing or visiting the elements of a list. Two ways to traverse a list are positive
                  indexing and negative indexing.
                     Index with positive integers: Index from left starts with 0.

                     Index with negative integers: Index from right starts with -1.
                  Syntax for traversing a list:
                  <name of the list>[index]




                  154   Premium Edition-VIII
   151   152   153   154   155   156   157   158   159   160   161