This page topics close

The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

Thee elif statement is optional

  • Syntax of elif statement

    if expression1:
       statement(s)
    elif expression2:
       statement(s)
    elif expression3:
       statement(s)
    else:
       statement(s)

    Example)
    drag_handle
     
    # using elif statement
    # checking the positive , negative or zero of number
    number=int(input("Enter the number "))
    if number<0:
    	print("Number is negative")
    elif number==0:
    	print("Number is zero")
    else:
    	print("Number is positive ")
    Output:
    Enter the number -1
    Number is negative

    Example

    
    # example 
    var =200
    if var == 200:
       print ("1 - Got a true expression value")
       print (var)
    elif var == 150:
       print ("2 - Got a true expression value")
       print (var)
    elif var == 100:
       print ("3 - Got a true expression value")
       print (var)
    else:
       print ("4 - Got a false expression value")
       print (var)
    
    print ("Good bye!")

    Output:
    1 - Got a true expression value
    200
    Good bye!
    

What next?

Share this page