Page 323 - Artificial Intellegence_v2.0_Class_9
P. 323

Source Code:                                  Flowchart
                                                                            START
            num=1

            sum=0                                                        Sum 0 Num 1
            while num<6:
                sum=sum+num
                                                                              Is     No        Display
                num=num+1                                                  Num < 6?            "Sum"
            print("Sum is ",sum)
            Output:                                                            Yes
                                                                       Sum = sum+num                       STOP
            Sum is 15


                    Lists in Python                                     Num = num+1

            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.


            Creating a 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",
               and percentage (list of mixed values)  91.3]




                                                                                   Introduction to Python  321
   318   319   320   321   322   323   324   325   326   327   328