Python Forum

Full Version: Python Coding Issue!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Everyone,

Why Is The “Pass” Keyword Used For In Python?

>>> if x==0:
pass
else:
print "x!=0"

Anyone tell me why it is used in python or Anyone tells me best wat to learn python. I want to explore more about python coding.
the pass statement is a place holder statement that does nothing. it is useful if a statement requires a minimum of one line and you will add it it later. Or your a particular situation where you are executing code where a statement is required and you don't need to do anything in your code. It has been useful in a few situations.

You can buy books and study on websites. You can also download the Python Training Kit from Siraoops at www.siraoops.com. The training kit contains hundreds of programming exercises.

Hope this answers all of you questions,
John
(Sep-13-2019, 08:33 AM)ankitdixit Wrote: [ -> ]Hello Everyone,

Why Is The “Pass” Keyword Used For In Python?

>>> if x==0:
pass
else:
print "x!=0"

Anyone tell me why it is used in python or Anyone tells me best wat to learn python. I want to explore more about python coding.

In spoken language it's: 'if x is equal zero do nothing, else print that x is not equal to zero.'. However, in spoken language and in Python it usually expressed:

# spoken language: "if x is not equal to zero notify the user about that"
if x != 0:
    print('x != 0')
EDIT:

Of course there is built-in help in Python (press 'q' key to exit help):

>>> help('pass')
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
(END)
This Python example page explains the use of the pass statement to indicate empty functions, classes and loops.
Pass. Time passes. And often nothing happens. In Python we use the "pass" keyword (a statement) to indicate that nothing happens—the function, class or loop is empty.

With pass, we indicate a "null" block. Pass can be placed on the same line, or on a separate line. Pass can be used to quickly add things that are unimplemented.

Method example. Here we use the "pass" statement on two methods. These methods do nothing. But they can be called in Python code.

Result: Nothing happens when the pass methods are called. The pass can be used in other places in Python programs.