Python If Else Statements – Conditional Statements

Redirect Timer

Get Solutions

Redirecting in 15 seconds…

 

(toc)

Conditional statements allow Python programs to make decisions based on conditions. Using if, elif, and else statements, you can control the flow of your program and execute specific blocks of code depending on whether a condition is true or false.

 we’ll cover:

  • What are conditional statements?
  • Types of conditional statements in Python.
  • Examples to make concepts clear and easy to understand.

What are Conditional Statements?

Conditional statements are decision-making structures. They enable Python to check conditions and execute code based on the outcome (True or False).

The most common keywords for conditional statements are:

  • if: Executes a block of code if the condition is true.
  • elif: Short for “else if,” allows checking multiple conditions.
  • else: Executes a block of code if no preceding conditions are true.

Syntax of Conditional Statements

Here’s the general structure:


if condition: # Code to execute if the condition is true elif another_condition: # Code to execute if the second condition is true else: # Code to execute if all conditions are false


1. If Statement

The if statement is used to execute a block of code if the condition is true.

Example:


age = 18 if age >= 18: print("You are eligible to vote.")

Output:


You are eligible to vote.


2. If-Else Statement

The if-else statement provides an alternative path of execution when the if condition is false.

Example:


age = 16 if age >= 18: print("You are eligible to vote.") else: print("You are not eligible to vote yet.")

Output:


You are not eligible to vote yet.


3. If-Elif-Else Statement

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

Example:


marks = 85 if marks >= 90: print("Grade: A") elif marks >= 75: print("Grade: B") elif marks >= 60: print("Grade: C") else: print("Grade: F")

Output:


Grade: B



4. Nested If Statements

You can use an if statement inside another if statement, called a nested if.

Example:


number = 15 if number > 10: if number % 2 == 0: print("The number is greater than 10 and even.") else: print("The number is greater than 10 and odd.")

Output:


The number is greater than 10 and odd.


5. Shorthand If Statement

Python allows a compact way to write if statements when there’s only one statement to execute.

Example:


age = 20 if age >= 18: print("You are an adult.")

Output:


You are an adult.


6. Shorthand If-Else (Ternary Operator)

For simple if-else conditions, Python supports a shorthand ternary operator.

Example:


age = 16 status = "Adult" if age >= 18 else "Minor" print(status)

Output:


Minor


7. Logical Operators in Conditional Statements

You can combine multiple conditions using logical operators like and, or, and not.

Example:


age = 20 citizen = True if age >= 18 and citizen: print("You are eligible to vote.") else: print("You are not eligible to vote.")

Output:


You are eligible to vote.


8. Membership Operators in Conditional Statements

You can check for membership using in or not in.

Example:


fruit = "apple" if fruit in ["apple", "banana", "cherry"]: print(f"{fruit} is available.") else: print(f"{fruit} is not available.")

Output:


apple is available.


9. Example: Even or Odd

Problem: Write a program to check if a number is even or odd.

Solution:


number = int(input("Enter a number: ")) if number % 2 == 0: print(f"{number} is even.") else: print(f"{number} is odd.")

Output:


Enter a number: 7 7 is odd.


10. Example: Grading System

Problem: Write a program to assign grades based on marks.

Solution:


marks = int(input("Enter your marks: ")) if marks >= 90: grade = "A" elif marks >= 80: grade = "B" elif marks >= 70: grade = "C" elif marks >= 60: grade = "D" else: grade = "F" print(f"Your grade is: {grade}")

Output:


Enter your marks: 85 Your grade is: B


Common Mistakes to Avoid

  1. Indentation Errors: Python uses indentation to define blocks of code. Ensure proper indentation.

    if True:
    print("This will cause an error!") # Incorrect indentation
  2. Using Assignment Instead of Comparison:

    • Use == for comparison, not =.
  3. Forgetting to Handle All Conditions:

    • Always include an else block if your logic requires it.

Leave a Reply