Discover more from Daily Dose of Data Science
Most Python Programmers Don't Know This About Python For-loops
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.
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new about Python and Data Science every day.
Find the code for my tips here: GitHub.