Re: [Tutor] How and where to use pass and continue

2005-03-27 Thread Kent Johnson
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":
  break
Kent
Thanks
Kevin
On Sun, 27 Mar 2005 22:13:45 -0500, Kevin <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> wrote:
On Sun, 27 Mar 2005 20:37:02 -0500, Kevin <[EMAIL PROTECTED]> 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.
Kevin,
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"
PASS
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.
CONTINUE
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.
Peace
Bill Mill
bill.mill at gmail.com

Thanks
Kevin
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How and where to use pass and continue

2005-03-27 Thread Sean Perry
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 ?
'test' must be given a value before Python will use it.
Without the first assignment, the program will fail to execute the first 
time it reaches the unknown variable.

Think of it as riming the pump.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How and where to use pass and continue

2005-03-27 Thread Kevin
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 ?

Thanks
Kevin


On Sun, 27 Mar 2005 22:13:45 -0500, Kevin <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> wrote:
> > On Sun, 27 Mar 2005 20:37:02 -0500, Kevin <[EMAIL PROTECTED]> 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.
> >
> > Kevin,
> >
> > 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"
> >
> > PASS
> >
> > 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.
> >
> > CONTINUE
> >
> > 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.
> >
> > Peace
> > Bill Mill
> > bill.mill at gmail.com
> >
> > >
> > > Thanks
> > >
> > > Kevin
> > > ___
> > > Tutor maillist  -  Tutor@python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> > >
> >
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How and where to use pass and continue

2005-03-27 Thread Kevin
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 <[EMAIL PROTECTED]> wrote:
> On Sun, 27 Mar 2005 20:37:02 -0500, Kevin <[EMAIL PROTECTED]> 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.
> 
> Kevin,
> 
> 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"
> 
> PASS
> 
> 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.
> 
> CONTINUE
> 
> 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.
> 
> Peace
> Bill Mill
> bill.mill at gmail.com
> 
> >
> > Thanks
> >
> > Kevin
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How and where to use pass and continue

2005-03-27 Thread Bill Mill
On Sun, 27 Mar 2005 20:37:02 -0500, Kevin <[EMAIL PROTECTED]> 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.

Kevin,

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"

PASS

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.

CONTINUE

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.

Peace
Bill Mill
bill.mill at gmail.com

> 
> Thanks
> 
> Kevin
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How and where to use pass and continue

2005-03-27 Thread Sean Perry
Kevin 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.
language idioms are one of the hardest things to learn and only really 
come after having written code and then coming back to it later.

My thoughts on the subject.
continue is a common idiom in I/O routines.
for line in fp.xreadlines():
line = line.strip()
if not line: continue
# process line data here
pass is an odd one. I tend to use it while prototyping to set up the 
bones of control statements to be.

def func(one, two):
if sometest(one):
pass # need to actually do something here
# work with one and two
pass can also be used in multi-catch functions / control statements
if input == 'something':
handle_something(input)
elif input == 'other':
pass
else:
# do stuff here
We want to ignore 'other' for some reason, so just use pass. Also handy 
if a value could be one of N choices but we only handle a few cases. 
This way someone else reading the code does not think "hey wait, they do 
not check for 'foo'!".
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Fwd: [Tutor] How and where to use pass and continue

2005-03-27 Thread Liam Clarke
Oops, forward to list as well.


-- Forwarded message --
From: Liam Clarke <[EMAIL PROTECTED]>
Date: Mon, 28 Mar 2005 14:22:00 +1200
Subject: Re: [Tutor] How and where to use pass and continue
To: Kevin <[EMAIL PROTECTED]>


Hi Kevin,

I generally use pass as a placeholder -

if you have a function that you want to define so you can use it
without creating syntax errors, but you haven't written it yet, it's
just a case of -

def neededFunc(x):
 pass

continue is good.

say you have a loop -

for i in range(10):
  if i % 2 == 0:
  continue
  print i * 3

So, what the loop does, is it gets i, checks if i divided by 2 has
zero remainder and if it does, it continues.

Which, basically means, it doesn't do anything else in the loop (i.e.
print i * 3), it just goes back, get the next value of i, and go
through the loop again.

You'll find you don't need continue that often. It's good for checking
an value in a list you're looping through meets your criteria before
passing it to a big function.

That's about it.

HTH,

Liam Clarke


On Sun, 27 Mar 2005 20:37:02 -0500, Kevin <[EMAIL PROTECTED]> 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.
>
> Thanks
>
> Kevin
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

--
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How and where to use pass and continue

2005-03-27 Thread Kevin
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.

Thanks

Kevin
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor