Page 411 - Ai_417_V3.0_C9_Flipbook
P. 411

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

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

                                                        print(word[-6:-3])
                                                        or
                                                        print(word[3:-3])
                     To  display  ['O',  'I',  'T']  from  the  print(word[7:4:-1])  ['O', 'I', 'T']
                     above list                         or

                                                        print(word[-2:-5:-1])
                                                        or
                                                        print(word[7:-5:-1])
                                                        or

                                                        print(word[-2:4:-1])
                     To display the list in reverse order  print(word[::-1])         ['N', 'O', 'I', 'T', 'A', 'C', 'U', 'D', 'E']


                               Task                                            #Experiential Learning


                     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])



                 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. The syntax of the append( ) function is:

                    list.append(item)
                 where, item can be any value like number, string, list, etc. For example,



                                                                                        Introduction to Python  409
   406   407   408   409   410   411   412   413   414   415   416