Page 76 - Touhpad Ai
P. 76

21 st
                 AI TASK                                                            Century   #Coding & Computational Thinking
                                                                                     Skills
               Write Python scripts for the following:

               1.  Accept 2 numbers from the user and display the larger number.
















               2.   Accept a number from the user. Check if the digit in the unit’s place is odd or even. Example: if the user enters 452,
                  2 is the digit in the unit’s place and is an even number.






















              Iterative Statements

              Iterative statements are also known as looping statements. A loop is used to execute instructions or a block of code
              multiple times, without writing it repeatedly. A sequence of instructions when repeated for a number of times or until a
              condition is true is called a loop.
                                                                                              for each element
              In Python, there are two types of conditional statements, which are as follows:  in sequence
              The for loop
              The while loop                                                           Last element     True
                                                                                         reached?
              The for Loop
              A for loop is used when you know in advance how many times you                  False
              want to execute a block of code. It iterates over a sequence (such as
              a list, tuple, string, or range) and executes the block of code for each   Execute statements
                                                                                      inside for block        Exit loop
              element in the sequence.
              The for statement executes a simple or compound statement for a fixed number of times.
              The syntax of while loop is as follows:

                 for element in sequence:
                          statements

                 74     Touchpad Artificial Intelligence - XI
   71   72   73   74   75   76   77   78   79   80   81