python : If else example

Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.

Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.

Lets have an simple work flow.

ifelse statement
ifelse statement

Now we will see how this can be achieved in python program and what is the syntax and how to write if else statement.

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.

Example: 

#!/usr/bin/python

test = 500

if ( test == 500 ) : print “Value of expression is 500”

print “Good bye!”

Output:

Value of expression is 500
Good bye!

if else statement python
if else statement python
  • if Statement

If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed. If boolean expression evaluates to FALSE, then the first set of code after the end of the if statement(s) is executed.

Syntax:

if expression:
statement(s)

Example:

#!/usr/bin/python

test = 500
if test:
print “1 – Got a true expression value”
print test

test2 = 0
if test2:
print “2 – Got a true expression value”
print test2
print “Good bye!”

Output:

1 – Got a true expression value

500

  • if else statement

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 resolves to 0 or a FALSE value.

The else statement is an optional statement and there could be at most only one else statement following if .

Please Note: Python does not provide switch case statement as other language. You can use if elif else statement at that place in python.

Syntax:

—————if else———————————————

if expression:
statement(s)
else:
statement(s)

—————if elif else——————————————-

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

Example for first syntax (Simple if else statement):

#!/usr/bin/python

test3 = 500
if test3:
print “1 – Got a true expression value”
print test3
else:
print “1 – Got a false expression value”
print test3

test4 = 0
if test4:
print “2 – Got a true expression value”
print test4
else:
print “2 – Got a false expression value”
print test4

print “Good bye!”

Output:

1 – Got a true expression value
500
2 – Got a false expression value
0
Good bye!

Example for second syntax (Simple if elif else statement):

#!/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 “Good bye!”

Output:

3 – Got a true expression value
100
Good bye!

  • Nested if else statement

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 construct.

In a nested if construct, you can have an if…elif…else construct inside another if…elif…else construct.

Syntax:

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

Example:

#!/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 “Good bye!”

Output:

Expression value is less than 200
Which is 100
Good bye!

 

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply