DEV Community

Cover image for Conditional Statements in Python
Mahalakshmi C
Mahalakshmi C

Posted on

Conditional Statements in Python

What are Conditional Statements?

Conditional statements are used to make decisions in a program based on certain conditions.

They allow a program to execute different blocks of code depending on whether a condition is True or False.

if Statement

The if statement is used to execute a block of code only when the given condition is True.

Example:

age = 20

if age >= 18:
    print("Eligible to vote")
Enter fullscreen mode Exit fullscreen mode

Output:

Eligible to vote
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The condition age >= 18 is checked.
  • Since the condition is True, the statement inside the if block is executed.

if-else Statement

The if-else statement is used when there are two possible outcomes.

  • If the condition is True, the if block executes.
  • If the condition is False, the else block executes.

Example:

number = 7

if number % 2 == 0:
    print("Even Number")
else:
    print("Odd Number")
Enter fullscreen mode Exit fullscreen mode

Output:

Odd Number
Enter fullscreen mode Exit fullscreen mode

Explanation:

The modulus operator (%) checks the remainder.

  • If the remainder is 0, the number is even.
  • Otherwise, the number is odd.

if-elif-else Statement

The if-elif-else statement is used to check multiple conditions.

  • if checks the first condition.
  • elif checks additional conditions if the previous conditions are False.
  • else executes when all conditions are False.

Example:

mark = 85

if mark >= 90:
    print("Grade A")
elif mark >= 80:
    print("Grade B")
elif mark >= 70:
    print("Grade C")
else:
    print("Fail")
Enter fullscreen mode Exit fullscreen mode

Output:

Grade B
Enter fullscreen mode Exit fullscreen mode

Nested if Statement

A nested if statement means writing an if statement inside another if statement.

It is used when a condition needs to be checked inside another condition.

Example:

age = 22
has_id = True

if age >= 18:
    if has_id:
        print("Eligible to enter")
    else:
        print("ID proof required")
else:
    print("Not eligible")
Enter fullscreen mode Exit fullscreen mode

Output:

Eligible to enter
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • First, Python checks whether the age is 18 or above.
  • If True, it checks whether the person has an ID.
  • Since both conditions are True, the person is eligible to enter.

Ternary Operator

Python supports a one-line if-else expression, also called a conditional expression or ternary operator.

Syntax:

value_if_true if condition else value_if_false
Enter fullscreen mode Exit fullscreen mode

Example:

age = 16

result = "Eligible" if age >= 18 else "Not Eligible"

print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

Not Eligible
Enter fullscreen mode Exit fullscreen mode

Conclusion

Conditional statements are an important part of Python programming. They help programs make decisions and execute different blocks of code based on conditions.

By understanding if, if-else, if-elif-else, and nested if statements, developers can build logical and decision-making programs.

Top comments (0)