Page 271 - Computer Science V2.0 Class 11
P. 271
is not at the same indentation level as the statement
print("Number is EVEN")
Instead, it is at a level at which the header
if num%2 == 0:
appears. Therefore, it is not part of the if-block and is executed irrespective of whether the conditional expression
(num%2 == 0) yields True or False. There is no limit on the number of statements that can be included in
the if block. Consider the following code that includes two statements in the if-block.
num = int(input("Enter a number : "))
if num%2 == 0:
print("Number is EVEN")
print("Division by 2 leaves the remainder Zero")
print("Divisibility Check Done")
The conditional expression may not include the use of relational operators. Consider the following examples:
x = 10
if x:
print("Condition is True")
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 True") is ignored by the Python interpreter.
10.3.2 if-else Statement
The if statement executes a sequence of statements when the conditional expression yields True. The if
statement is ignored by the Python interpreter when the conditional expression yields False. However, sometimes,
certain statements need to be executed when the conditional expression is False. In such a situation, an else
clause is used.
Syntax:
if <conditional expression>: ……..header
<sequence S1 of statement(s)> ……. if suite / block
else:
<sequence S2 of statement(s)> …... else suite / block
Conditional Statements 257

