Conditional Statements Examples

Checking a single condition:


            age = 18

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

Output:


            You are eligible to vote.
        

Checking multiple conditions using if, elif, and else:


            grade = 85

            if grade >= 90:
                print("You got an A grade.")
            elif grade >= 80:
                print("You got a B grade.")
            elif grade >= 70:
                print("You got a C grade.")
            elif grade >= 60:
                print("You got a D grade.")
            else:
                print("You failed the exam.")
        

Output:


            You got a B grade.
        

Nested if statements:


            num = 10

            if num > 0:
                print("The number is positive.")
                
                if num % 2 == 0:
                    print("The number is even.")
                else:
                    print("The number is odd.")
            else:
                print("The number is zero or negative.")

        

Output:


            The number is positive.
            The number is even.