Python IF, ELSE, ELIF, Nested IF & Switch Case Statement

What are Conditional Statements?

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.

When “else condition” does not work

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.

Python 2 Example

Above codes are Python 3 examples, If you want to run in Python 2 please consider following code.