OneCompiler

Conditional Statements

When ever you want to perform a set of operations based on a condition if and if-else are used.

1. If

if conditional-expression :
    #code

Example

x = 10
y = 20

if (x != y) : 
  print (" x and y are not equal") 

Check result here

2. If-else

if conditional-expression :
    #code
else :
    #code

Example

x = 10
y = 10

if (x != y) : 
  print (" x and y are not equal")
else :
  print ("x and y are equal")

Check result here

3. Nested If-else

if conditional-expression :
    #code
elif conditional-expression :
    #code
else :
    #code

Example

x = 10
y = 20

if (x > y) :
  print (" x is greater than y")
elif (x < y ) :
  print ("x is less than y") 
else :
  print ("x and y are equal")

Check result here

Note

Indentation is very important in Python, make sure the indentation is followed correctly