Page 327 - Artificial Intellegence_v2.0_Class_9
P. 327

• Elements within a range - list[start index : end index]
               • Elements within range using step value - list[start index : end index : step value]
               • Whole list in forward order- list[ : ]
               • Whole list in reverse order - list[ : : -1].
            For example,


            word = ['E', 'D', 'U', 'C', 'A', 'T', 'I', 'O', 'N']
                      Forward Indexing     0    1    2    3    4    5    6    7    8

                                           E    D    U    C    A    T     I   O    N
                                           –9   –8   –7   –6   –5   –4   –3   –2   –1      Backward Indexing


                              Examples                    Commands                        Output

                   To display the whole list        word[:]                   ['E', 'D', 'U', 'C', 'A', 'T', 'I', 'O', 'N']

                                                    or
                                                    word[::]

                                                    or
                                                    word[0:]
                                                    or

                                                    word[-9:]
                   Displays the alternate elements  print(word[::2]           ['E', 'U', 'A', 'I', 'N']
                   of the given list
                                                    )
                   To  display  ['E',  'D',  'U']  from  the  word[0:3]       ['E', 'D', 'U']
                   above list
                                                    or

                                                    word[:3]
                                                    or

                                                    word[-9:-6]
                                                    or

                                                    word[:-6]
                   To  display['C',  'A',  'T']  from  the  word[3:6]         ['C', 'A', 'T']
                   above list
                                                    or

                                                    word[-6:-3]
                                                    or

                                                    word[3:-3]
                                                    or
                                                    word[-6:-3]









                                                                                   Introduction to Python  325
   322   323   324   325   326   327   328   329   330   331   332