Page 130 - Computer Genius Class 08
P. 130
Syntax:
if (Condition1):
Statement1
. . . . . . . . . .
else:
Statement2
. . . . . . . . . .
Example 3: Python if-else Statement
num = 3
if num > 0:
print("Positive number.")
else :
print("N egative number.")
Output:
Positive number.
Description:
In the above program, we checked whether a number is greater than zero. If the condition gets true:
The ‘If’ statement gets executed, otherwise, the ‘else’ statement will be executed.
Example 4: Python if-else Statement
num = 3
if num > 0: # Condition 1
print(num,"is a positive number.")
else :
print("This is always printed.")
num = - 1
if num > 0: # Condition 2
print(num,"is a positive number.")
else :
print("This number not greater than 0")
Output:
3 is a positive number.
This number not greater than 0.
Description:
In the above program, two numbers have been used for the execution of both ‘if’ and ‘else’ Statement.
if...elif...else Statement
The if…elif…else ladder is another type of if statement. It helps us to test multiple conditions and
follows a top-down approach. The ‘elif’ is short for ‘else if’.
128 Computer Genius-VIII

