Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 113 - 2620_Birla Open Mind C-8
P. 113

    Take Off                                                          Century   #Critical Thinking
                                                                                                        21 st
                                                                                                       Skills
                          State whether these statements are true or false.

                          1.  Python does not support conditional statements.

                          2.  We can use if statement inside other if statement.
                          3.  We can check multiple conditions in Python.

                          LOOPING IN PYTHON

                     Sometimes, you need to repeat a task multiple times or you may need to repeat the task either a certain
                     number of times or until a condition is satisfied. In Python, the statements that are used to repeat a
                     set of instructions are called iterative or looping statements. Looping statements are very useful and
                     necessary for developing applications.
                     Python provides two types of looping statements—for and while. The for statement is used to repeat
                     an instruction or a set of instructions a fixed number of times. Whereas, the while statement is used to
                     repeat a set of instructions until a condition evaluates to true. Let’s discuss these statements in brief.

                          THE FOR STATEMENT

                     The for statement executes a simple or compound statement for a fixed number of times. The syntax
                     of the for statement is given below:
                                                                                  for each
                     for <variable> in <iterator>:
                                                                                   item in
                            Statements                                            sequence

                                                                           Last item    True
                                                                           reached?

                                                                                 False

                                                                           Execute the
                                                                            for block

                                                                                   Exit loop
                     Program 1: To print all the days of a week
                         Program1.py

                      File  Edit  Format   Run   Options   Window    Help

                      #Program to print all days of the week

                      days = ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
                      for x in days:
                          print(x)

                                                                                                       #Loops in Python 111

| 108 109 110 111 112 113 114 115 116 117 118 |