Conditional Statements in Python

📘 Python 👁 75 views 📅 Nov 05, 2025
⏱ Estimated reading time: 1 min

Conditional Statements in Python

Conditional statements allow Python programs to execute code based on conditions.


1. if Statement

  • Executes a block of code if a condition is True.

x = 10 if x > 5: print("x is greater than 5")

2. if...else Statement

  • Executes one block if condition is True, another if False.

age = 18 if age >= 18: print("You are an adult") else: print("You are a minor")

3. if...elif...else Statement

  • Handles multiple conditions.

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

4. Nested if Statements

  • Place if statements inside another if for complex conditions.

num = 10 if num > 0: print("Positive number") if num % 2 == 0: print("Even number") else: print("Odd number")

5. Conditional Expressions (Ternary Operator)

  • Short form of if-else in a single line.

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

6. Key Points

  • if executes code when condition is True.

  • elif checks additional conditions.

  • else runs when all conditions are False.

  • Supports nested conditions and ternary expressions.

  • Conditions use comparison operators (==, !=, >, <, >=, <=) and logical operators (and, or, not).


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes