Conditional Statements in Python
Conditional Statements in Python
Conditional Statements:
Conditional statements are used to perform different types of ACTIONS. It works depending on
whether a condition is TRUE or FALSE.Mostly with conditional statements we use COMPARIONS
and ARITHMETIC expressions with VARIABLES.
IF STATEMENTS
IF..ELSE STATEMENTS
IF..ELIF..ELSE STATEMENTS
NESTED IF STATEMENTS
IF STATEMENTS
Rules:
The colon (:) is significant and required. It separates the header of the compound
statement from the body.
The line after the colon must be indented. It is standard in Python to use four spaces for
indenting.
All lines indented the same amount after the colon will be executed whenever the
BOOLEAN_EXPRESSION is true.
In python NON-ZERO value interprets as TRUE. NONE and ZERO interprets as FALSE.
Syntax:
if condition:
True Statement Block
Program:
If.else Statements:
The ifelse statement checks the condition, If the condition is TRUE it executes the TRUE
STATEMENT BLOCK otherwise it executes the FALSE statement block.
Syntax:
if condition:
True Statement Block
else:
False Statement Block
Flowchar for if.else:
Program:
Mark = input(Enter your mark)
if(Mark>=50)
print(you are pass) # indentation here 4 space
else:
if.elif.else statement:
The elif can have multiple elif blocks but only one else block.
If.else Flowchart:
Con False
1
True
Con
2 False
True
Execute true Execute false
condition2 stat condition2 stat
Program:
if(Mark>80&&Mark<90)
if(Mark>70&&Mark<80)
else:
Nested if statements:
The ifelifelse statement can be used inside another ifelifelse statement. This is called
nesting in computer programming
Program:
if Mark>=0:
if Mark==0:
else:
else: