Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial guide, we are going to be discussing about the if.....elif.....else statements in Python.
An else statement in Python can be combined with an if statement. An else statement contains a 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.
An else statement in Python can be combined with an if statement. An else statement contains a 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
RECOMMENDED POST: Python if Statement Tutorial with examples
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 lets you to check multiple expressions for true and executes a block of code soon as one of those conditions evaluates to TRUE.
Similar to the else statement, the elif statement is optional. But, unlike else, for which there can be at most only one statement, there can be an arbitrary number of elif statements following an if.
Similar to the else statement, the elif statement is optional. But, 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)
Python does not provide switch or case statements just like other programming languages, but we can use the 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!
RECOMMENDED: Python Variables and Data Types
Alright guys! This is where we are rounding up for this tutorial. In our next tutorial, we will be discussing about the nested If Statements.
Do 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.
Do 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.