Skip to content
Exercises · Q4

Q.Use assert statement in Question No. 3 to test the division expression in the program.

Odisha ChseTextbookSubjective· 2mImportance★★★★★
44% · 4/9 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The assert statement in Python is used as a debugging aid to test conditions that must be true — here, it will verify that the divisor is not zero before performing division, raising an AssertionError if the condition fails.

Understanding the assert Statement

The assert statement is Python's built-in debugging tool. Its syntax is:

assert condition, "optional error message"

When Python encounters an assert, it evaluates the condition. If the condition is True, execution continues normally. If the condition is False, Python raises an AssertionError with the optional message (if provided).

Important

The assert statement is not meant for handling runtime errors in production code — that's what try/except blocks are for. assert is a development-time tool to catch impossible states early.

Why Use assert for Division?

Division has a critical precondition: the divisor must not be zero. If you divide by zero, Python raises a ZeroDivisionError at runtime. Using assert before the division lets you catch this problem before the division happens, making the error location clearer and the code's intent more explicit.

Consider this program without assert:

a = 10
b = 0
result = a / b   # ZeroDivisionError: division by zero

The error occurs, but the traceback points to the division line. With assert, you can check the divisor first:

a = 10
b = 0
assert b != 0, "Divisor cannot be zero!"
result = a / b   # This line never runs if b == 0

Now, if b is zero, you get an AssertionError with your custom message — much clearer debugging.

Complete Solution for Question No. 3

Here's the program that uses assert to test the division expression:

# Program to demonstrate assert with division
def safe_divide(dividend, divisor):
    """
    Divides dividend by divisor, using assert to check divisor is non-zero.
    """
    assert divisor != 0, f"Division by zero is not allowed! Divisor was {divisor}"
    
    result = dividend / divisor
    return result

# Test cases
print("Test 1: 10 / 2 =", safe_divide(10, 2))
print("Test 2: 15 / 3 =", safe_divide(15, 3))
print("Test 3: 8 / 4 =", safe_divide(8, 4))

# This will raise AssertionError
print("Test 4: Attempting 10 / 0...")
try:
    safe_divide(10, 0)
except AssertionError as e:
    print(f"AssertionError caught: {e}")

Expected output:

Test 1: 10 / 2 = 5.0
Test 2: 15 / 3 = 5.0
Test 3: 8 / 4 = 2.0
Test 4: Attempting 10 / 0...
AssertionError caught: Division by zero is not allowed! Divisor was 0
Watch out

A common mistake is to use assert for input validation in production code. assert statements can be globally disabled by running Python with the -O (optimize) flag. For production input validation, always use explicit if checks and raise appropriate exceptions.

Key Lines Explained …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.