[Tutor] Built In Functions

Alan Gauld alan.gauld at btinternet.com
Mon Dec 16 18:15:28 CET 2013


On 16/12/13 15:28, Rafael Knuth wrote:

> First question: all(iterable) and any(iterable) - can you give me one
> or two examples what these functions do and how they are specifically
> used?

In my experience they aren't used all that often. But the logic is 
straightforward. all(seq) returns true if everything in the eq is
true

 >>> all([1,2,3,4,5]
True
 >>> all([0,1,3,5])
False
 >>> all("testing")
True
 >>> all('')
True

Actually the last one surprised me. I expected false. So maybe a 
resident guru can explain that anomaly... I assume it works on the basis 
of testing until it finds a false and so an empty sequence
always returns true...

any(seq) returns true if any member of seq is true so:

 >>> any([0,0,0,0])
False
 >>> any([1,0])
True
 >>> any([isPrime(n) for n in range(8,11)])
False
 >>> any([isPrime(n) for n in range(8,15)])
True
 >>> any('')
False
 >>> any('fgfhgf')
True
 >>>

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list