• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / If vs Elif vs Else If in Python

If vs Elif vs Else If in Python

Author: Aditya Raj
Last Updated: May 6, 2023

We use conditional statements in Python to control the execution flow of a program. In this article, we will discuss if vs elif vs else if in Python to get an understanding of how these conditional statements work.

Table of Contents
  1. If Statements in Python
  2. Else Statement in Python
  3. Elif Statement in Python
  4. If Else If Statement in Python
  5. If vs Elif vs Else If in Python
  6. Conclusion

If Statements in Python

If statements are used to execute certain Python statements when a particular condition is True. The syntax for if statements in Python is as follows.

#statements outside if block before the if statement 
if condition: 
    #statements in if block 
#statements outside if block after the if statement

Here, the condition evaluates to a boolean value i.e. True or False. If the condition is True, the statements in the if block are executed. Otherwise, the statements outside the if block are executed. You can observe this in the following code.

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

Output:

Pass

Else Statement in Python

We use the else statement along with the if statement when we have to execute a task whenever the condition inside the if statement is False. It has the following syntax.

#statements outside if block before the if statement 
if condition:
    #statements in if block 
else: 
    #statements inside the else block 
#statements outside the if block after the if statement

Here, when the condition in the if block is False, the statements in the else block are executed. You can observe this in the following example.

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

Output:

Fail

Elif Statement in Python

We use elif statements in Python if we have to execute code based on multiple conditions. It has the following syntax.

if condition:
    #statements in if block
elif condition1:
    #statements in elif block 1
elif condition2:
    #statements in elif block 2 
.
.
.
elif condition N:
    #statements in elif block N
#statements outside the if block after the if and elif statement

Here,

  • If the condition inside the if block is True, the code in the if block is executed and the rest of the code in the elif blocks is skipped.
  • When the condition inside the if block is False, the first elif block is executed. If condition1 inside elif block 1 is True, the statements inside elif block 1 are executed and the rest of the code is skipped. 
  • When the conditions inside if block and elif block 1 are False, the second elif block is executed. If the condition2 inside the elif block 2 is True, the statements inside elif block 2 are executed and the rest of the code is skipped. 
  • This process continues until one of the conditions in the if-elif blocks are true or all the conditions are false.

You can observe this in the following example.

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("Pass")
else:
    print("Fail")

Output:

B

If all the conditions in the if and elif blocks are false, the codes written inside these blocks are not executed. You can observe this in 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("Pass")
print("Outside if elif statements.")

Output:

Outside if elif statements.

You can also add an else block with if and elif blocks in the code. If none of the conditions in the if and elif blocks are True, the statements in the else block are executed as shown below.

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("Pass")
else:
    print("Fail")
print("Outside if elif statements.")

Output:

Fail
Outside if elif statements.

If Else If Statement in Python

We cannot use the else if statement in Python in a single statement. We can only use else-if statements in Python if we have to use nested conditional statements. For this, you can use the following syntax.

If condition:
    #statements in if block
Else:
    If condition 2:
        #statements in the inner if block
    Else:
        #statememts in the inner else block

Here, if the condition in the outer if block is False, the code in the else block is executed. Again, if condition2 inside the inner if block is true, the code inside the inner if block is executed. Otherwise, the code inside the inner else block is executed. You can observe this in the following example.

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

Output:

Negative Marks

If vs Elif vs Else If in Python

After discussions in the previous sections, we can conclude the following remarks for If vs Elif vs Else if in Python.

  1. The If statement is used to execute a single conditional statement whereas elif statements are used with the if statement to execute multiple conditional statements. 
  2. We use the if statement to execute code when a condition is True. On the other hand. We can use the else statement to execute code when the condition inside the if statement is False.
  3. We use elif statements in Python to execute multiple conditional statements whereas the else if blocks are used to execute nested conditional statements. 

Conclusion

In this article, we discussed how to execute If vs Elif vs Else if statements in Python. To read more about Python programming, you can read this article on working with toml files in Python. You might also like this article on Python finally keyword.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us