Skip to content

If-else Statement in Python

Mastering If-else Statement in Python: A Comprehensive Guide

The if-else statement in Python is used to execute a block of code based on a condition. Understanding the if-else statement is fundamental to writing flexible and dynamic Python programs. In this comprehensive guide, we’ll explore the syntax and usage of the if-else statement in Python.

What is the If-else Statement in Python?

The if-else statement in Python is used to execute a block of code based on a condition. The if-else statement is also known as the if-then-else statement. The if-else statement is a selection statement, which is a type of control statement in Python.

Syntax of the If-else Statement in Python

The syntax of the if-else statement in Python is as follows:

Syntax
if condition:
    # code to execute if condition is True
else:
    # code to execute if condition is False
Syntax
if condition:
    # code to execute if condition is True
else:
    # code to execute if condition is False

The if-else statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces.

Rules for Writing the If-else Statement in Python

The following are the rules for writing the if-else statement in Python:

  • The ifif keyword must be followed by a condition.
  • The condition must be followed by a colon (::).
  • The code to execute if the condition is TrueTrue must be indented by four spaces.
  • The elseelse keyword must be followed by a colon (::).
  • The code to execute if the condition is FalseFalse must be indented by four spaces.
  • The elseelse clause is optional.
  • The elseelse clause must be written after the ifif clause.
  • The elseelse clause must be written before the elifelif clause.
  • The elseelse clause must be written after the elifelif clause.
  • The elifelif clause is optional.
  • The elifelif clause must be written after the ifif clause.
  • The elifelif clause must be written before the elseelse clause.
  • The elifelif clause must be written after the elifelif clause.
  • The elifelif keyword must be followed by a condition.

There are five ways to write the if-else statement in Python:

  • ifif statement
  • if-elseif-else statement
  • if-elif-elseif-elif-else statement
  • nested if-elsenested if-else statement
  • ternary operatorternary operator statement

If Statement

ifif Statement

The ifif statement in Python is used to execute a block of code if a condition is TrueTrue. The ifif statement is also known as the if-thenif-then statement. The ifif statement is a selection statement, which is a type of control statement in Python.

The syntax of the ifif statement in Python is as follows:

Syntax
if condition:
    # code to execute if condition is True
Syntax
if condition:
    # code to execute if condition is True

Diagram:

Diagram of the if statement in Python


  graph TD
    A[Start] --> B{Condition}
    B -->|True| C[Execute Code]
    C --> D[End]
    B ---->|False| E[End]

if statement

The ifif statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces.

The following example demonstrates how to use the ifif statement in Python:

if.py
# if statement
x = 10
if x > 5:
    print("x is greater than 5")
if.py
# if statement
x = 10
if x > 5:
    print("x is greater than 5")

Output:

command
C:\Users\Your Name> python if.py
x is greater than 5
command
C:\Users\Your Name> python if.py
x is greater than 5

In the above example, we have used the ifif statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.

If-else Statement

if-elseif-else Statement

The if-elseif-else statement in Python is used to execute a block of code if a condition is TrueTrue. If the condition is FalseFalse, a different block of code is executed. The if-elseif-else statement is also known as the if-then-elseif-then-else statement. The if-elseif-else statement is a selection statement, which is a type of control statement in Python.

The syntax of the if-elseif-else statement in Python is as follows:

Syntax
if condition:
    # code to execute if condition is True
else:
    # code to execute if condition is False
Syntax
if condition:
    # code to execute if condition is True
else:
    # code to execute if condition is False

The if-elseif-else statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces.

Diagram:

Diagram of the if-else statement in Python


  graph TD
    A[Start] --> B{Condition}
    B -->|True| C[Execute Code]
    C --> D[End]
    B ---->|False| E[Execute Code]
    E --> F[End]

if-else statement

The following example demonstrates how to use the if-elseif-else statement in Python:

if-else.py
# if-else statement
x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")
if-else.py
# if-else statement
x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")

Output:

command
C:\Users\Your Name> python if-else.py
x is greater than 5
command
C:\Users\Your Name> python if-else.py
x is greater than 5

In the above example, we have used the if-elseif-else statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.

If-elif-else Statement

if-elif-elseif-elif-else Statement

The if-elif-elseif-elif-else statement in Python is used to execute a block of code if a condition is TrueTrue. If the condition is FalseFalse, a different block of code is executed. The if-elif-elseif-elif-else statement is also known as the if-then-elseif-then-else statement. The if-elif-elseif-elif-else statement is a selection statement, which is a type of control statement in Python.

The syntax of the if-elif-elseif-elif-else statement in Python is as follows:

Syntax
if condition1:
    # code to execute if condition1 is True
elif condition2:
    # code to execute if condition2 is True
else:
    # code to execute if condition1 and condition2 are False
Syntax
if condition1:
    # code to execute if condition1 is True
elif condition2:
    # code to execute if condition2 is True
else:
    # code to execute if condition1 and condition2 are False

The if-elif-elseif-elif-else statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The elifelif keyword is followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces.

Diagram:

Diagram of the if-elif-else statement in Python


  graph TD
    A[Start] --> B{Condition1}
    B -->|True| C[Execute Code]
    C --> D[End]
    B ---->|False| E{Condition2}
    E -->|True| F[Execute Code]
    F --> G[End]
    E ---->|False| H[Execute Code]
    H --> I[End]

if-elif-else statement

The following example demonstrates how to use the if-elif-elseif-elif-else statement in Python:

if-elif-else.py
# if-elif-else statement
x = 10
if x > 5:
    print("x is greater than 5")
elif x < 5:
    print("x is less than 5")
else:
    print("x is equal to 5")
if-elif-else.py
# if-elif-else statement
x = 10
if x > 5:
    print("x is greater than 5")
elif x < 5:
    print("x is less than 5")
else:
    print("x is equal to 5")

Output:

command
C:\Users\Your Name> python if-elif-else.py
x is greater than 5
command
C:\Users\Your Name> python if-elif-else.py
x is greater than 5

In the above example, we have used the if-elif-elseif-elif-else statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.

Another example of the if-elif-elseif-elif-else statement in Python:

if-elif-else.py
# if-elif-else multiple statements
x = 10
if x > 5:
    print("x is greater than 5")
elif x < 5:
    print("x is less than 5")
elif x > 10:
    print("x is greater than 10")
else:
    print("x is equal to 5")
if-elif-else.py
# if-elif-else multiple statements
x = 10
if x > 5:
    print("x is greater than 5")
elif x < 5:
    print("x is less than 5")
elif x > 10:
    print("x is greater than 10")
else:
    print("x is equal to 5")

Output:

command
C:\Users\Your Name> python if-elif-else.py
x is greater than 5
command
C:\Users\Your Name> python if-elif-else.py
x is greater than 5

In the above example, we have used the if-elif-elseif-elif-else statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.

Nested If-else Statement

nested if-elsenested if-else Statement

The nested if-elsenested if-else statement in Python is used to execute a block of code if a condition is TrueTrue. If the condition is FalseFalse, a different block of code is executed. The nested if-elsenested if-else statement is also known as the nested if-then-elsenested if-then-else statement. The nested if-elsenested if-else statement is a selection statement, which is a type of control statement in Python.

The syntax of the nested if-elsenested if-else statement in Python is as follows:

Syntax
if condition1:
    # code to execute if condition1 is True
    if condition2:
        # code to execute if condition2 is True
    else:
        # code to execute if condition2 is False
else:
    # code to execute if condition1 is False
Syntax
if condition1:
    # code to execute if condition1 is True
    if condition2:
        # code to execute if condition2 is True
    else:
        # code to execute if condition2 is False
else:
    # code to execute if condition1 is False

The nested if-elsenested if-else statement begins with the ifif keyword, followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The ifif keyword is followed by a condition. The condition is followed by a colon (::). The code to execute if the condition is TrueTrue is written on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces. The code to execute if the condition is FalseFalse is written after the elseelse keyword, on the next line, indented by four spaces.

Diagram:

Diagram of the nested if-else statement in Python


  graph TD
    A[Start] --> B{Condition1}
    B -->|True| C[Execute Code]
    C --> D{Condition2}
    D -->|True| E[Execute Code]
    E --> F[End]
    D ---->|False| G[Execute Code]
    G --> H[End]
    B ---->|False| I[Execute Code]
    I --> J[End]

nested if-else statement

The following example demonstrates how to use the nested if-elsenested if-else statement in Python:

nested-if-else.py
# nested if-else statement
x = 10
if x > 5:
    print("x is greater than 5")
    if x > 10:
        print("x is greater than 10")
    else:
        print("x is less than or equal to 10")
else:
    print("x is less than or equal to 5")
nested-if-else.py
# nested if-else statement
x = 10
if x > 5:
    print("x is greater than 5")
    if x > 10:
        print("x is greater than 10")
    else:
        print("x is less than or equal to 10")
else:
    print("x is less than or equal to 5")

Output:

command
C:\Users\Your Name> python nested-if-else.py
x is greater than 5
x is less than or equal to 10
command
C:\Users\Your Name> python nested-if-else.py
x is greater than 5
x is less than or equal to 10

Ternary Operator

ternary operatorternary operator Statement

The ternary operatorternary operator statement in Python is used to execute a block of code if a condition is TrueTrue. If the condition is FalseFalse, a different block of code is executed. The ternary operatorternary operator statement is also known as the conditional operatorconditional operator statement. The ternary operatorternary operator statement is a selection statement, which is a type of control statement in Python.

The syntax of the ternary operatorternary operator statement in Python is as follows:

Syntax
condition1 if condition2 else condition3
Syntax
condition1 if condition2 else condition3

The ternary operatorternary operator statement begins with a condition, followed by the ifif keyword. The ifif keyword is followed by a condition. The condition is followed by the elseelse keyword. The elseelse keyword is followed by a condition.

Diagram:

Diagram of the ternary operator statement in Python


  graph TD
    A[Start] --> B{Condition2}
    B -->|True| C[Execute Code]
    C --> D[End]
    B ---->|False| E[Execute Code]
    E --> F[End]

ternary operator statement

The following example demonstrates how to use the ternary operatorternary operator statement in Python:

ternary-operator.py
# ternary operator statement
x = 10
print("x is greater than 5") if x > 5 else print("x is less than or equal to 5")
ternary-operator.py
# ternary operator statement
x = 10
print("x is greater than 5") if x > 5 else print("x is less than or equal to 5")

Output:

command
C:\Users\Your Name> python ternary-operator.py
x is greater than 5
command
C:\Users\Your Name> python ternary-operator.py
x is greater than 5

In the above example, we have used the ternary operatorternary operator statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.

Another example of the ternary operatorternary operator statement in Python:

ternary-operator.py
# ternary operator multiple statements
x = 10
print("x is greater than 5") if x > 5 else print("x is less than or equal to 5") if x < 5 else print("x is equal to 5")
ternary-operator.py
# ternary operator multiple statements
x = 10
print("x is greater than 5") if x > 5 else print("x is less than or equal to 5") if x < 5 else print("x is equal to 5")

Output:

command
C:\Users\Your Name> python ternary-operator.py
x is greater than 5
command
C:\Users\Your Name> python ternary-operator.py
x is greater than 5

In the above example, we have used the ternary operatorternary operator statement to check if the value of the variable xx is greater than 55. Since the value of xx is 1010, the condition is TrueTrue, and the code inside the ifif statement is executed. The string "x is greater than 5""x is greater than 5" is printed to the console.

Conclusion

In this guide, we explored the syntax and usage of the if-else statement in Python. We also explored the ternary operator, which is a one-line shorthand for the if-else statement. Now that you have a solid understanding of the if-else statement in Python, you can use it to write flexible and dynamic Python programs. In this guide, we explored the syntax and usage of the if-else statement in Python. We also explored the ternary operator, which is a one-line shorthand for the if-else statement. Now that you have a solid understanding of the if-else statement in Python, you can use it to write flexible and dynamic Python programs.

As you delve deeper into Python programming, experiment with different control statements, explore their applications in real-world scenarios, and use them to enhance the efficiency and clarity of your code. For more hands-on examples and in-depth tutorials, explore our resources on Python Central Hub!

Was this page helpful?

Let us know how we did