IF,ELIF,ELSE Statement in Python Programming Language

if, elif, else Statement in Python Programming Language

In this lesson, you will learn about the if,elif,else Statement in Python Programming Language. You will see a practical example of how these statements code and work. We will cover this lesson in two parts. First, we will see how if..else statement work and then we move on for if..elif..else statement.

if, else Statement in Python Programming Language:

If, else Syntax:

if test expression:
    if code
    else code

in the above code, the block of else statement will only execute if the test expression of if statement failed to run or return FALSE value otherwise else statement will not execute. No matter how many if statements you are using else statement only use for once moreover the else statement is optional to use.

Recommended:  Structuring In Python Programming Language

if..else Flowchart in Python


Python programming does not support switch statements like other programming languages but you can take the similar work with these statements.

Python if..else Statement Example

# The code below verifies whether the number is positive or negative

number = 3

if number >= 0:
    print("The Number is Positive or Zero")
    print("The number is Negative")

In the above example, the number is 3 which is greater than 0 (zero) means our condition is true and if block will be executed.

And in case if the number is less than 0 (zero) suppose -5 the then the condition becomes false and else block will execute.

Recommended:  Basic Operators In Python Programming Language

if, elif, else Statement in Python Programming Language:

If, else Syntax:

if test expression:
    Block of if code
elif test expression:
    Block of elif code
    Block of else code

The name elif maybe confusing but it stands for else if. It works along with if statement means first we set any test condition for if statement and then we set some other test conditions for elif statements.

The benefit of this is if the test expression for if  becomes False it doesn’t end rather it checks the next elif test expression and continue till all the elif checked. Now in case if all the elif become False then the body of else statement will be executed.

Recommended:  Keywords And Identifiers In Python Programming Language

Remember only one else statement allow in if statement but opposite to this if statement block can have as many elif statement blocks as needed.

if.elif.else Flowchart in Python


Python if.elif.else Statement Example

#In this example we will check the working of if..elif..else statement

number = 10

if number > 0:
    print("Positive number")
elif number == 0:
    print("Negative number")

In the above example the variable number is 10 which is greater than 0 (zero) so our if statement becomes True and the message inside if block will be executed.

In case if the number is equal to 0 (zero) it the elif block will be executed. And if number is negative than the else block will be executed.

Jack Sparrow

Hay I am Admin Of This Website. I am very passionate about Computers and technology. This website is the result of my passion. I love to eliminate the barriers comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

Leave a Reply

Close Menu