Page 155 - CodePilot V5.0 C8
P. 155

LISTS

                 In Python, a list is an ordered collection of items that can store multiple values in a single variable.
                 It is a mutable data type, which means its elements can be altered after the list is created. Each
                 element or value is called an item.

                 CREATING A LIST

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

                 The syntax for creating a list is as follows:

                 list_name = [value1, value2, value3, ...]
                  Program 14 A program to demonstrate the creation of a list.


                     Program14.py
                  File  Edit  Format   Run    Options   Window    Help

                  # List of strings: Names of students
                  student_names = ["Anita", "Rahul", "Sneha", "Vikas", "Rohit"]
                  print("Student Names:", student_names)

                  # List of integers: Ages of students
                  student_ages = [15, 16, 17, 18, 19]
                  print("Student Ages:", student_ages)
                  # List of characters: Section initials of students
                  student_sections = ['A', 'B', 'C', 'D', 'E']
                  print("Student Sections:", student_sections)
                  # List of floats: Exam scores of students
                  exam_scores = [85.5, 90.2, 78.9, 92.1, 88.6]
                  print("Exam Scores:", exam_scores)




                 The output of the preceding code would be as follows:
                     Output

                  Student Names: ['Anita', 'Rahul', 'Sneha', 'Vikas', 'Rohit']
                  Student Ages: [15, 16, 17, 18, 19]
                  Student Sections: ['A', 'B', 'C', 'D', 'E']
                  Exam Scores: [85.5, 90.2, 78.9, 92.1, 88.6]




                 Empty List
                                  If you made a team of heroes and creatures in Python lists, who would you choose and
                                  how would you change it to stay strong in battles?





                                                                                                                  153
                                                                                            Step Ahead with Python
   150   151   152   153   154   155   156   157   158   159   160