► Write clean and Pythonic for, while, and “for each“ loops in Python
One of the easiest ways to spot a developer with a background in C-style languages who only recently picked up Python is to look at how they loop through a list.
In this tutorial you’ll learn how to take a “C-style“ (Java/PHP/C/C ) loop and turn it into a nice and “native“ Python loop.
You can use these techniques to refactor your existing Python for loops and while loops in order to make them easier to read and more maintainable.
You’ll learn how to use Python’s range()/xrange() and enumerate() built-ins to refactor your loops and to avoid having to keep track of loop indexes manually.
The main takeaways in this tutorial are:
• Writing C-style loops in Python is considered unpythonic. Avoid managing loop indexes and stop conditions manually if possible.
• Python’s for-loops are really “for-each” loops that can iterate over items from a container or sequence directly.
Read the complete tutorial at →
To get more Python Tricks and to discover the full potential of Python check out “Python Tricks: The Book“ at the link below.
FREE COURSE – “5 Thoughts on Mastering Python“
PYTHON TRICKS: THE BOOK
SUBSCRIBE TO THIS CHANNEL:
* * *
► Python Developer MUGS, T-SHIRTS & MORE:
FREE Python Tutorials & News:
» Python Tutorials:
» Python News on Twitter: @dbader_org
» Weekly Tips for Pythonistas:
» Subscribe to this channel: