Conditional Statement in Python perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false. Conditional statements are handled by IF statements in Python.
In this tutorial, we will see how to apply conditional statements in Python.
In Python, If Statement is used for decision making. It will run the body of code only when IF statement is true.
When you want to justify one condition while the other condition is not true, then you use "if statement".
Syntax:
if expression Statement else Statement
Let see an example-
# #Example file for working with conditional statement # def main(): x,y =2,8 if(x < y): st= "x is less than y" print(st) if __name__ == "__main__": main()
In this step, we will see what happens when your "if condition" does not meet.
The "else condition" is usually used when you have to judge one statement on the basis of other. If one condition goes wrong, then there should be another condition that should justify the statement or logic.
Example:
# #Example file for working with conditional statement # def main(): x,y =8,4 if(x < y): st= "x is less than y" else: st= "x is greater than y" print (st) if __name__ == "__main__": main()
There might be many instances when your "else condition" won't give you the desired result. It will print out the wrong result as there is a mistake in program logic. In most cases, this happens when you have to justify more than two statement or condition in a program.
An example will better help you to understand this concept.
Here both the variables are same (8,8) and the program output is "x is greater than y", which is WRONG. This is because it checks the first condition (if condition), and if it fails, then it prints out the second condition (else condition) as default. In next step, we will see how we can correct this error.
# #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" else: st= "x is greater than y" print(st) if __name__ == "__main__": main()
To correct the previous error made by "else condition", we can use "elif" statement. By using "elif" condition, you are telling the program to print out the third condition or possibility when the other condition goes wrong or incorrect.
Example
# #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" elif (x == y): st= "x is same as y" else: st="x is greater than y" print(st) if __name__ == "__main__": main()
In this step, we will see how we can condense out the conditional statement. Instead of executing code for each condition separately, we can use them with a single code.
Syntax
A If B else C
Example:
def main(): x,y = 10,8 st = "x is less than y" if (x < y) else "x is greater than or equal to y" print(st) if __name__ == "__main__": main()
Following example demonstrates nested if Statement
total = 100 #country = "US" country = "AU" if country == "US": if total <= 50: print("Shipping Cost is $50") elif total <= 100: print("Shipping Cost is $25") elif total <= 150: print("Shipping Costs $5") else: print("FREE") if country == "AU": if total <= 50: print("Shipping Cost is $100") else: print("FREE")
Uncomment Line 2 in above code and comment Line 3 and run the code again
What is switch statement?
A switch statement is a multiway branch statement that compares the value of a variable to the values specified in case statements.
Python language doesn’t have a switch statement.
Python uses dictionary mapping to implement switch statement in Python
Example
function(argument){ switch(argument) { case 0: return "This is Case Zero"; case 1: return " This is Case One"; case 2: return " This is Case Two "; default: return "nothing"; }; };
For the above switch statement Alternative in Python
def SwitchExample(argument): switcher = { 0: " This is Case Zero ", 1: " This is Case One ", 2: " This is Case Two ", } return switcher.get(argument, "nothing") if __name__ == "__main__": argument = 1 print (SwitchExample(argument))
Python 2 Example
Above codes are Python 3 examples, If you want to run in Python 2 please consider following code.
# If Statement #Example file for working with conditional statement # def main(): x,y =2,8 if(x < y): st= "x is less than y" print st if __name__ == "__main__": main() # How to use "else condition" #Example file for working with conditional statement # def main(): x,y =8,4 if(x < y): st= "x is less than y" else: st= "x is greater than y" print st if __name__ == "__main__": main() # When "else condition" does not work #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" else: st= "x is greater than y" print st if __name__ == "__main__": main() # How to use "elif" condition #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" elif (x == y): st= "x is same as y" else: st="x is greater than y" print st if __name__ == "__main__": main() # How to execute conditional statement with minimal code def main(): x,y = 10,8 st = "x is less than y" if (x < y) else "x is greater than or equal to y" print st if __name__ == "__main__": main() # Nested IF Statement total = 100 #country = "US" country = "AU" if country == "US": if total <= 50: print "Shipping Cost is $50" elif total <= 100: print "Shipping Cost is $25" elif total <= 150: print "Shipping Costs $5" else: print "FREE" if country == "AU": if total <= 50: print "Shipping Cost is $100" else: print "FREE" #Switch Statement def SwitchExample(argument): switcher = { 0: " This is Case Zero ", 1: " This is Case One ", 2: " This is Case Two ", } return switcher.get(argument, "nothing") if __name__ == "__main__": argument = 1 print SwitchExample(argument)
A conditional statement in Python is handled by if statements and we saw various other ways we can use conditional statements like if and else over here.