Python Language / Python Conditional Statements

It is used to execute a certain block / lines of code / alter certain data only if a specific condition has been met in a computer program. There are 4 conditional statements

1. If statement.
2. Elif statement.
2. Else statement.
3. Switch-Case statements.

Python If statements
In if statement if the condition is true it will execute true block otherwise it execute the code below the lines below the true block code.

Program to find greatest of 2 numbers Output
a = 500
b = 1000
if b > a:
 print("b is greater than a")
b is greater than a

Explanation: Here a, b are variables.

Elif statement
In if statement if the condition is true it will execute true block otherwise it execute else block of code.

Program Output
a = 50
b = 100
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
b is greater than a


Else statement
Else is used when if , elif are false.
Program Output

a = 50
b = 100
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

b is greater than a


Home     Back