Page 128 - ComputerGenius_V2.1_Class8
P. 128

l    A statement or a collection of statements within the if block is executed only if a certain condition
                     or expression evaluates to True.

                  l   Python interprets non-zero values as True.
                  l   None and 0 are interpreted as False.

                  Syntax:
                  if (Test Expression):
                    Indented statement block

                  # if block ends here

                  Example 1: Python if Statement
                  Program:
                    num = 3
                    if num > 0:
                      print(num,"is a positive number.")

                    print("This is always printed.")
                  Output:
                    3 is a positive number.

                    This is always printed.
                  Description:

                  In the above program, we have checked a condition using ‘If’ Statement. If the statement turns out
                  to be true, the statement after indentation gets executed.

                  Example 2: Python if Statement
                  Program:
                    num = int(input("Enter a number : "))

                    if num > 0: #Condition 1
                        print(num,"is a positive number.")

                    print("This is always printed.")
                  Output 1:
                    Enter a number : 15

                    15 is a positive number.
                    This is always printed

                  Output 2:
                    Enter a number : -5

                    This is always printed.
                  Description:

                  In the above program, we have checked a condition using the ‘If’ Statement. If the statement gets
                  false, the statement after the indentation gets executed.

                      126    Computer Genius (V2.1)-VIII
   123   124   125   126   127   128   129   130   131   132   133