Python if-else and if-elif-else

Python if else statements can be expressed on two ways

  • Simple if-else statement
  • if-elif-else statement with one or more occurrences of elif.

if else example

x = 1
y = 1
if x == y:
  print "equal"
else:
  print "not equal"
equal
Env: Python 2.7.6

if-elif-else example

x = 1
if x < 0:
  print "lt zero"
elif x == 0:
  print "zero"
else:
  print "gt zero"
gt zero
Env: Python 2.7.6
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments