[Tutor] Using break, else with for loop

Alan Gauld alan.gauld at yahoo.co.uk
Sun Oct 11 04:02:03 EDT 2020


On 11/10/2020 08:58, Alan Gauld via Tutor wrote:

> That's a valid solution.
> A slightly shorter version is:
> 
> if any(n%2 for n in l):
>    print("Odd no found in list")

I meant to add, that this has the advantage of explicitly
expressing the intent of the code in the function.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list