Take the 2-minute tour ×

I often find myself writing if / elif / else constructs in python, and I want to include options which can occur, but for which the corresponding action is to do nothing. I realise I could just exclude those if statements, but for readability I find it helps to include them all, so that if you are looking through the code you can see what happens as a result of each option. How do I code the no-op? Currently, I'm doing it like this:

no_op = 0

if x == 0:
    y = 2 * a
elif x == 1:
    z = 3 * b
elif x == 3:

(The code is actually quite a bit longer than that, and more complicated. This is just to illustrate the structure).

I don't like using a variable as a no-op, but it's the neatest way I could think of. Is there a better way?

share|improve this question

4 Answers 4

up vote 93 down vote accepted

Use pass for no-op:

if x == 0:
  print "x not equal 0"

And here's another example:

def f():


class c:
share|improve this answer
Thank you! That's what I was looking for. –  Ben Mar 27 '09 at 17:09
+1: Reference the documentation –  S.Lott Mar 27 '09 at 18:46
As a one-liner, i use doSQL = lambda *x:x. –  Cees Timmerman Jan 30 at 11:09

How about pass?

share|improve this answer

For references for the pass command see: http://www.network-theory.co.uk/docs/pytut/passStatements.html

share|improve this answer

Some useful materials for similar questions, in my order of preference:

The official python tutorial

Think Python

Dive into Python

Learning Python

share|improve this answer

Your Answer


