69

How do I get a Python program to do nothing with if statement?

 if (num2 == num5):
     #No changes are made
1
  • 1
    @Polli Ester If one of the answers below helped you, it would be good to accept it (click the tick underneath the score for it).
    – rlms
    Oct 29, 2013 at 10:34

5 Answers 5

139

You could use a pass statement:

if condition:
    pass

Python 2.x documentation

Python 3.x documentation

However I doubt you want to do this, unless you just need to put something in as a placeholder until you come back and write the actual code for the if statement.

If you have something like this:

if condition:        # condition in your case being `num2 == num5`
    pass
else:
    do_something()

You can in general change it to this:

if not condition:
    do_something()

But in this specific case you could (and should) do this:

if num2 != num5:        # != is the not-equal-to operator
    do_something()
21

The pass command is what you are looking for. Use pass for any construct that you want to "ignore". Your example uses a conditional expression but you can do the same for almost anything.

For your specific use case, perhaps you'd want to test the opposite condition and only perform an action if the condition is false:

if num2 != num5:
    make_some_changes()

This will be the same as this:

if num2 == num5:
    pass
else:
    make_some_changes()

That way you won't even have to use pass and you'll also be closer to adhering to the "Flatter is better than nested" convention in PEP20.


You can read more about the pass statement in the documentation:

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

if condition:
    pass
try:
    make_some_changes()
except Exception:
    pass # do nothing
class Foo():
    pass # an empty class definition
def bar():
    pass # an empty function definition
3

you can use pass inside if statement.

1
  • 5
    I think you could have put a little bit more effort into this "answer". It looks more like a comment...
    – Lix
    Oct 28, 2013 at 11:02
-4
if (num2 == num5):
    for i in []: #do nothing
        do = None
else:
    do = True

or my personal favorite

if (num2 == num5):
    while False: #do nothing
        do = None
else:
    do = True
2
  • Consider adding an explanation of why you believe this answer is more useful than the accepted one.
    – gshpychka
    Aug 18, 2021 at 6:43
  • This is a terrible solution. It actually is doing something by setting the do variable.
    – Nelson
    Jun 28 at 3:14
-8

You can use continue

if condition:
    continue
else:
    #do something
1
  • 1
    In case you don't understand why you got voted down, the reason is because your answer is wrong. The continue statement is used to change the execution of a loop, not to break out of an if statement. If you had tried the solution you offered, you would have discovered that Python complains about incorrect syntax. Dec 9, 2020 at 7:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.