for loops in python are bunk

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

cytg111

Lifer
Mar 17, 2008
23,334
12,946
136
it is just semantics, does not make programming language that bad. python is quick and dirty way to get something done. bordeline scripting language.

- I am sorry, but any intellectual hippie pot smoking computer-science-PHd-whatever that gets the urge to reinvent the for-loop is retarded & stoned out. IMO.

Likewise, many programmers, while coding apps against a rdbms, will get the urge to (re)invent a persistence framework. Dont. Just dont. (That should be a mandatory teaching in any general programming course slash book)
 

MooseNSquirrel

Platinum Member
Feb 26, 2009
2,587
318
126
Your professor is funny. Why hate a programming language?

C++ is faster than Python, as a rule.

Python takes less time to code in, as a rule.

Pick your poison.

Whats that rule again?

You can have it cheap, fast, and good. Pick 2.
 
Dec 30, 2004
12,554
2
76
- I am sorry, but any intellectual hippie pot smoking computer-science-PHd-whatever that gets the urge to reinvent the for-loop is retarded & stoned out. IMO.

The whole range business makes a lot more sense if you think about it from the perspective of Mathcad, Matlab, Mathematica, and tuples in general.

You create the range of elements over which you want to iterate.
Then you iterate.
It's simply a different syntax, which if you've never seen before, is annoying and makes you go WTF???? as the OP is doing. He's not wrong for doing that, he just hasn't had the "ah ha!" moment where it makes sense why, for certain solutions, it would make more sense to express the for loop this way. You simply specify an array of elements that you want to iterate data for. Our first assignment in python taught us this for graphing data sets, I think. So you think of it like x=1, x= 2, x=3, and I want the y values for these x guys, well what range of x values do I want graphed? range (-5, -1, -1).

Once you've memorized the parameters as "(start, stop, step)" it won't bother you as much.

Don't listen to esun, he's being a pedant. There's nothing wrong with preferring the for loop.

Hm, I wonder what the reasoning was for making the "stop" parameter exclusive not inclusive? -5 to -1 for stopping at 0 _is_ a bit confusing....
 
Last edited:

Pia

Golden Member
Feb 28, 2008
1,563
0
0
Hm, I wonder what the reasoning was for making the "stop" parameter exclusive not inclusive? -5 to -1 for stopping at 0 _is_ a bit confusing....
http://stackoverflow.com/questions/4504662/why-does-rangestart-end-not-include-end

edit: and also (I don't think this was discussed in the SO thread) representing empty ranges seem much tidier to me with exclusive ranges, because all your indexes stay non-negative. With inclusive ranges you'd have to do stuff like range(0, -1) to represent empty range. In many languages you wouldn't be using signed integers as indices in the first place.
 
Last edited:

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Don't listen to esun, he's being a pedant. There's nothing wrong with preferring the for loop.

Fascinating, I don't recall saying that there is anything wrong with preferring C's for loop. And there's really no reason to bring in other languages, if the OP understands C++ and Java, he presumably knows about iterators, which are basically the same thing.
 

Check

Senior member
Nov 6, 2000
367
0
0
I don't know about other people, but I use While loops in place of where I would typically use a For loop in C style languages.

I tend to only use python's For loop when I am indexing through an array and a couple other tasks where it makes for easy coding.
 

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
I prefer Java / C style for loops.......I guess you can probably get used to whatever style, but visually they are the most appealing to me
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
I like the FOR loops in vba. Looping through objects by simply using an object. Brilliant!
 

irishScott

Lifer
Oct 10, 2006
21,568
3
0
You're complaining cause it's different, not cause it's worse.

Define "worse". For my money, the unnecessarily cryptic solution is the worse solution. Especially in a language that supposedly prides itself on its explicitness.
 

ObscureCaucasian

Diamond Member
Jul 23, 2006
3,934
0
0
Define "worse". For my money, the unnecessarily cryptic solution is the worse solution. Especially in a language that supposedly prides itself on its explicitness.

IMO python loops are much less cryptic than C style for loops, you just have a preconceived notion of what is obvious based on your past experiences with other languages.

Python for loops make iterating over a collection much more readable as well.
 

irishScott

Lifer
Oct 10, 2006
21,568
3
0
IMO python loops are much less cryptic than C style for loops, you just have a preconceived notion of what is obvious based on your past experiences with other languages.

Python for loops make iterating over a collection much more readable as well.

Fortunately this point is provable. So let's assume you never looked at for loops before, and have no available reference.

for(int i = 5; i >= 0; i--)
Here you have explicit logical conditions. You may not know what -- means, but you can understand, with zero programming knowledge, that something called i is equal to 5 and it is greater than or equal to zero.


for i in range(5, -1, -1)
Here you have this "range" thing. You can speculate that it has something to do with the range of i, but the meaning of the numbers is absolutely unknown without reference.
 

ObscureCaucasian

Diamond Member
Jul 23, 2006
3,934
0
0
The common case is python (for me) is to iterate over collections, hence:
for thing in things:
Which is a pretty natural/readable for-loop.

I also disagree about the clarity of a C style for loop if you have no programming experience... how is someone with zero programming knowledge supposed to know what the 3 sections of the for loop mean?
 

irishScott

Lifer
Oct 10, 2006
21,568
3
0
The common case is python (for me) is to iterate over collections, hence:
for thing in things:
Which is a pretty natural/readable for-loop.

I also disagree about the clarity of a C style for loop if you have no programming experience... how is someone with zero programming knowledge supposed to know what the 3 sections of the for loop mean?

Yes, for iterating over collections python is more intuitive (assuming you want to access the items and don't care about their indices). I usually find myself requiring the indices at some point, making this feature something of a side-show but I guess that depends on application.


They won't know what the 3 sections of the loop mean. But they'll see "i = 5" and "i >= 0". Assuming the person graduated elementary school and has an IQ above 70, I would hope they would be able to at least speculate that i is somewhere between 5 and 0 (inclusive). If they've taken Algebra I they might further infer that "int" stands for "integer". With some pattern recognition they could even infer that -- decrements the loop, especially if the loop is written more explicitly: for(int i = 5; i >= 0; i = i - 1). The only truly alien language is "for", and if they extrapolate the rest of the loop they could even infer that it means "for some number of iterations".

With python, you have the word "range" and 3 numbers of completely unknown meaning. There is no way to figure out what this means without reference (short of trial and error).

It's like deciphering French when you already know Latin, vs deciphering Egyptian hieroglyphs when you know nothing (until the Rosetta stone comes along).
 

mv2devnull

Golden Member
Apr 13, 2010
1,500
145
106
I would presume that the Python has a manual that is more approachable than the Rosetta stone?

(On the other hand reading Stroustrup or the Standard and then expecting to do C++ falls short on non-compliant compilers.)
 

irishScott

Lifer
Oct 10, 2006
21,568
3
0
I would presume that the Python has a manual that is more approachable than the Rosetta stone?

(On the other hand reading Stroustrup or the Standard and then expecting to do C++ falls short on non-compliant compilers.)

Yes, although its documentation isn't all that polished and leaves a little to be desired IMO. My point was that it is possible for someone with zero programming knowledge and a little intellect to extrapolate the meaning of the c-style for-loop. It is impossible to do so for python when the range function is used.

Aside from specialized embedded work (where I would hope the developers would know enough to compensate), who uses a non-standard compiler?
 

Zxian

Senior member
May 26, 2011
579
0
0
I'll agree that you're more likely to "decipher" a C-style for loop than you would a python for/range statement, but it does come down to a matter of learning and preference. When you step back and look at the for statement as "for item in list", it becomes very simple to decipher. You just need to build the list first - something that C-loops do for you implicitly. Your example in your first post is comparing apples to oranges, even though both do the same thing.

The for statement in python becomes far more flexible than a for loop in C as you do not need to explicitly enumerate each item you wish to iterate over. Build a range (or more specifically, a list of objects), and do something for each of them.

Alternatively, if we go back to your original example, I could build the loop as follows:

for i in reverse(range(0,6))

The only thing you'd have to know about the range function is that the end point is not included in the list. Otherwise, the above statement is far more "readable" than any C-loop you could think of (once you understand the concept of a list).
 

mv2devnull

Golden Member
Apr 13, 2010
1,500
145
106
There is neither loop nor range in abs, but the example in that documentation is clearly wrong too (unless that Arduino language differs deviously). @irishScott: By "non-compliant" I did mean all compilers that do not implement 100% of the Standard. In C++ that covers almost all compilers.
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
There is neither loop nor range in abs, but the example in that documentation is clearly wrong too (unless that Arduino language differs deviously). @irishScott: By "non-compliant" I did mean all compilers that do not implement 100% of the Standard. In C++ that covers almost all compilers.
Does even a single C++11 compliant compiler actually exist? I'm willing to entertain the possibility that someone has a slow and impractical one in a lab, but no major compiler is even close to compliant.
 

Kilroy5100

Junior Member
Dec 23, 2021
1
0
6
>for i in range(5, -1, -1)
Here you have this "range" thing. You can speculate that it has something to do with the range of i, but the meaning of the numbers is absolutely unknown without reference.

No they're obvious if you have a BASIC/Fortran background, the equivalent in Fortran is :

FOR I=5 TO 0 STEP -1
NEXT I

it just reads straight across from Fortran, - start, end, step - easy.

The reason you have to end on -1 is that the loop has to go around another time to end at 0, because in Python it only branches out at the top, in Fortran it branches out at NEXT.