Example 1: Using only If

      
          if grade >= 98:
    print("Grade: A+ (Exceptional)")
elif grade >= 90:
    print("Grade: A")
elif grade >= 80:
    print("Grade: B")
elif grade >= 70:
    print("Grade: C")
elif grade >= 60:
    print("Grade: D")
else:
    print("Grade: F")
          

Example 2:

      
          #Checking for Prime Numbers
n = int(input("Enter a number: "))
if n > 1:
    for i in range(2, n):
        if (n % i) == 0:
            print(n, "is not a prime number")
            break
    else:  # The else part of a loop (not an if-else)
        print(n, "is a prime number")
else:
    print(n, "is not a prime number")
          

Example 3:

      
          Multiple Conditions with Logical operators
a, b, c = 5, 3, 4
if a > b and b > c:
    print("a is greater than b and b is greater than c")
elif a == b or b == c:
    print("Either a equals b or b equals c")
else:
    print("No condition matched")
          

Example 4:

      
          # Nested Condition
x = 20
if x > 10:
    if x > 20:
        print("x is greater than 20")
    else:
        print("x is greater than 10 but not greater than 20")
else:
    print("x is 10 or less")
          

Example 5:

      
          # using if and List Comprehension
numbers = [1, 2, 3, 4, 5, 6]
squares_even = [n**2 for n in numbers if n % 2 == 0]
print(squares_even)
          

Example 6:

      
          # handling Exception with condition
try:
    x = int(input("Enter a number: "))
    y = int(input("Enter another number: "))
    if y != 0:
        print(x / y)
    else:
        print("Cannot divide by zero")
except ValueError:
    print("Invalid input, please enter a number")
          

Example 7:

      
          # using conditions to filter dictionary entries

students = {
    "Alice": 85,
    "Bob": 90,
    "Carol": 87,
    "Dave": 92,
}
honors_students = {k: v for k, v in students.items() if v > 90}
print(honors_students)
          

Example 8:

      
          # using conditions with functions
def check_age(age):
    if age >= 18:
        return "Adult"
    elif age >= 13:
        return "Teenager"
    else:
        return "Child"

print(check_age(20))
          

Example 9:

      
          # pattern matching with conditions
def describe(value):
    match value:
        case value if value > 0:
            return "Positive"
        case value if value == 0:
            return "Zero"
        case value if value < 0:
            return "Negative"

print(describe(-5))
          

Example 10:

      
          # With function Recursion
def fibonacci(n):
    if n <= 1:
        return n
    else:
        return(fibonacci(n-1) + fibonacci(n-2))

n_terms = 10
print("Fibonacci sequence:")
for i in range(n_terms):
    print(fibonacci(i))