Pass Statement in Python

Pass Statement in Python

In Python programming, pass is a null statement. The difference between a comment and pass statement in Python is that, while the interpreter ignores a comment entirely, pass is not ignored.

पॉयथन प्रोग्रामिंग में, pass एक नल स्टेटमेंट है। पॉयथन में एक comment और pass स्टेटमेंट के बीच का अंतर है, कि इंटरप्रेटर पूरी तरह से comment को इग्नोर करता है, pass को इग्नोर नहीं किया जाता है।

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

पॉयथन में pass स्टेटमेंट का उपयोग तब किया जाता है जब किसी स्टेटमेंट को वाक्यविन्यास की आवश्यकता होती है लेकिन आप एक्सिक्यूट करने के लिए कोई कमांड या कोड नहीं चाहते हैं।

The pass statement is a null operation; nothing happens when it executes.

pass स्टेटमेंट एक नल ऑपरेशन है, जब यह एक्सिक्यूट होता है तो कुछ भी नहीं होता है।

The pass is also useful in places where your code will eventually go, but has not been written yet.

pass उन स्थानों में भी उपयोगी है जहां आपका कोड अंततः जाएगा, लेकिन अभी तक नहीं लिखा गया है।

Syntax:
           pass
Example 1:
for a in ‘Python’:
         pass

We generally use it as a placeholder. Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future. They cannot have an empty body. The interpreter would complain. So, we use the pass statement to construct a body that does nothing.

हम आम तौर पर एक प्लेसहोल्डर के रूप में इसका उपयोग करते हैं। माना कि हमारे पास एक लूप या एक फंक्शन है जिसे अभी तक लागू नहीं किया गया है, लेकिन हम इसे भविष्य में लागू करना चाहते हैं। उनके पास खाली बॉडी नहीं हो सकता। इंटरप्रेटर शिकायत करेगा। तो, हम pass स्टेटमेंट का उपयोग बॉडी बनाने के लिए करते हैं जो कुछ भी नहीं करता है।
Example 2

for a in 'Python': 
    if a == 'h' :
        pass
        print ( 'This is pass block') 
    print ('Current Letter :', a) 
print ("Good bye!")

Output:
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!

The preceding code does not execute any statement or code if the value of letter is ‘h’. The pass statement is helpful when you have created a code block but it is no longer required.

यदि लेटर की वैल्यू ‘h’ होती है तो पूर्ववर्ती कोड किसी भी कथन या कोड को एक्सिक्यूट नहीं करता है। pass स्टेटमेंट तब मददगार होता है जब आपने एक कोड ब्लॉक बनाया हो, लेकिन इसकी अब आवश्यकता नहीं है।

You can then remove the statements inside the block but let the block remain with a pass statement so that it doesn’t interfere with other parts of the code.

फिर आप ब्लॉक के अंदर के स्टेटमेंट हटा सकते हैं लेकिन ब्लॉक को pass स्टेटमेंट के साथ रहने दें ताकि वह कोड के अन्य हिस्सों के साथ हस्तक्षेप न करें।

else
In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with ‘if conditional statements. But Python also allows us to use the ‘else’ condition with for loops. Both ‘for’ and ‘while’ loops are in Python also take an optional else block. Title else block will be executed if we don’t exit the loop in any way other than its natural way. So, no ‘break’ statement, no ‘return’ statement, or no exceptions’ being raised inside the loop.

अधिकांश प्रोग्रामिंग भाषाओं (C/C++, Java, etc) में, else स्टेटमेंट के उपयोग को ‘if कंडीशनल स्टेटमेन्टों के साथ प्रतिबंधित किया गया है। लेकिन पॉयथन हमें लूप्स के साथ कंडीशन ‘else’ कंडीशन का उपयोग करने की भी अनुमति देता है। पॉयथन में ‘for’ और ‘while’ लूप्स दोनों else block का विकल्प लेते हैं। else ब्लॉक को एक्सिक्यूट किया जाएगा यदि हम लूप को उसके नैचुरल तरीके के अलावा किसी भी तरह से एक्जिट नहीं करते हैं। इसलिए, कोई ‘ब्रेक स्टेटमेंट’ नही, कोई ‘रिटर्न’ स्टेटमेंट नही या कोई ‘अपवाद’ लूप के अंदर नहीं रखा जाता है।
Syntax:

While loopFor loop
while <test-expression>:          <statement(s)> else: <special-statement(s)>for a in sequence:         <statement(s)> else: <special-statement(s)>

The <special-statement (s)> specified in the else clause will be executed when the while loop terminates – that is, if the loop iterates until the controlling condition becomes false. Consider a simple example:

किसी else क्लॉस में स्पेशिफाईड <special-statement (s) > तब एक्जिक्यूट किया जाएगा जब लूप समाप्त हो जाता

अर्थात लूप कंडीशन फाल्स होने तक इटरेट करता है ।
Example 1:

x = 1 
while x<= 5:
  print (x)
  x = x+1 
else:
  print("loop finished")

Output:
1
2
3
4
5
loop finished

assert – एसर्ट
Assertions in any programming language are the debugging tools which help in smooth flow of code. The process of identifying and fixing the bug is called debugging. Very common debugging is to use print() statement. But the problem with the print () statement is after fixing the bug, compulsory we have to delete the extra added print () statements, otherwise these will be executed at runtime which creates performance issues and disturb console output.

किसी भी प्रोग्रामिंग भाषा में अभिकथन डिबगिंग टूल हैं जो कोड के सुचारू प्रवाह में मदद करते हैं। बग को पहचानने और ठीक करने की प्रक्रिया को डीबगिंग कहा जाता है। बहुत आम डिबगिंग print() स्टेटमेंट का उपयोग करना है। लेकिन print) स्टेटमेंट के साथ समस्या बग को ठीक करने के बाद है, अनिवार्य हमें अतिरिक्त print() स्टेटमेंट को डिलीट करना होगा, अन्यथा इन्हें रनटाइम पर एक्सिक्यूट किया जाएगा जो परफॉर्मेंस इश्यू और डिस्टर्ब कंसोल आउटपुट बनाता है।

To overcome this problem in Python we should go for assert statement. The advantages of assert statement over print statement is after fixing bug we are not required to delete assert statements. Based on our requirement we can enable or disable assert statements.

पॉयथन में इस समस्या को दूर करने के लिए हमें एसर्ट स्टेटमेंट के लिए जाना चाहिए। print() स्टेटमेन्ट पर एसर्ट स्टेटमेंट के फायदे बग को ठीक करने के बाद हैं क्योंकि हमें एसर्ट स्टेटमेंट को हटाने की आवश्यकता नहीं है। अपनी आवश्यकता के आधार पर हम एसर्ट स्टेटमेन्टों को सक्षम या अक्षम कर सकते हैं।

Hence the main purpose of assertions is to perform debugging. Usually we can perform debugging either in development or in test environments but not in production environment.

इसलिए एजर्शन का मुख्य उद्देश्य डिबगिंग करना है। आमतौर पर हम डिबगिंग या तो विकास या परीक्षण वातावरण में कर सकते हैं लेकिन उत्पादन वातावरण में नहीं।

Types of assert statements:
एसर्ट के प्रकार

  1. Simple version

Syntax: assert <condition>

  1. Argumented version

Syntax: assert <condition>, error_message

Parameters: पैरामीटर्स

Condition: The Boolean condition returning true or false.

टू या फाल्स पर रिटर्न होने वाली बूलियन कंडीशन ।

error_message: The optional argument to be printed in console in case of AssertionError

ऐजर्शन एरर की स्थिति में कंसोल में प्रिंट किया जाने वाला वैकल्पिक आगुर्मेन्ट ।

Returns: Returns AssertionError, in case the condition evaluates too false along with the error message which when provided.

एरर मैसेज के साथ जब कंडीशन का फाल्स मूल्यांकन किया जाता है तो इस केस मे एजर्शन एरर रिटर्न करता है। In Python we can use assert statement in two ways as mentioned above.

पॉयथन में हम ऊपर बताए गए स्टेटमेन्ट का उपयोग दो तरीकों से कर सकते हैं।

  1. Assert statement has a condition and if the condition is not satisfied the program will stop and give AssertionError.

एजर्ट स्टेटमेन्ट के पास कंडीशन होती है और अगर कंडीशन संतुष्ट नहीं करती तो प्रोग्राम रूक जाएगा और एजर्शन एरर देगा।

  1. Assert statement can also have a condition and an optional error message. If the condition is not satisfied assert stops the program and gives Assertion Error along with the error message.

एजर्ट स्टेटमेंट में एक कंडीशन और एक वैकल्पिक एरर मैसेज भी हो सकता है। यदि कंडीशन संतोषजनक नहीं है

तो एसर्ट प्रोग्राम को रोक देता है और एरर मैसेज के साथ एसर्शन एरर देता है।

Example 1:
a = 4
b = 0
# using assert to check for 0
print (“The value of a / bis: “)
assert b != 0, “Divide by 0 error”
print (a / b)

Ouput:
The value of a / bis:
Traceback (most recent call last):
  File “C:/Users/HP/Desktop/Desktop/d.py”, line 5, in <module>
    assert b != 0, “Divide by 0 error”
AssertionError: Divide by 0 error

Chapter wise Model Paper Link

About Me