How To Do Nothing In An If Statement Python

Are you going to use them if and else statement in Python and want to do nothing when if statement is true? Learn how to do nothing in an if statement in Python.

It is very easy to do nothing in the statement of Python. All you are required to use is the pass statement after your if condition in your Python Program. Let us learn about how to use a pass[1] statement to do nothing in if condition.

1. Using The pass Statement: How To Do Nothing In An If Statement

The pass statement in the if condition will lead your code to run the next line of code and usually it is like if your if the condition becomes true and whichever line should be executed will be executed if you use the pass statement just after the if statement.

Let us see the usage of a pass statement in Python to do nothing in an if statement.

#Python Code Example
#Initializing the List
givenList = ['com', 'You', 'Are', 'Visiting', 'Coduber']

#Checking is Coduber is present in 
#The above list
if "Coduber" in givenList:
    pass
else:
    print("Not Found.")

print("This line will be printed irrespective of If Statement.")

Output

This line will be printed irrespective of If Statement.

As you can see in the above code, the last print statement is printed and the else condition is skipped as the if condition became true and a pass statement allowed the Python interpreter to execute the last line of the code after the else statement.

How To Make Else Statement Do Nothing In Python

Similarly for the Else statement as well you can use the pass statement that will skip to the next line of code to be executed by the interpreter. And you can do nothing when the else statement becomes true in your program.

Let us see in the below example code the usage of the pass statement in Else Statement in Python.

#Python Code Example
#Initializing the List
givenList = ['com', 'You', 'Are', 'Visiting', 'Coduber']

#Checking if Rocky is present in 
#The above list
if "Rocky" in givenList:
    print("Found.")
else:
    pass

print("This line will be printed irrespective of If Statement.")

Output:


This line will be printed irrespective of If Statement.

As you can see in the above code rocky is not present in the given list and hence the else statement becomes true, once inside the else statement the interpreter reads the pass statement and it skips to the next print statement immediately.

How To Do Nothing In An If Statement Python

Wrap Up

I hope you understood how to use the pass statement to do nothing in If or else statement in Python. If you have any better options than the one mentioned above then please let me know in the comment section.

I will happy to add a new solution to the above problem. Also, let me know in the comment section if you are having any problem understanding the above code I will be happy to help as soon as possible.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Code Example: Remove The First Item From List Python
  2. 4 Ways To Loop Through List With Index Using Python
  3. Code Examples: Multiply In Python
  4. How To Join Words In a List Using Python Code Example
  5. Python Code Example: How To Find Element in a List in 6 Way
  6. How To Test For An Empty Object In JavaScript
  7. How To Iterate Over Rows in a DataFrame in Pandas Python
  8. Python Get Filename From A Path Without Extension 4 Ways

Leave a Comment