Python if-elif-else – Conditional Statements

In this python article, we will discuss how to control the flow of a program based on different conditions and how to use if, if-else, and if-elif-else statements.

1. If Statement

Assume we want to execute a selected segment of the program on the basis of condition. For that purpose, we can use if Statement in python.

if statement is the type of selection statement because we select which code to execute according to the result of a particular condition.

if condition Flow Chart
if condition Flow Chart

If the condition is True then the statements within the if block is executed else the control goes to the rest of the program.

Syntax : 
if <condition/expression>:
 statements-sequence

Let’s see some valid examples of if Statement

1.1. if Condition in one line

If we have only one statement within the if condition we can write the whole expression in just one line which is also called as shorthand if condition syntax.

if A > B: print(A)

1.2. Multiple statements in if block

When we have multiple lines of code inside the if statement, we have to start from the next line and entire code within if block should have one extra level of indentation.

if A > B:
 A += B
 print("A is greater than B")

1.3. Multiple condition in if statement

We can add more than one condition in an if statement and then combine them using logical operators.

if A > B and A > C:
 print("A is the greatest")
 A = B + C

Let’s see how we can use if statement in python.

In the code below, we are taking two integer inputs and finding which one is greater.

a = int(input())
b = int(input())

if a > b:
    print("A is greater than B")

if a < b:
    print("B is greater than A")

if a == b: print("A is equal to B")
Output
10
20
B is greater than A

Note : indentation is important in python , inside if block indentation of all the statement should be same otherwise it will give error


2. if…else… statement

In the above program, we can observe that the simple if statement does nothing when the condition is False.

There is one more drawback with using just the if conditions and that is that even if the first condition A>B is true then also it is checking other conditions, which are not necessary and also increases program execution time.

So to resolve this issue python has if-else statement

if-else condition Flow chart
if-else condition Flow chart
Syntax : 
if <condition/expression>:
 statement-sequence
else :
 statement-sequence

Let’s see how we can write the above code using if else statement

a = int(input())
b = int(input())

if a > b:
    print("A is greater than B")
else:
    print("B is greater than A")

if a == b: print("A is equal to B")
Output
10
20
B is greater than A

In the code above, it will always check the == condition, when we think of this code simply then we want:

  • check if a > b ,true print the statement
  • else check if a < b, true print the statement
  • else print the statement that both are equal

2.1. if-else Condition in one line

If we have only one statement within the if and else blocks then we can write the whole expression in just one line which is also called as shorthand if-else condition syntax.

a = 1
b = 2
print("a") if a > b else print("b")

So to connect multiple if else blocks together python has a keyword elif.


3. if…elif…else statement

elif statement is used when we want to give a condition with else statement. We can simply say that it like a nested if inside else statement.

if...elif...else condition Flow chart
if…elif…else condition Flow chart
Syntax : 
if <condition/expression>:
 Statement-sequence
elif <condition/expression>:
 Statement-sequence
else:
 Statement-sequence

Let’s see how we can write the above code using if…elif…else statement

a = int(input())
b = int(input())

if a > b:
    print("A is greater than B")
elif a < b:
    print("B is greater than A")
else:
    print("A is equal to B")
Output
10
20
B is greater than A

Note : elif and else are optional , but if is mandatory


4. Nested if..else statement

As mentioned in the previous section if..elif..else statement is similar to nested if else lets see how we can write the same code using nested if else statements and not using the elif keyword.

Syntax : 
if <condition/expression>:
 Statement-sequence
 if <condition/expression>:
   Statement-sequence
else:
 Statement-sequence
 if <condition/expression>:
   Statement-sequence

Let’s see how we can write the above code using a nested if-else statement. One thing to note here is that there can be a number of combinations possible, we just have to write the one that makes it easy to read and understand.

a = int(input())
b = int(input())

if a > b:
    print("A is greater than B")
else:
  if a < b:
    print("B is greater than A")
  else:
    print("A is equal to B")
Output
10
20
B is greater than A

Note : elif and else are optional , but if is mandatory.


5. Conclusion

We have discussed how to make the decisions using the if block, if-else block, how to use elif keyword, nested if else blocks and how to write if and if-else blocks in a single line.


Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index