If vs Elif vs Else If in Python

[ad_1]

We use conditional statements in Python to regulate the execution circulation of a program. On this article, we are going to focus on if vs elif vs else if in Python to get an understanding of how these conditional statements work.

If Statements in Python

If statements are used to execute sure Python statements when a selected situation is True. The syntax for if statements in Python is as follows.

#statements exterior if block earlier than the if assertion 
if situation: 
    #statements in if block 
#statements exterior if block after the if assertion

Right here, the situation evaluates to a boolean worth i.e. True or False. If the situation is True, the statements within the if block are executed. In any other case, the statements exterior the if block are executed. You’ll be able to observe this within the following code.

marks=50
if marks>40:
    print("Go")

Output:

Go

Else Assertion in Python

We use the else assertion together with the if assertion when now we have to execute a job at any time when the situation contained in the if assertion is False. It has the next syntax.

#statements exterior if block earlier than the if assertion 
if situation:
    #statements in if block 
else: 
    #statements contained in the else block 
#statements exterior the if block after the if assertion

Right here, when the situation within the if block is False, the statements within the else block are executed. You’ll be able to observe this within the following instance.

marks=34
if marks>40:
    print("Go")
else:
    print("Fail")

Output:

Fail

Elif Assertion in Python

We use elif statements in Python if now we have to execute code based mostly on a number of situations. It has the next syntax.

if situation:
    #statements in if block
elif condition1:
    #statements in elif block 1
elif condition2:
    #statements in elif block 2 
.
.
.
elif situation N:
    #statements in elif block N
#statements exterior the if block after the if and elif assertion

Right here,

  • If the situation contained in the if block is True, the code within the if block is executed and the remainder of the code within the elif blocks is skipped.
  • When the situation contained in the if block is False, the primary elif block is executed. If condition1 inside elif block 1 is True, the statements inside elif block 1 are executed and the remainder of the code is skipped. 
  • When the situations inside if block and elif block 1 are False, the second elif block is executed. If the condition2 contained in the elif block 2 is True, the statements inside elif block 2 are executed and the remainder of the code is skipped. 
  • This course of continues till one of many situations within the if-elif blocks are true or all of the situations are false.

You’ll be able to observe this within the following instance.

marks=65
if marks>90:
    print("A+")
elif marks >80:
    print("A")
elif marks>70:
    print("B+")
elif marks>60:
    print("B")
elif marks>40:
    print("Go")
else:
    print("Fail")

Output:

B

If all of the situations within the if and elif blocks are false, the codes written inside these blocks will not be executed. You’ll be able to observe this within the following code.

marks=23
if marks>90:
    print("A+")
elif marks >80:
    print("A")
elif marks>70:
    print("B+")
elif marks>60:
    print("B")
elif marks>40:
    print("Go")
print("Outdoors if elif statements.")

Output:

Outdoors if elif statements.

You can too add an else block with if and elif blocks within the code. If not one of the situations within the if and elif blocks are True, the statements within the else block are executed as proven under.

marks=23
if marks>90:
    print("A+")
elif marks >80:
    print("A")
elif marks>70:
    print("B+")
elif marks>60:
    print("B")
elif marks>40:
    print("Go")
else:
    print("Fail")
print("Outdoors if elif statements.")

Output:

Fail
Outdoors if elif statements.

If Else If Assertion in Python

We can not use the else if assertion in Python in a single assertion. We are able to solely use else-if statements in Python if now we have to make use of nested conditional statements. For this, you need to use the next syntax.

If situation:
    #statements in if block
Else:
    If situation 2:
        #statements within the inside if block
    Else:
        #statememts within the inside else block

Right here, if the situation within the outer if block is False, the code within the else block is executed. Once more, if condition2 contained in the inside if block is true, the code contained in the inside if block is executed. In any other case, the code contained in the inside else block is executed. You’ll be able to observe this within the following instance.

marks=-34
if marks>40:
    print("Go")
else:
    if marks>0:
        print("Fail")
    else:
        print("Unfavourable Marks")

Output:

Unfavourable Marks

If vs Elif vs Else If in Python

After discussions within the earlier sections, we are able to conclude the next remarks for If vs Elif vs Else if in Python.

  1. The If assertion is used to execute a single conditional assertion whereas elif statements are used with the if assertion to execute a number of conditional statements. 
  2. We use the if assertion to execute code when a situation is True. However. We are able to use the else assertion to execute code when the situation contained in the if assertion is False.
  3. We use elif statements in Python to execute a number of conditional statements whereas the else if blocks are used to execute nested conditional statements. 

Conclusion

On this article, we mentioned tips on how to execute If vs Elif vs Else if statements in Python. To learn extra about Python programming, you possibly can learn this text on working with toml recordsdata in Python. You may also like this text on Python lastly key phrase.

I hope you loved studying this text. Keep tuned for extra informative articles.

Comfortable Studying!

Advisable Python Coaching

Course: Python 3 For Inexperienced persons

Over 15 hours of video content material with guided instruction for newcomers. Learn to create actual world purposes and grasp the fundamentals.

[ad_2]

Leave a Comment

Your email address will not be published. Required fields are marked *