Python | Create a function with a pass statement

Today, in this article we will be learning about a Python Program to create a function with a pass statement. Before moving on to the coding of this, let us take a look at what the Pass keyword exactly is.

Pass is a null operation that permits the next statement to be executed. Future code is utilized as a placeholder in the pass statement. Nothing occurs when the pass statement is performed, but you avoid receiving an error when an empty code is prohibited. In loop declarations, function definitions, class definitions, or if statements, no empty code is permitted.

Different methods to create a function with a pass statement

def using_pass(digit):
    if digit == 2:
        print(">> INSIDE IF CONITIONAL\n This statement is printed because your digit matches the condition")
        pass
    print(">> AFTER PASS \n You pressed: ", digit, "\nThis statement is a pass statement. This statement will get printed even if the digit does not match the condition")

d = int(input("Enter a digit: "))

using_pass(d)

Output:

Enter a digit: 2
>> INSIDE IF CONITIONAL
 This statement is printed because your digit matches the condition
>> AFTER PASS
 You pressed:  2
This statement is a pass statement. This statement will get printed even if the digit does not match the condition

Explanation:

Here we made a function that accepts an argument. The user enters a number that gets passed to the defined function using_pass. Here inside this function, we used the if conditional wherein we check whether the digit is equal to 2 or not. If the condition is satisfied then the next statement that is inside the if conditional gets printed i.e “math is ok. This statement is printed because your digit matches the condition”. But the next statement after the pass keyword will get printed i.e “You pressed: “, digit, “\nThis statement is a pass statement. This statement will get printed even if the digit does not match the condition”

Share:
Avatar of Dhvanil V

Author: Dhvanil V

Leave a Reply

Your email address will not be published. Required fields are marked *