[Tutor] same ol s
Alan Gauld
alan.gauld at blueyonder.co.uk
Thu Aug 12 08:53:14 CEST 2004
> Well, you can use len(list) to determine if its even or odd:
>
> ###
> if str(len(list)/2.0)[-1] != 0:
> print 'Odd number of elements.'
> else:
> print 'Even number of elements.'
> ###
Eek!
Try this, its a bit simpler.
if len(aList) % 2:
print 'odd'
else:
print 'even'
The % operator returns the remainder of an integer division.
If the length is even the remainder is zero, or false.
Alan G.
More information about the Tutor
mailing list