If, elif and else statement in python allow us to perform different tasks depending on certain conditions.

These are also known as conditional statements because they decide which statements should run and which should not.

Verbally:

“Hey, if this happens then perform this action.”

The general Syntax of If, elif and else statements are as follows:

If there is only one condition depending on which you want to execute some statements then elif is not required.

Let’s say, if a student gets more than or equal to 40 marks then he passes the examination or else he fails. This is how we can represent it in python using if and else.

Let see some example more examples for clear understanding:

output

output

output

Characteristics of if-else in Python

  • if statement allows the computer to perform some action.
  • elif statement is executed if above statements is false.
  • else statement is executed only when all the conditions are false.

Use if-elif-else in that situation when only one of the given tasks/statements can be executed depending on some conditions.

Leave a Reply