Towards smart things and smarter people

A blog about research and development work
Photo by [Chris Ried]( on

Python for loops have an else clause

Something that is not common knowledge is the fact that Python for loop can have an else clause. An else clause is executed when a for loop ends normally without break being called. Internally, Python will trigger a StopIteration when this happens. def check(target, iterable): for element in iterable: if element == target: print("Jup, target is in container.") break else: print("Seems like target is not in container.") check("s", "a string") # >>> Jup, target is in container....

September 22, 2022 · Alexander Neumann

About Me

My name is Alexander Neumann and I am currently working at CareTech OWL as a PostDoc Researcher. Photo by Patrick Pollmeier

September 21, 2022 · Alexander Neumann

Legal Notice

Disclaimer Opinions expressed are solely my own and do not express the views or opinions of my employer. Privacy Information/GDPR Notice Personal Data This web site does not collect, store, or process personal data. It does not require the use of cookies. Privacy Policy This web site is hosted on Codeberg Pages. The Codeberg Privacy Statement applies. External resources Through this site, you may access some resources that are hoste externally, from the following sources:...

September 21, 2022 · Alexander Neumann