Page 118 - Information_Practice_Fliipbook_Class11
P. 118

The conditional expression may not include the use of relational operators. Consider the following examples:
               >>> x = 10
               >>> if x:
                       print("Condition is True")    # if block is executed
               In Python, any non-zero value (other than None) is considered to be Boolean True. In the above example, as the
               value of x is 10, the Boolean expression x yields True. Therefore, the conditional expression of the if statement
               yields True.
               >>> if None:
                       print("Condition is not True")   # if block isn't executed
               In the above  example, the conditional expression None  yields False.  Therefore, the  if  block  following the
               condition None:
               print("Condition is not True")
               is ignored by the Python interpreter.



        5.3.2 if-else statement
        ThtlifltSnStetnSltxtcuSttlnlttqutnctlofltSnStetnStlwhtnlShtlconditonnaltxprtttionlyitadtlTrue.lThtltSnStetnS(t)l
        inlShtliflbaocklit/nrtlignortdlbylShtlPyShonlinStrprtStrlwhtnlShtlconditonnaltxprtttionlyitadtlFalse.lHowtvtr,l
        toettett,lctrSninltSnStetnStlnttdlSolbtltxtcuStdlwhtnlShtlconditonnaltxprtttionlitlFalse.lInltuchlnltiSunton,l
        nn else canuttlitluttd.

        Syntax:
        if <conditional expression>:                                  ………htndtr

               <sequence S1 of statement(s)>                            ………ifltuiStl/lbaock
        else:

               <sequence S2 of statement(s)>                            ………else tuiStl/lbaock




                                               TttSl     Fnatt   tqutnctlofl SnStetnStl
                                             conditon               (elselbaock)


                                                 Trut

                                        tqutnctlofl SnStetnStl
                                            (iflbaock)
                                         Figl5.4:lFaowldingrneloflnnlif-elseltSnStetnSl

        InlShtlgivtnltynSnx,lShtrtlnrtlSwolhtndtrlcanuttt:liflnndlelse.lThtlindtnStdlttqutnctlofltSnStetnStlinlShtlif tuiStl
        (natolcnaatd,lif baock)lwiaalbtltxtcuStdlwhtnlShtlconditonnaltxprtttionlyitadtlTrue.lThtlelseltuiStl(natolcnaatd,l
        elselbaock)lwiaalbtltxtcuStdlwhtnlShtlconditonnaltxprtttionlyitadtlFalse.lContidtrlShtlfoaaowingltxnepat:


         Program 5.1 WriStlnlprogrnelSolchtcklwhtShtrlnlnuebtrltnStrtdlbylnluttrlitltvtnlorlodd.
        Solution:

          01 # To check whether a number entered by a user is even or odd.
          02 num = int(input("Enter a number: "))
          03 if num%2 == 0:
          04     print("Number is EVEN")
          05     print("Division by 2 leaves the remainder Zero")



          104  Touchpad Informatics Practices-XI
   113   114   115   116   117   118   119   120   121   122   123