Skip to content

If-else Statement in Python

Mastering If-else Statement in Python: A Comprehensive Guide

Section titled “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.

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.

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

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

Rules for Writing the If-else Statement in Python

Section titled “Rules for Writing the If-else Statement in Python”

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

  • The if keyword must be followed by a condition.
  • The condition must be followed by a colon (:).
  • The code to execute if the condition is True must be indented by four spaces.
  • The else keyword must be followed by a colon (:).
  • The code to execute if the condition is False must be indented by four spaces.
  • The else clause is optional.
  • The else clause must be written after the if clause.
  • The else clause must be written before the elif clause.
  • The else clause must be written after the elif clause.
  • The elif clause is optional.
  • The elif clause must be written after the if clause.
  • The elif clause must be written before the else clause.
  • The elif clause must be written after the elif clause.
  • The elif keyword must be followed by a condition.

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

  • if statement
  • if-else statement
  • if-elif-else statement
  • nested if-else statement
  • ternary operator statement

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

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

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

Diagram:

diagram if statement mermaid
Diagram of the if statement in Python

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

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

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

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

The if-else statement in Python is used to execute a block of code if a condition is True. If the condition is False, a different block of code is executed. 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.

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

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

Diagram:

diagram if-else statement mermaid
Diagram of the if-else statement in Python

The following example demonstrates how to use the if-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")

Output:

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

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

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

The syntax of the if-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

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

Diagram:

diagram if-elif-else statement mermaid
Diagram of the if-elif-else statement in Python

The following example demonstrates how to use the if-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")

Output:

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-else statement to check if the value of the variable x is greater than 5. Since the value of x is 10, the condition is True, and the code inside the if statement is executed. The string "x is greater than 5" is printed to the console.

Another example of the if-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")

Output:

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-else statement to check if the value of the variable x is greater than 5. Since the value of x is 10, the condition is True, and the code inside the if statement is executed. The string "x is greater than 5" is printed to the console.

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

The syntax of the nested 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

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

Diagram:

diagram nested if-else statement mermaid
Diagram of the nested if-else statement in Python

The following example demonstrates how to use the nested 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")

Output:

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

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

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

Syntax
condition1 if condition2 else condition3

The ternary operator statement begins with a condition, followed by the if keyword. The if keyword is followed by a condition. The condition is followed by the else keyword. The else keyword is followed by a condition.

Diagram:

diagram ternary operator statement mermaid
Diagram of the ternary operator statement in Python

The following example demonstrates how to use the ternary 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")

Output:

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

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

Another example of the ternary 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")

Output:

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

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

An if / elif / else chain is a single path through a set of gates: Python checks each condition in order and takes the first branch that is true, skipping the rest.

diagram How if / elif / else decides mermaid
Conditions are checked top to bottom; the first true branch runs and the rest are skipped.

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!

Section titled “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!”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading