Python Flow Control

Python Pass Statement

Python pass is a null statement provided by the Python programming language in order to ignore a set of code that we do not want to execute. If a function, class, or a loop needs to be coded and executed in the future, then the python pass statement is used to indicate the Python Interpreter to ignore that particular function, class, or loop while the program is being executed.

Python Pass

Let us consider an example:

                    

def func1():
    # implement function later

def func2():
    # implement function later

def func3(a):
    print(a)

func3('Python')

Output

                    

File "", line 3
    def func2():
    ^
IndentationError: expected an indented block

In the above program, suppose the programmer has decided to code the function 1 and function 2 later, according to his needs. The programmer has completed the 3rd function and has passed a value to it. After running the program, Python Compiler will give the above error. To avoid such a situation, we use the Python Pass statement.

Example

                    

def func1():
    pass

def func2():
    pass

def func3(a):
    print(a)

func3('Python')

Output

Definition of Pass Python

  • The Python Pass statement is used as a placeholder within loops, functions, classes, and if-statements that will be implemented later.
  • In Python, a Pass statement is a null statement that is used in cases where the loop, function, or class is to be ignored or written and executed in the future.

Pass statement in python

The Python pass statement is a null statement that is used by the programmers if they do not know what to code or if they want to skip an iteration of the loop. The pass statement does not get ignored by the Python compiler. Instead, it gets executed by just skipping past the loop condition, function, or class. When the pass statement is executed, it results in no operation.

Syntax

Flowchart –

Python pass statement flowchart 

Note – Python has a syntactical requirement that code blocks (following if, except, def, class, and so on) must not be empty.

Let us assume that a list of numbers is declared, and we check the condition if the numbers are odd or even. We check this by using the if statement in the for loop. But we only want to print the odd numbers by checking the condition for even numbers. This is possible by using the Python Pass statement. Let us look at the example below for a better understanding.

Example – Pass statement inside the loop

                    

# Use of pass statement in Python Program
num = [1, 3, 6, 33, 76, 29, 17, 60, 100, 47, 53, 88]

print('Odd numbers are: ')
for i in num:
    # check if the number is even
    if i % 2 == 0:
        # if even, then pass
        pass
    # print the odd numbers
    else:
        print (i)

Output

                    

Odd numbers are:
1
3
33
29
17
47
53

Pass statement in python can also be used within a class and within a function. Let us look at the below example to understand how it’s done.

Example – Pass statement inside the class

                    

class Student:
    print('Inside Student class')   
    pass
    print('Pass executed')

Output

                    

Inside Student class
Pass executed

Example – Pass statement inside a function

                    

def my_func():
    print('Pass statement inside function')
    pass
    print('Pass executed')

my_func()

Output

                    

Pass statement inside function
Pass executed

FAQs on Python Pass

Q1. What is the Python Pass statement?

Python language provides looping control statements such as break, continue and pass. It is considered as the python pass statement is a null operation. In Python, the pass statement is used when the programmer wants to bypass any coding segment. The pass keyword in Python is used to execute nothing; that is, when we don’t want to execute code, we can use the pass to execute nothing. It simply causes the control to pass by without any code being executed. The Python compiler/interpreter does not ignore the pass statement.

Q2. How does the Pass statement work in Python?

When you merely want a function with no implementation, the pass the statement is used. It will be used to implement functions, classes, loops, and other things in the future.

Pass statement is used inside any function, class, or loops. If the given condition is true and the next statement is the pass statement, then the python compiler ignores that value or iteration of the loop and proceeds to the next line of code.

Example

                    

print('Odd numbers are:')
for i in range(1, 10):
    if i % 2 == 0:
        pass
    else:
        print(i)

Output

                    

Odd numbers are:
1
3
5
7
9

Q3. What is the difference between the Python pass and continue statements?

The pass statement has no effect. When you design a method, function, class, or a loop code that you don’t intend to implement just yet, you use the pass statement. It will run the method and if the condition is satisfied, then it will ignore the code and skip to the next line of code.

The continue statement, on the other hand, skips all of the loop’s remaining statements and returns control to the top. If the condition of the loop is satisfied, that condition is skipped and the next iteration is performed.

Example

                    

string = 'Program'

# Pass statement
for i in string:
    if i == 'g':
        print('Pass executed')   # g character is found and will be printed
        pass
    print(i)

print()

# Continue statement
for j in string:
    if j == 'g':
        print('Continue executed')   # g character is omitted
        continue
    print(j)

Output

                    

P
r
o
Pass executed
g
r
a
m

P
r
o
Continue executed
r
a
m

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.