Page 217 - Robotics and AI class 10
P. 217

• Negative/Backward Indexing: The index number begins with -1 till -length and we refer the elements from
              right to left.
            For example,
                       L1 = ['P', 'Y', 'T', 'H', 'O', 'N']


                            Forward Indexing      0    1     2    3     4    5
                                                  P    Y     T    H    O     N

                                                 –6    –5   –4    –3   –2   –1       Backward Indexing

            Each element can be accessed by using:
               listname[index number]

                                             Commands                                Output

                           print(L1[0])                                      P
                           print(L1[3])                                      H
                           print(L1[-3])                                     H
                           print(L1[-1])                                     N
                           print(L1)      #print complete list               ['P', 'Y', 'T', 'H', 'O', 'N']

                           print(L1[0], L1[4])                               P O

                        Task                                                             #Coding & Computational Thinking



                 If word = ['E', 'D', 'U', 'C', 'A', 'T', 'I', 'O', 'N']
                 What will be the output of the following instructions:
                 ●  print(word[:1:-2])
                 ●  print(word[-1:])
                 ●  print(word[:-1])





            List Operations
            A list is a collection of items that can be manipulated using various operations. These operations involve adding,
            removing, and changing elements within a list. Learning about list operations helps in organizing and processing
            data effectively, which is essential for basic programming tasks and problem-solving.


            Adding New Elements to a List
            There are three different functions used to add elements in an existing list which are append(), extend() and
            insert(). Let us learn about them in detail.


            The append() Function
            The append() function appends a single element with the given value(any datatype) at the end of the list. We use
            this function in a loop for adding multiple elements. With the single append() function, the length of the list will
            increase by 1. Syntax of the append() function is:
               list.append(item)

                                                         Introduction to Data and Programming with Python  215
   212   213   214   215   216   217   218   219   220   221   222