Skip to main content

Python if....else Statement

If..else statements are like extension of if statements. The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition.

As we know if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.

Syntax:

>>>if (condition):
          Statement
>>>else:
             Statement

Flow chart:

Flow chart for if....elese Statement

Example:

a = 2 
b = 3
if a > b:
    print("a is greater than b")
else:
    print("a is less than b")

Output:

a is less than b

Tags
Submitted by devanshi.srivastava on June 3, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.