Course
Pass Statement
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Pass Statement
Python pass Statement
Python
pass
statement is used when a statement is required syntactically but you do not want any command or code to execute.Python
pass
statement is a null operation; nothing happens when it executes. Python pass
statement is also useful in places where your code will eventually go, but has not been written yet, i.e., in stubs).Syntax
pass
Example
The following code shows how you can use the
pass
statement in Pythonfor letter in 'Python': if letter == 'h': pass print ('This is pass block') print ('Current Letter :', letter)print ("Good bye!")
When the above code is executed, it produces the following output
Current Letter : PCurrent Letter : yCurrent Letter : tThis is pass blockCurrent Letter : hCurrent Letter : oCurrent Letter : nGood bye!
Dummpy Infinite Loop with Pass
This is simple enough to create an infinite loop using
pass
statement. For instance, if you want to code an infinite loop that does nothing each time through, do it with a pass:while True: pass # Type Ctrl-C to stop
Because the body of the loop is just an empty statement, Python gets stuck in this loop. As explained earlier
pass
is roughly to statements as None is to objects — an explicit nothing.Using Ellipses ... as pass Alternative
Python 3.X allows ellipses coded as three consecutive dots ...to be used in place of
pass
statement. This ...
can serve as an alternative to the pass statement.For example if we create a function which does not do anything especially for code to be filled in later, then we can make use of
...
def func1(): ... # Alternative to pass
def func2(): ... # Works on same line too
func1() # Does nothing if calledfunc2() # Does nothing if called
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.