[Tutor] Using break, else with for loop

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


On 11/10/2020 09:07, Manprit Singh wrote:
> Dear sir ,
> 
> The point I want to raise here is , in my program I am using break because
> I want my program 's for loop to break at a point where an odd number is
> found .
> But in your code  you are checking the entire list .
> Need your comments

I'm not sure that's true since I would hope that any() is optimised
to break when it encounters any true value. Also since any() is
a built in function it is written in C so should be faster than
pure Python(assuming it does break). Finally, as I've repeatedly
said, readability is one of the most important features of
good code. any() exactly expresses the purpose of the code
which improves maintainability

-- 
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