Page 328 - Artificial Intellegence_v2.0_Class_9
P. 328

To  display  ['O',  'I',  'T']  from  the  word[7:4:-1]    ['O', 'I', 'T']
               above list
                                                or
                                                word[-2:-5:-1]

                                                or
                                                word[7:-5:-1]

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


                       Task


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

           list.append(item)
        where, item can be any value like number, string, list, etc. Examples are:

                                Commands                                            Output

           fruits = ["apple", "banana", "cherries"]               ["apple", "banana", "cherries", "melon"]

           fruits.append("melon")
           print(fruits)

           cars = ["Maruti", "Ford"]                              cars = ["Maruti", "Ford", "Honda", "Hyundai"]
           cars.append("Honda")
           cars.append("Hyundai")

           print(cars)







              326     Touchpad Artificial Intelligence (Ver. 2.0)-IX
   323   324   325   326   327   328   329   330   331   332   333