- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- Python Versions
- Python Environment Setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control & Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break Vs continue
- Python pass Vs continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List Vs Tuple Vs Dict Vs Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python CGI Programming
- Python Network Programming
- Python Send E-mail
- Python Multi-threading
- Python XML Processing
- Python MySQL Database
- Python GUI Programming
- Python Event Handling
- Python Keywords
- Python All Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
- Python Test
- Python Online Test
- Give Online Test
- All Test List
pass Statement in Python
As name suggests, the pass statement does nothing, as it gets treated as a null statement. Now may be, this question may arise in your mind:
- If pass statement does nothing, then why we need/use it or why Python provides this statement ?
The answer to this question may be according to the programmer's need. But I've provided two solid reasons/answers for the above question, that are:
- We can use pass statement wherever the statement is required to avoid syntax error. And only if that section of program does not require to take any action.
- We can also use pass statement, if we want to add some block of codes in future. That is, in short, pass statement can also be worked as a placeholder for whatever the block of code, we want to write in future
Important - The pass statement does nothing, it is only required when syntactically needed to avoid syntax error, but actually to do nothing. That is, if the program's need to provide the statement, but we want to do nothing, then we can use pass keyword or statement there.
Syntax of pass Statement
The complete statement of pass is nothing, but the pass keyword itself, therefore if we talk about its syntax, then it is just the keyword pass like shown below:
pass
We can use pass statement, wherever we want in the whole Python program such as:
- We can use it in conditional block
- We can use it in user-defined function
- We can also use it in classes
Examples of pass Statement
The theory part of the pass statement is completed. Therefore it's time to take its examples. Example helps a lot to understand the topic in computer programming world like Python:
nums = [1, 2, 3, 4, 5] for n in nums: if n==2: pass else: print(n)
The snapshot given below shows the sample output produced by above example program on pass statement:
As you can see from the above sample output, the number 2 gets skipped to print. Because using the if statement, I've applied the condition n==2, that tells, whenever the value of n becomes equal to 2, then program flow goes inside the body of if. And inside the body of if, I've used the pass statement, that does nothing.
Since the pass statement does nothing, it just passes that block or body where it is present. Let's take another example, relatable to previous program:
nums = [1, 2, 3, 4, 5] for n in nums: if n==2: pass print(n)
This time, the program produces all the five numbers on output like shown in the snapshot given below:
Unlike continue that forces the loop to continue for its next iteration, skipping rest statement(s), that lies below the continue keyword in the same indentation, to execute. Confused ? Let's take an example to differentiate between these two :)
nums = [1, 2, 3, 4, 5] for n in nums: if n==2: continue print(n)
This program produces following output:
Either pass works as a placeholder for future code or used to avoid syntax error. For example, the following program:
nums = [1, 2, 3, 4, 5] for n in nums: if n == 3: print(n)
Produces an error like shown in the snapshot given below:
Therefore, to avoid these types of syntax error, we use pass statement like shown in the program given below:
nums = [1, 2, 3, 4, 5] for n in nums: if n == 3: pass print(n)
« Previous Tutorial Next Tutorial »