Re: [Tutor] Differences between while and for

2019-06-15 Thread Alan Gauld via Tutor
On 15/06/2019 05:53, mhysnm1...@gmail.com wrote:

> In C, Perl and other languages. 

As a point of interest not all languages have these constructs.
Oberon, for example, only has a while loop because it can be
used to simulate all other loop types. Some Lisp dialects
don't even have a loop construct because recursion can be
used instead.

In addition to for, while and repeat/until some languages
(eg ADA and some BASICs) include a general loop construct.

And of course in assembler GOTO is the ultimate loop construct.

Don;t assume that just because one language supports a
particular construct that others will or should also support
it. The variety of control structures offered is one of the
defining features of any programming language.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Differences between while and for

2019-06-15 Thread Cameron Simpson

On 15Jun2019 14:53, Sean Murphy  wrote:
In C, Perl and other languages. While only uses a conditional statement 
and

for uses an iteration. In python while and for seems to be the same and I
cannot see the difference.


No, they're really much as in other languages.

In general (most languages), a for loop is for iterating over some 
collection or list.  A while is not explicitly for iteration over some 
collection, it is for repeating an action until some condition fails (or 
the inverse of the condition is achieved).


Let's take C's for loop. It really is closely related to a while. That's 
because C is a pretty low level language. Early C is almost like a 
structured assembly language (well, it is a lot better, but it is 
deliberately close to the underlying machine). So C's for loop goes:


 for (setup; condition; advance)
   statement-or-block

You can leave any of these out. It is equivalent to this while loop:

 setup
 while condition:
   statement-or-block
   advance

but it is almost always used for iteration:

 s="foo"
 for (i=0; s[i]; i++)
   ...

which counts "i" along the string "s". You _can_ use of for arbitrary 
while loops, but idiomatically that is rarely done - it is conceptually 
useful to use "for" for various kinds of iteration and "while" for more 
arbitrary repetition.


Python is a bit more rigid. The "while" loop is just like "while" in 
other languages: do something while a condition holds. But a "for" loop 
in Python is inherently about iteration; it is defined as:


 for variable in iterable:
   suite

and applies to any "iterable", some object or expression which can be 
iterated over. Any object which is iterable may be used. So it is very 
oriented towards collections of various kinds: lists, tuples, dictionary 
(iterates over the keys) and so on.



Python does not have an until (do while) where
the test is done at the end of the loop. Permitting a once through the loop
block. Am I correct or is there a difference and if so what is it?


You're correct.


Why doesn't Python have an until statement?


Basicly because it isn't necessary. It is usually easy enough to work 
around the lack that nobody has made a conincing case (meaning nobody 
has convinced the core developers). It would probably be written:


 do:
   ...
 while condition

in some form if it ever came in to avoid using an new keyword ("until").

It does sometimes take a little contortion to make a do/while loop into 
a Python while - you usually have to perform some kind of hack to make 
the condition initially true. In the extreme case you just treat the 
first loop specially:


 first = True
 while first or the-actual-condition:
   ... do stuff ...
   first = False

if you want to use "first" during the "do stuff". Or you could be a bit 
more reliable and go:


 first_test = True
 while first_test or the-actual-condition:
   first_test = False
   ... do stuff ...

putting the flag up the top next to the condition.

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Differences between while and for

2019-06-15 Thread Steven D'Aprano
On Sat, Jun 15, 2019 at 02:53:43PM +1000, mhysnm1...@gmail.com wrote:
> All,
> 
>  
> 
> In C, Perl and other languages. While only uses a conditional statement and
> for uses an iteration. In python while and for seems to be the same and I
> cannot see the difference.

Python ``while`` uses a conditional statement, and Python ``for`` uses 
iteration. Python's ``for`` is like "foreach" in some other languages.

while condition: ...

for x in values: ...


> Python does not have an until (do while) where
> the test is done at the end of the loop. Permitting a once through the loop
> block. Am I correct or is there a difference and if so what is it?

Correct, there is no "do until" in Python.

> Why doesn't Python have an until statement?

Because Guido didn't want one :-)

Because it is unnecessary: any "do until" can be written as a regular 
while loop, using a break:

# do...until with test at the end
while True:
do_something()
if test:
   break


# "loop and a half"
# https://users.cs.duke.edu/~ola/patterns/plopd/loops.html#loop-and-a-half
while True:
do_something()
if test:
break
do_something_else()



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Differences between while and for

2019-06-15 Thread mhysnm1964
All,

 

In C, Perl and other languages. While only uses a conditional statement and
for uses an iteration. In python while and for seems to be the same and I
cannot see the difference. Python does not have an until (do while) where
the test is done at the end of the loop. Permitting a once through the loop
block. Am I correct or is there a difference and if so what is it?

 

Why doesn't Python have an until statement?

 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor