Python pass statement:
The pass statement in Python acts like a placeholder which means you can use the pass statement if you don’t want to execute any code if a specific condition matches. So, you can simply place a pass
where empty code is not permitted, like in loops, function definitions, class definitions, or in if statements.
Syntax :
The signature for the pass
statement is as shown below.
pass
Flow diagram – pass statement :
Python pass statement Examples:
Example 1: In this example, we will demonstrate the pass
statement with the string.
for i in 'Hello':
if i == "H":
pass
print('Inside pass block')
else:
print (i)
Output
Inside pass block
e
l
l
o
Example 2: In this case, we will use pass statement with a function that does nothing.
#Initializing
def func1():
pass
print("Function is called")
func1()
Output
Function is called
Example 3: In this example, We will demonstrate the use of break, continue and pass statements. We will count number of vowels in a string.
#Initializing
vowel=['a','e','i','o','u']
consonant=['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']
v=0
# Using break, continue and pass statements
for i in 'python @ programming $ hello ! how #':
if i == "$":
print("Found $ so terminating....")
break
elif i in vowel:
print("Current letter",i, "is vowel")
v+=1
continue
elif i in consonant:
pass
print("Inside pass block for ",i)
print("Total number of vowels",v)
Output
Inside pass block for p
Inside pass block for y
Inside pass block for t
Inside pass block for h
Current letter o is vowel
Inside pass block for n
Inside pass block for p
Inside pass block for r
Current letter o is vowel
Inside pass block for g
Inside pass block for r
Current letter a is vowel
Inside pass block for m
Inside pass block for m
Current letter i is vowel
Inside pass block for n
Inside pass block for g
Found $ so terminating....
Total number of vowels 4
References
Happy Learning 🙂