[Tutor] How and where to use pass and continue

Kent Johnson kent37 at tds.net
Mon Mar 28 05:57:16 CEST 2005

Kevin wrote:
> Ok I have another question now I noticed that at the tope of a while
> loop there will be somthing like this:
> test = None
> while test != "enter":
>          test = raw_input("Type a word: ")
>          if test == "enter":
>               break
> what is the purpose of test = None ?

Otherwise you will get a NameError the first time the while is executed because test is not defined.

Personally I prefer this form of the loop which is shorter and doesn't duplicate the test:

while True:
          test = raw_input("Type a word: ")
          if test == "enter":


> Thanks
> Kevin
> On Sun, 27 Mar 2005 22:13:45 -0500, Kevin <python.programming at gmail.com> wrote:
>>That was a great help I understand now what they do and how to use
>>them. Thanks alot for all your help.
>>On Sun, 27 Mar 2005 21:43:45 -0500, Bill Mill <bill.mill at gmail.com> wrote:
>>>On Sun, 27 Mar 2005 20:37:02 -0500, Kevin <python.programming at gmail.com> wrote:
>>>>I am having lot of trouble learning where and when to use pass and
>>>>continue. The two books that I use don't explian these very good. Is
>>>>there a website the explains these is great detail?
>>>>I have also looked at the python tutorial as well.
>>>I'll try to help you out - pass and continue are pretty simple
>>>concepts. Consider the following code snippet which I will try to use
>>>to explain both:
>>>command = None
>>>while command != '3':
>>>    command = raw_input("Press 1 to pass, 2 to continue, or 3 to exit ")
>>>    if command == '1':
>>>        print "passing"
>>>        pass
>>>    elif command == '2':
>>>        print "continuing"
>>>        continue
>>>    else:
>>>        print "othering"
>>>    print "end of loop reached"
>>>print "exiting"
>>>The 'pass' statement simply means 'do nothing'. In the example above,
>>>when the python interpreter encounters the pass statement, it simply
>>>continues with its execution as it normally would.  It is usually used
>>>as the only statement in the body of an if statement to denote
>>>explicitly that nothing is to be done. I will often use it as a
>>>placeholder so that a program compiles correctly, like:
>>>if 'a':
>>>    do_something()
>>>elif 'b':
>>>    #TODO: implement do_something_else()
>>>    pass
>>>elif 'c':
>>>    quit_foo()
>>>Without the pass statement, there are no statements in the second
>>>block, and python will raise a SyntaxError.
>>>In the first example above, Python sees the pass, exits the series of
>>>'If...elif..." conditions, advances to the final statement of the
>>>while loop, prints "end of loop reached", and resumes execution at the
>>>top of the loop.
>>>The continue statement means what it says - continue with the loop,
>>>but resume execution at the top of the loop. In the case of a while
>>>loop, the exit condition will be evaluated again, and execution
>>>resumes from the top. In the case of a for loop, the item being
>>>iterated over will move to its next element. Thus,
>>>for i in (1,2):
>>>    print i
>>>    continue
>>>    print "we never get here"
>>>Will print 1, hit the continue, update i to the value 2, print 2, hit
>>>the continue, and exit because there are no more iterations for i.
>>>In the first example I gave, after python reaches the continue,
>>>'command' is again evaluated to see if its value is 3, then the loop
>>>proceeds from the top down. If you run the example, you should be able
>>>to figure out what's going on.
>>>There are a couple more wrinkles - for example, continue only works on
>>>the innermost loop in its execution context - but generally, they work
>>>as you expect. The longer you work with python, the more you'll find
>>>this to be the case, but I'm biased.
>>>Hope this helps, and feel free to ask questions about what you don't understand.
>>>Bill Mill
>>>bill.mill at gmail.com
>>>>Tutor maillist  -  Tutor at python.org
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list