Skip to main content

Python If Elif and Else Statements

This is an conditional statement of python programming. that are normally used to select a decision at current situation and execute instruction. condition is depend upon given expression. if condition are valid then executed of select statement. this process are change the flow of program execution. that is as follows.

If statement

If statement are check given expression result if result are not zero or not false then control are enter the condition block (if block) and executes block instructions.

python if statement flow chart

so that is an condition execution statement. let take one example.

if(1>-1):
  print("1 is greater of -1")
  #statements
no1=10
no2=20
if(no1!=no2):
  print(no1,"is not equal to ",no2)
  #statements
if(no1==30):
  print("no1 is 30")
  #statements
Output
1 is greater of -1
10 is not equal to  20

Elif statement

This is similar to if statement behaviour. it will work when previous binding if or elif condition are fail. that is also check of a given expression is valid or not. for example.

o1=6
no2=6
if(no1>no2):
  print(no1,"Is Greater Then",no2)
elif(no1==no2): #expression(no1==no2)
  print(no1,"Are Equal To",no2)
elif(no1<no2): #expression(no1<no2)
  print(no1,"Is Less Then",no2)
Output
6 Are Equal To 6

Else statement

else statement is condition less statement. it will automatic execute when previous defined condition (if or if elif pair) are not valid.

if(5>=7):
  print("5>=7")
else:
  print("5<7")
Output
5<7

else are also use in while loop situations.

no1=90
while(no1<10):
  print(no1)
  no1+=1
else:
  print("Else Condition")
Output
Else Condition




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment