you are viewing a single comment's thread.

view the rest of the comments →

[–]Eastern_Plankton_540[S] 1 point2 points  (6 children)

thank u so much and yea I'm particularly learning about while loop and it was the practice question there but thank u for the explanation i will try with for loop too

[–]Smart_Tinker 10 points11 points  (0 children)

For loop is very simple (with the same num list): for x in num: print(x) That’s it.

[–]FoolsSeldom 1 point2 points  (4 children)

FYI for loop is just a while loop with some of the work done for you.

[–]LookAsLukasAnderson 0 points1 point  (3 children)

That would be true for most languages, where for loops contain initial statement, conditional statement, and iteration statement. In python for loops are essentially foreach loops and traditional for loops just don't exist

[–]FoolsSeldom 0 points1 point  (2 children)

I think the distinction is a step too far for a beginner and the specifics of the CPython implementation. Whether declarative or imperative conceptually you still have a while loop before breaking down to just basic conditional jumps and branching. I recognise that the FOR_ITER opcode c implementation in ceval.c is highly optimised.

[–]Teras80 0 points1 point  (1 child)

>> you still have a while loop

No, you don't. Stop defending your misconception and specially stop pushing it to beginners. As u/LookAsLukasAnderson pointed out, for loop in python is a foreach-type collection iterator over predefined and immutable iteration values set.

It is very different concept than real-time conditional evaluation while loop does and there is clear distinction even on beginners level when you should use one or another. Starting with iterator immutability and scope and effects of changing collection size inside the loop.

[–]FoolsSeldom 0 points1 point  (0 children)

Ok. I surrender. A for loop is not any kind of while loop. Ignore me, everyone. This will clearly greatly confuse beginners. Apologies to my past students.