if else condition in python

What is conditional statement?

A conditional statement is a programming construct that allows a program to take a specific action based on the evaluation of a specific condition. In Python, if-else conditions are used to create conditional statements. 

They execute different blocks of code depending on whether a certain condition is true or false. Conditional statements are useful for making decisions in your code based on different scenarios.

Python Logical Conditions

  1. Equal to ( a==b )
  2. Not Equal to ( a!=b )
  3. Grater than ( a>b)
  4. Grater than or Equal to ( a>=b)
  5. Less than ( a<b)
  6. Less than or Equal to ( a<=b )
Implementation

1). if statement:

a = 10
b = 20
if a<b:
    print("a is less than b")

In this example we have two variable a and b if condition a<b is true than code will be execute inside the if statement rather than no execution occur.

2). else statement:

a = 10
b = 20
if a<b:
    print("a is less than b")
else:
    print("Some thing went wrong")

In this example we have two variables a and b if condition a<b is false than code will be execute inside the else statement.

3). elif statement:

If you want to apply multiple conditions checking the you can use elif statement.

a = 10
b = 20
if a<b:
    print("a is less than b")
elif a>b:
    print("a is grater than b")
else:
    print("Some thing went wrong")

In this example we have two variable a and b if condition a<b is true than code will be execute inside the if statement and a>b than code will be execute inside the elif statement rather else statement code execution occur.


LIKE | SHARE | SUBSCRIBE