Page 283 - Ai_C10_Flipbook
P. 283

Conditional Statements

                 Conditional statements are used for selecting the block of statements to be executed based on the condition. We
                 specify the condition in the program which evaluates to either True or False. If condition is True then the block
                 of statements written for True will be executed and in case the condition is False then the block of statements
                 for False will be executed. It is also called branching as a program decides which statement to execute based on
                 the result of the evaluated condition. In Python:
                    • A block is identified by using indentation (minimum 1 space). Ensure that all statements in one block are
                   indented at the same level.

                    • We use if to do conditional programming.
                    • Conditions with the if statements can also be written within (). It is totally optional.
                    • else statement is optional and if required can be only one else statement in one if block.
                    • It is must to specify colon (:) after writing a condition with the if statement.

                    • The number of statements (also called as a block) written within if can be of any size.
                 Let us understand the flow of conditional statements:

                 if (Condition):
                                                                       Num=int(input("Enter the number: "))
                           statements to be executed if a condition evaluates
                                                                       if Num>9 and Num<100:
                       to True
                                                                              print("Two Digit Number")
                          always indented
                 if (Condition):                                       Num=int(input("Enter the number: "))
                         Statements for True will be executed          if Num>0:
                 else:                                                        print("Positive Number")
                                                                       else:
                      Statements for False will be executed
                                                                              print("Negative Number")

                 if (Condition):                                       Num=int(input("Enter the number: "))
                        Statements for condition1 will be executed     if Num%2==0:
                 elif (Condition):                                            print("Even Number")
                        Statements for condition2 will be executed
                                                                       elif Num%3==0:
                 elif (Condition):
                                                                              print("Multiple of 3")
                        Statements for condition2 will be executed
                                                                       elif Num%5==0:
                       .
                                                                              print("Multiple of 5")
                       .
                       .                                               else:
                 else:                                                        print("Invalid Number")
                         Statements executed when all above conditions are
                      False


                 Few examples of conditional statements:

                 [1]:   Age=int(input("Enter the age: "))
                        if Age>18:
                            print("Eligible for Voting")
                        else:
                            print("Not Eligible for Voting")

                        Enter the age:  25
                        Eligible for Voting




                                                                                    Advance Python (Practical)  281
   278   279   280   281   282   283   284   285   286   287   288