Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 38 - 2501_KVS_C-7
P. 38

    Syntax:   if<condition>:
                                    Statement1            if section Body

                                    Statement2            (Block 1)

                                    …  .
                                else:
                                                          else section Body
                                    Statement1
                                                          (Block 2)
                                    Statement2
                                    …  .
                      3.2.3 Practice

                      Let's understand it with an example.

                      Program 6: Program to check whether inputted number is even or odd.
                      You will write the following code.

                      Code:

                      Num=int(input("Enter a number"))
                      if Num%2==0:
                                                                           Executes when condition is True.
                             print("Number is Even")

                      else:
                                                                           Executes when condition is False.
                             print("Number is Odd")

                      You can observe, we have applied a condition Num%2==0. Num%2 returns 0 if the number
                      is divisible by 2. If the result is 0, it’s an even number. Otherwise, it’s an odd number.

                      We can write single statements or multiple statements in any part of a decision statement.

                      Program 7: Program to check whether a person is eligible to vote or not.

                      Code:

                      Age=int(input("Enter your age"))
                      if Age>=18:
                             print("You are eligible to vote.")
                      else:

                             print("You are not eligible to vote.")
                      Program 8: Program to check whether given angle is straight angle or right angle.
                      Code:

                      angle = int(input("Enter an angle: "))
                      # Checking if the angle is a right angle (90 degrees)

                      if angle == 90:

                    36                         KVS DELHI REGION 2025

| 33 34 35 36 37 38 39 40 41 42 43 |