If else-elif else nested if Statement

Python conditional statement is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.

Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.

Statement Description

if statements

An if statement consists of a boolean expression followed by one or more statements.

if...else statements

An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE.

nested if statements

You can use one if or else if statement inside another if orelse if statement(s).

Python IF Statement

The if statement contains a logical expression using which data is compared and a decision is made based on the result of the comparison.

if expression:
   statement(s)
#!/usr/bin/python

var1 = 100
if var1:
   print "1 - Got a true expression value"
   print var1

var2 = 0
if var2:
   print "2 - Got a true expression value"
   print var2

print "Last Line!"

Result

1 - Got a true expression value
100
Last Line!

Python IF...ELIF...ELSE Statements

An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement returns 0 or a FALSE value.

Syntax

The syntax of the if...else statement is ?

if expression:
   statement(s)
else:
   statement(s)
#!/usr/bin/python

var1 = 100
if var1:
   print "1 - Got a true expression value"
   print var1
else:
   print "1 - Got a false expression value"
   print var1

var2 = 0
if var2:
   print "2 - Got a true expression value"
   print var2
else:
   print "2 - Got a false expression value"
   print var2

print "Last Line!"

Result

1 - Got a true expression value
100
2 - Got a false expression value
0
Last Line!

The elif Statement

The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

if expression1:
   statement(s)
elif expression2:
   statement(s)
elif expression3:
   statement(s)
else:
   statement(s)

Core Python does not provide switch or case statements as in other languages, but we can use if..elif...statements to simulate switch case as follows ?

 
#!/usr/bin/python

var = 100
if var == 200:
   print "1 - Got a true expression value"
   print var
elif var == 150:
   print "2 - Got a true expression value"
   print var
elif var == 100:
   print "3 - Got a true expression value"
   print var
else:
   print "4 - Got a false expression value"
   print var

print "Last Line!"

Result

3 - Got a true expression value
100
Last Line!

Python nested IF statements

There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if statement.

Syntax:

The syntax of the nested if...elif...else construct may be:

if expression1:
   statement(s)
   if expression2:
      statement(s)
   elif expression3:
      statement(s)
   else
      statement(s)
elif expression4:
   statement(s)
else:
   statement(s)

Result : 

#!/usr/bin/python

var = 100
if var < 200:
   print "Expression value is less than 200"
   if var == 150:
      print "Which is 150"
   elif var == 100:
      print "Which is 100"
   elif var == 50:
      print "Which is 50"
elif var < 50:
   print "Expression value is less than 50"
else:
   print "Could not find true expression"

print "Last Line!"

Result :-

Expression value is less than 200
Which is 100
Last Line!

One line Statement

If the suite of an if clause consists only of a single line, it may go on the same line as the header statement.

#!/usr/bin/python

var = 100
if ( var  == 100 ) : print "Value of expression is 100"
print "Last Line!"

Result

Value of expression is 100
Last Line!

Loading ...

Related Results :

  1. If else-elif else nested if Statement
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author