Re: for loop

2004-12-12 Thread loritsch
houbahop wrote:
> Hello,
> I have seen a lot of way to use the for statement, but I still don't
know
> how to do:
>
> for i=morethanzero to n
> or
> for i= 5 to len(var)
>
> of course I kno wthat I can use a while loop to achieve that but if
this is
> possible whith a for one, my preference goes to the for.
>
> regards,
> Dominique.

I think the key point that hasn't been made here is what a for
statement is really doing in python...

>From the online documentation:

"The for statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object"

Neither of the statements:
> for i=morethanzero to n
> for i= 5 to len(var)
create a sequence or iterable object, and that is why they don't work.

That is why previous posts in this thread have suggested using range(),
xrange(), etc.  Because they create a sequence or iterable object.
When first using python, I recall that this distinction was not clear
to me, as I was used  to a more traditional for statement (as in
C/C++):

for ( initialise ; test ; update )

Essentially, python's for statement is more like a foreach statement in
many other languages (Perl, C#, etc).  These statements essentially
reduce the traditional form above to what many consider a more readable
form:

foreach item in collection:

In order to transform the tradition for statement into this more
readable form, each language requires that their collections being
iterated over satisfy a precondition defined by the language (in python
this precondition is that the collection is iterable).

While this restriction may seem a little strange to people who are used
to the more traditional for statement form, the result of this approach
is often a more readable and clear statement.
Regards,

Michael Loritsch

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python vs. Perl

2004-12-11 Thread loritsch
Christopher De Vries wrote:
> Roy Smith already touched on regular expressions, but as far as
> features go, I would say that the real difference between python and
> perl is not in the features, but in the philosophy.

To help aid in this discussion, the following Python and Perl
philosophy links might be useful:
http://c2.com/cgi/wiki?PythonPhilosophy
http://www.maths.adelaide.edu.au/~cmc/tutorials/perlintro/x175.html

Now, so that I don't start another Python vs. Perl flamewar, I'd like
to inform everyone that I'm about to make a few generalizations based
on my experience.  As should be understood implicitly, one man's
experience is not the experience of everyone...

As a user of both languages, I've found that what Perl and Python
programmers have in common is that they were looking for a better tool
when they stumbled across their language of choice...  After all, one
can be productive in both languages.

What I've also noticed that people who use Perl tended to start using
it as a way to make either C or shell scripting tasks easier (after
all, this is Perl's original intended audience).  Many of these
developers have gone on to use Perl for bigger and better things, but
without a lot of discipline (use strict, and a lot of work with the
Exporter), Perl doesn't scale very well to large projects.  My belief
is that Perl's strength (TMTOWTDI) is also it's greatest weakness.

I've also noticed that Python programmers tend to be a more diverse
lot.  While Guido originally intended Python to be a second language
for C/C++ developers, it is also quite useful as a first language.
Python's philosophy is more that there should a clear understandable
way to do things, and that readability counts.  That is not to say you
can't perform tasks in multiple ways, it is just to say that Python
doesn't believe in TMTOWTDI as Perl does.

So the bottom line is this.  In choosing Perl or Python, the real
difference should be your mindset, and what you intend to use it for.
If you want a multiparadigm programming language that offers wonderful
OO support, is easy to learn, and in which you will naturally write
maintainable code, choose Python.

On the other hand, if you are looking for a language to text-processing
and to perform short quick shell scripting like tasks, choose Perl.
While both languages can be used to perform both sets of tasks, my
belief is that one should pair a language and a task by strengths
rather than what can be done in each language.
I hope this helps!

Michael Loritsch

-- 
http://mail.python.org/mailman/listinfo/python-list