Page 270 - CTS - CSA TP - Volume 2
P. 270

COMPUTER SOFTWARE APPLICATION - CITS














           Iterating Through Lists:
           You can use loops to iterate through the elements of a list.










           List Comprehensions:
           Python supports concise ways to create lists using list comprehensions.









           Nested Lists:
           Lists can contain other lists, creating nested structures.










           Here are five tasks  demonstrating different aspects of Python lists:



           TASK 1: Basic List Operations
           # Creating a list
           fruits = [“Grapes”, “Banana”, “Orange”,]
           # Accessing elements

           print(fruits[0])      # Output: Grapes
           # Modifying list
           fruits.append(“Apple”) # Adding a new element
           fruits[1] = “Kiwi”     # Modifying an element
           # Iterating through the list

           for fruit in fruits:
           print(fruit)
           Output:







                                                           255
                              CITS : IT & ITES - Computer Software Application - Exercise 128
   265   266   267   268   269   270   271   272   273   274   275