CBSE Class 11 Python @ Home

CBSE Class XI: Python If Condition

Class 11: if condition

Python if…else Statement

Decision making is required when we want to execute code only if a certain condition is satisfied.

1.
Python if Statement

Syntax
if (test expression):
      statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if the test expression is True.

If the test expression is False, the statement(s) is not executed.

In Python, the body of if statement is indicated by the indentation. Body starts with an indentation and the first unindented line marks the end.

Python interprets non-zero values as True.

None and 0 are interpreted as False.

 

2.
Python if…else Statement

Syntax of if…else
if (test expression):
     Body of if
else:
     Body of else

The if..else statement evaluates test expression and will execute body of if only when test condition is True.

If the condition is False, body of else is executed. Indentation is used to separate the blocks.

 

3.
Python if…else…if … else

Syntax of if…else … if ….else
if (test expression):
   Body of if
else:
    if (test expression):
        Body of if
   else:
       Body of else

This syntax is used when we have number of conditions. Based on the condition-specific set of statements are executed.

 

4.
Python if…elif…else

Syntax of if…elif…else
if (test expression):
    Body of if
elif (test expression):
    Body of elif
else:
    Body of else

This syntax is used when we have number of condition. Based on the condition specific set of statments are executed.
In this syntax we use elif when we have number pf conditions and there is only one else used.