Often when we use a for-loop in Python, we tend not to modify the loop variable inside the loop.
The impulse typically comes from acquaintance with other programming languages like C++ and Java.
But for-loops don't work that way in Python. Modifying the loop variable has no effect on the iteration.
This is because, before every iteration, Python unpacks the next item provided by iterable (𝐫𝐚𝐧𝐠𝐞(5)) and assigns it to the loop variable (𝐧𝐮𝐦).
Thus, any changes to the loop variable are replaced by the new value coming from the iterable.
Share this post on LinkedIn: Post Link.
Find the code for my tips here: GitHub.
I like to explore, experiment and write about data science concepts and tools. You can read my articles on Medium. Also, you can connect with me on LinkedIn.
Wow!! I had no clue! and have been using Python for years. thanks for sharing Avi.