Tuesday, June 16, 2020

A Guide to Python IF...ELIF...ELSE...Statements



Hello guys! Welcome back to another section of my tutorial on Python. In this tutorial guide, we are going to be discussing about the if.....elif.....else statements in Python.

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

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

Syntax

The following below is the syntax of Python if...else statement -

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

Flow Chart



Example

Below is a short example -

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

Output

The following below is the output of the above example -

1 - Got a true expression value
100
2 - Got a false expression value
0
Good bye!


Elif Statement
The Python elif statement allows you to check multiple expressions for a TRUE and executes a block of code as soon as one of those conditions evaluates to TRUE.

Similar to the else, the elif statement is optional. However, unlike else, for which there can be at most only one statement, there can be an arbitrary number of elif statements following an if.

RECOMMENDED POST: Python Basic Syntax Tutorial

Syntax
The following below is the syntax for an elif statement -

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

Core Python does not provide switch or case statements just like other programming languages, but we can make use of if....elif....statements to simulate switch case as follows -

Example
Below is a short example -

#!/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
Below is the output of the above example -

3 - Got a true expression value
100
Good bye!


Alright guys! This is where we are rounding up for this tutorial. In my next tutorial, we will be discussing about the  nested If Statements.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.