assert Statement in Python in Hindi – Python में Assert Statement क्या है?
Table of Contents
2. Characteristics of assert Statement in Hindi – एसर्ट स्टेटमेंट की विशेषताएं
4. Working of assert Statement in Hindi – एसर्ट स्टेटमेंट कैसे काम करता है?
5. Examples of assert Statement in Hindi – एसर्ट स्टेटमेंट के उदाहरण
6. Advantages of Using assert in Debugging in Hindi – Debugging में एसर्ट के फायदे
assert Statement in Hindi – एसर्ट स्टेटमेंट क्या है?
जब हम कोई program लिखते हैं, तो कई बार हमें यह पक्का करना होता है कि कोई particular condition हमेशा true रहे, ताकि आगे का code सही तरीके से चल सके। इसी काम के लिए Python में assert Statement दिया गया है।
assert Statement एक debugging tool है जो किसी दी गई condition को check करता है — अगर वो condition true है, तो program normal तरीके से आगे चलता रहता है, लेकिन अगर वो false निकलती है, तो Python तुरंत एक
AssertionErrorraise कर देता है और program वहीं रुक जाता है।आसान भाषा में समझें तो, assert एक तरह का "sanity check" है — यह programmer को यह भरोसा दिलाता है कि जो चीज़ें उसने मान ली थीं (assumptions), वो वाकई सही हैं, वरना program तुरंत error देकर रोक देगा, बिना गलत result को आगे बढ़ाए।
यह मुख्य रूप से development और testing के दौरान इस्तेमाल किया जाता है, ताकि bugs को उनकी जड़ पर ही, जल्दी पकड़ा जा सके, बजाय इसके कि program गलत output दे और वो गलती बाद में पता चले।
रोज़-मर्रा की life से example लें तो, यह बिल्कुल वैसा ही है जैसे कोई electrician बिजली का काम शुरू करने से पहले check करता है कि मेन स्विच बंद है या नहीं। अगर स्विच बंद नहीं मिला (condition false), तो वो आगे काम ही नहीं करेगा। assert Statement भी program में बिल्कुल यही role निभाता है।
Exam की भाषा में इसकी definition याद रखनी हो तो: "assert Statement is a debugging aid that tests a condition, and triggers an error if that condition is not true, allowing early detection of bugs during development."
यह ध्यान रखना ज़रूरी है कि assert Statement मुख्य रूप से development और testing phase के लिए बनाया गया है, production-level error handling के लिए नहीं — इसके लिए Python में
try-exceptजैसे proper exception handling tools इस्तेमाल किए जाते हैं।
Characteristics of assert Statement in Hindi – एसर्ट स्टेटमेंट की विशेषताएं
1. Condition-Based Check
assert Statement हमेशा एक condition (expression) को check करता है, और उसी के आधार पर decide करता है कि program आगे चलेगा या रुक जाएगा।
2. AssertionError Raise करता है
अगर assert की गई condition false निकलती है, तो Python एक built-in exception AssertionError raise करता है, जिसे बाद में debug किया जा सकता है।
3. मुख्य रूप से Debugging के लिए
assert को production error-handling के लिए नहीं, बल्कि development और testing phase में internal assumptions verify करने के लिए बनाया गया है।
4. Optional Error Message
assert Statement में condition के साथ-साथ एक custom error message भी दिया जा सकता है, जो error आने पर screen पर print होता है और debugging को आसान बनाता है।
5. Optimized Mode में Disable हो सकता है
जब Python को -O (optimize) flag के साथ run किया जाता है, तो सारे assert statements ignore हो जाते हैं — इसलिए critical validations के लिए assert पर पूरी तरह depend नहीं करना चाहिए।
6. Simple और Short Syntax
assert Statement सिर्फ एक keyword है, इसलिए इसे लिखना बहुत आसान होता है और यह code को ज़्यादा लंबा किए बिना ही validation add कर देता है।
7. Early Bug Detection में मदद
चूँकि assert condition check होते ही तुरंत रुक जाता है, इसलिए bugs का पता program के आगे बढ़ने से पहले ही चल जाता है, जिससे debugging समय पर हो जाती है।
Syntax of assert in Hindi – एसर्ट का Syntax
Python में assert Statement का syntax बहुत simple होता है, और इसे दो तरीकों से लिखा जा सकता है:
# Sirf condition ke saath
assert condition
# Condition ke saath custom error message
assert condition, "Error message yaha likhi jaati hai"
condition: यह वो expression है जिसे check किया जाता है। अगर यह
Trueनिकलती है, तो कुछ नहीं होता और program आगे चलता रहता है।error message (optional): अगर condition
Falseनिकलती है, तो यह messageAssertionErrorके साथ print होता है, जिससे पता चलता है कि गलती कहाँ हुई।
एक छोटा सा example देखें:
age = 15
assert age >= 18, "Age 18 se kam hai, entry allowed nahi"
यहाँ चूँकि age की value 18 से कम है, इसलिए condition false हो जाती है और Python तुरंत यह error देगा:
AssertionError: Age 18 se kam hai, entry allowed nahi
Working of assert Statement in Hindi – एसर्ट स्टेटमेंट कैसे काम करता है?
assert Statement के अंदर actually एक if जैसा ही logic काम करता है, बस उसका output अलग तरीके से handle होता है। नीचे दिया गया diagram इसी internal working को दिखाता है।
यह diagram दिखा रहा है कि assert Statement सबसे पहले condition को evaluate करता है — अगर वो False निकलती है तो AssertionError raise होकर program रुक जाता है, और अगर True निकलती है तो program बिना किसी रुकावट के अगली line पर चला जाता है।
Step-by-step समझें तो:
Step 1: Python सबसे पहले
assertके बाद लिखी गई condition को evaluate करता है।Step 2: अगर condition
Trueनिकलती है, तो कुछ भी नहीं होता — program अगली line पर normal तरीके से आगे बढ़ जाता है।Step 3: अगर condition
Falseनिकलती है, तो Python एकAssertionErrorexception raise करता है।Step 4: अगर assert के साथ कोई custom message दिया गया था, तो वो message error के साथ print होता है, ताकि पता चल सके कि गलती कहाँ है।
Step 5: जब तक इस error को
try-exceptमें handle न किया जाए, तब तक program वहीं terminate हो जाता है।
Examples of assert Statement in Hindi – एसर्ट स्टेटमेंट के उदाहरण
Example 1: Simple assert Check
x = 10
y = 20
assert x < y
print("x, y se chota hai")
यहाँ चूँकि x < y true है, इसलिए assert चुपचाप pass हो जाता है और अगली line print हो जाती है। कोई error नहीं आता।
Example 2: Custom Error Message के साथ
marks = 45
assert marks >= 50, "Student fail hai, marks 50 se kam hain"
print("Student pass hai")
यहाँ marks की value 50 से कम है, इसलिए condition false हो जाती है और यह error आएगा:
AssertionError: Student fail hai, marks 50 se kam hain
Example 3: Function में Input Validate करना
def calculate_square_root(number):
assert number >= 0, "Negative number ka square root nahi nikal sakte"
return number ** 0.5
print(calculate_square_root(16)) # Output: 4.0
print(calculate_square_root(-9)) # AssertionError raise hoga
इस example में assert एक तरह से function के अंदर एक "guard" की तरह काम कर रहा है, जो function को गलत input के साथ आगे बढ़ने से पहले ही रोक देता है।
Example 4: Testing में assert का इस्तेमाल
def add(a, b):
return a + b
assert add(2, 3) == 5, "add function galat result de raha hai"
print("Test passed")
यह तरीका खासकर छोटे unit tests लिखते समय बहुत इस्तेमाल होता है, जहाँ programmer यह verify करता है कि function का actual output expected output से match करता है या नहीं।
Advantages of Using assert in Debugging in Hindi – Debugging में एसर्ट के फायदे
Bugs को development phase में ही, जल्दी पकड़ लिया जाता है, इससे पहले कि वो production में जाकर बड़ी problem बनें।
Code में जगह-जगह assumptions को साफ़-साफ़ लिखा जा सकता है, जिससे code खुद अपनी documentation जैसा भी काम करता है।
Custom error message की वजह से यह exactly बताता है कि गलती कहाँ और क्यों हुई, जिससे debugging का समय काफ़ी कम हो जाता है।
Functions को गलत input के साथ आगे बढ़ने से रोका जा सकता है, जिससे बाद में आने वाले unpredictable errors कम होते हैं।
Unit testing के दौरान assert एक बहुत आसान और lightweight तरीका है यह check करने का कि function सही output दे रहा है या नहीं।
Syntax इतना छोटा और simple है कि development के दौरान बार-बार validations लिखना बोझ नहीं लगता।
Points to Remember in Hindi – ध्यान रखने वाली बातें
assert Statement को production-level error handling के लिए इस्तेमाल नहीं करना चाहिए, क्योंकि Python के optimized mode (
-Oflag) में यह पूरी तरह disable हो जाता है।Critical validations (जैसे user input check करना जिस पर पूरा program depend करता हो) के लिए
ifcondition के साथ proper exception raise करना ज़्यादा सही approach है।assert का इस्तेमाल मुख्य रूप से development, debugging और testing के दौरान internal assumptions verify करने के लिए किया जाना चाहिए।
अगर assert को गलती से एक tuple के साथ लिखा जाए (जैसे
assert (condition, "message")), तो वो हमेशा True ही मान लिया जाता है, क्योंकि एक non-empty tuple हमेशा truthy होता है — यह एक common mistake है जो exam में भी पूछी जाती है।assert Statement AssertionError raise करता है, जिसे अगर ज़रूरत हो तो
try-exceptब्लॉक की मदद से handle भी किया जा सकता है।