Page 216 - Robotics and AI class 10
P. 216

Lists in Python

        List is a collection of heterogeneous data arranged in a sequence. The values can be of any data type like string,
        integer, float, objects or even a list. Each value in a list is called an element and its position in a list is referred by an
        index number. The number of elements in a list will make the length of a list. All the elements in a list are separated
        by comma and enclosed in square brackets [ ].
        List is a mutable data type in Python as the content of the list can be added, modified or deleted by the user
        anytime throughout the code. A number of operations can be performed on the list by using a few built-in list
        functions which we will study in detail in this chapter.


        Important Features of Lists
        Based on the above definition, the list has the following important features:
           • It is mutable data type.
           • It is an ordered sequence of values.
           • Each element is separated by comma and enclosed in square brackets [ ].

           • Each value/element is accessed by an index number.
           • The values can be added, modified or deleted by the user throughout the program.
           • It stores the values of different data types.


        Creation of List
        A list is created by enclosing the values within square brackets. Each value/element is separated by a comma and
        stored under a common name. These values can be of different data types. The name follows the rules of naming
        conventions for identifiers as shown below:
                       Examples                                           Commands

         Creating an empty list                list1 = []
         Creating a list with single value     list2 = [56]
         Creating a list of friends            friends = ["Saisha", "Advika", "Arshia"]

         Creating a list of marks              marks=[92, 91, 89, 95, 93]

         Creating a list of students with roll no  students = [21, "Gunjan", 94.5, 34, "Rohit", 91.3]
         and percentage (list of mixed values)
         Creating a list within another list   section = ["A", 25, "English", ["B",34], ["C",37],

                                               56.7]
         Creating a list with repeated values  L1 = [10] * 5   # L1 will have [10, 10, 10, 10, 10]

         Creating a list from another sequence  alpha = list("hello") #string converted to a list
         or string using list() function       #output will [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
         Values input from the user in a list  L1 = list(input("enter numbers :"))


        Access Elements of a List
        The elements of a list can be accessed by using its index number starts with 0 (zero). There are two different ways
        of using the index number:

           • Forward Indexing: The index number begins with 0 till length-1 and we refer the elements from left to right.


              214     Touchpad Robotics & Artificial Intelligence-X
   211   212   213   214   215   216   217   218   219   220   221