python control statement if, if…else, if…elif…else statement with examples by unitdiploma￼
•Concept of Indentation
•if, if…else, if…elif…else statement with examples
•Control statements decides the direction of flow of program execution.
The if statement
•It is used to execute one or more statement depending upon whether condition is true or not.
if condition: if num==1:
•In Python, the body of the if statement is indicated by the indentation. Body starts with an indentation and the first un-indented line marks the end.
•It refers to spaces that are used in the beginning of a statement. The statements with same indentation belong to same group called a suite.
•By default, Python uses 4 spaces but it can be increased or decreased by the programmers.
The if..else statement
•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.
num = 3
if num >= 0:
print(“Positive or Zero”)
•The elif is short for else if. It allows us to check for multiple expressions.
•If the condition for if is False, it checks the condition of the next elif block and so on.
•If all the conditions are False, body of else is executed.
•Only one block among the several if…elif…else blocks is executed according to the condition.
•The if block can have only one else block. But it can have multiple elif blocks.
Body of else
num = 3.4
if num > 0:
elif num == 0: