You are invited to Log in or Register a free Frihost Account!

# Python Sequences: Getting length, min, max

Getting length, minimum or maximum from a sequence is quiet simple thru usage of the len(), min() and max() method:
 Code: >>> seq = (100,1,2,10,5,50) >>> seq (100, 1, 2, 10, 5, 50) >>> len(seq) 6 >>> min(seq) 1 >>> max(seq) 100

Does this also work for a sequence of strings ? You can guess the answer: it does !
 Code: >>> seq2 = ('Ted','Joe','Richard','Jim','Orlando','Axel') >>> seq2 ('Ted', 'Joe', 'Richard', 'Jim', 'Orlando', 'Axel') >>> len(seq2) 6 >>> min(seq2) 'Axel' >>> max(seq2) 'Ted' >>>

Does this also work for strings ? It has to, since strings are sequences.
 Code: >>> str = "Hello World !" >>> len(str) 13 >>> min(str) ' ' >>> max(str) 'r' >>>

Is it possible to get the average of all numbers in a sequence ? Nope.
 Code: >>> avg(seq) Traceback (most recent call last):   File "", line 1, in     avg(seq) NameError: name 'avg' is not defined

Unless you do this, which is already some advanced usage of Python:
 Code: >>> reduce(lambda x, y: x + y, seq) / len(seq) 28