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

COMPUTER SOFTWARE APPLICATION - CITS























           TASK 2: List Slicing
           # Creating a list
           numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

           print (“\n Slicing the list from 2:7: “)
           # Slicing the list
           subset = numbers[2:7]   # Output: [3, 4, 5, 6, 7]
           foriin subset :
           print (i, end=” “)
           print (“\n Odd Number slicing from the list:”)

           # Step in slicing
           even_numbers = numbers[1::2]  # Output: [2, 4, 6, 8]
           foriineven_numbers :
           print (i, end=” “)

           Output:



















           TASK 3: List Comprehension
           # Creating a list
           original_numbers = [1, 2, 3, 4, 5]

           # Using list comprehension to create a new list
           squared_numbers = [x**2for x inoriginal_numbers]
           print(“Original Numbers:”, original_numbers)
           print(“Squared Numbers:”, squared_numbers)





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