Page 406 - Ai_417_V3.0_C9_Flipbook
P. 406

Commands                                           Output

                  #display the digits of a number in a reverse order                    Enter a number 594

                  num=int(input("Enter a number "))                                     495
                  while num>0:

                        r=num%10
                        print(r, end="")

                        num//=10

                Program 11:  To print the sum of first five natural numbers.

              Algorithm
               Step 1   Start
               Step 2   num=1

               Step 3   sum=0                                Flowchart
                                                                              START
               Step 4   while num is less than 6
                            sum=sum + num
                                                                          Sum=0, Num=1
                            num = num + 1
               Step 5   Display "Sum is ", sum

               Step 6   Stop                                                    Is      No       Display
              Source Code:                                                   Num < 6?            "Sum"

              num=1                                                               Yes

              sum=0                                                      Sum = Sum + Num                     STOP
              while num<6:

                  sum=sum+num                                             Num = Num + 1
                  num=num+1
              print("The sum of first five natural numbers is",sum)
              Output:

              The sum of first five natural numbers is 15

                       Lists in Python


              A list in Python is a collection of items that are ordered and mutable (changeable). It can contain elements of
              different data types, such as strings, integers, floats, objects, or even other lists. Each value in a list is called an
              element, and each element is indexed based on its position in the list. The index starts at 0 for the first element,
              1 for the second element, and so on. The number of elements in a list determines its length. Lists are defined by
              enclosing the elements within square brackets [], with each element separated by a comma. For example,

              my_list = [1, 2, 3, 'hello', 5.0]
              Lists are mutable, meaning that you can change the elements in a list after it has been created. You can add
              elements to a list, modify existing elements, or remove elements from a list.



                    404     Touchpad Artificial Intelligence (Ver. 3.0)-IX
   401   402   403   404   405   406   407   408   409   410   411