In this video, we look at a lesser known feature of Python for and while loops: they have an "else" clause.
You can use the else on a loop to indicate whether a loop completed successfully or not.
We give some practical examples so you see how to leverage this structure in your Python code.
Additionally, we learn how to utilize the Abstract Syntax Tree (AST) to quickly identify this construct in Python files.
We use it in a script - linked below - to find use cases of our own as well as examples from 3rd party libraries (e.g. Django + Pillow).
Links:
for-else.py gist to search for this construct in code using AST: https://gist.github.com/bbelderbos/f4...
Raymond Hettinger on the history of the for ... else construct: • Transforming Code into Beautiful, Idi... (found via https://stackoverflow.com/a/23748240 / watch his whole talk, it's really good! Notes: https://pybit.es/beautiful-python.html)
For vs while beginner video on this channel: • For vs while loops in Python: when to...
---
Learn more about Pybites / what we do: https://pybit.es
Get our weekly developer / mindset emails: https://pybit.es/friends/
Join our Slack community: https://pybit.es/community/
And last but not least, we appreciate any feedback to make our YouTube content better 💡
https://form.jotform.com/230941798741062 🙏
Sur cette page du site, vous pouvez voir la vidéo en ligne Python Loop Mastery: Demystifying the Else Clause in For and While Loops durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Pybites 22 mai 2023, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 199 fois et il a aimé 13 téléspectateurs. Bon visionnage!