Page 156 - CodePilot V5.0 C8
P. 156

You can create a blank list by using square brackets with no elements inside.


                   Program 15 A program to create an empty list.


                      Program15.py                                                       Output
                   File  Edit  Format    Run   Options   Window    Help               []

                    student_names = []
                    print(student_names)












                      In Python, a list can have elements of different data types, such as integer, string, boolean and float.





                  ACCESSING A LIST

                  Accessing a list means traversing or visiting the elements of a list. There are various ways to
                  traverse a list, including positive indexing and negative indexing.


                   Program 16 A program to access elements of a list.


                      Program16.py
                   File  Edit  Format   Run    Options   Window    Help


                   # Program: Exam Schedule using Positive and Negative Indexing
                   exam_subjects = ["Math", "Science", "English", "History", "Computer"]
                   print("List of Exam Subjects:", exam_subjects)
                   # Positive Indexing → Accessing subjects from the beginning
                   print("Using Positive Indexing:")
                   print("The first exam subject is:", exam_subjects[0])   # First exam
                   print("The third exam subject is:", exam_subjects[2])   # Third exam

                   # Negative Indexing → Accessing subjects from the end
                   print("Using Negative Indexing:")
                   print("The last exam subject is:", exam_subjects[-1])   # Last exam
                   print("The second last exam subject is:", exam_subjects[-2]) # Second last exam











                   154
                        CodePilot (V5.0)-VIII
   151   152   153   154   155   156   157   158   159   160   161