Source code for first_negative

"""This module contains a function to illustrate the use of 
while loops. 

"""

    
[docs]def firstnegative(L): """ Given a list of values, finds and returns the index of the first negative value in the list. If the list does not contain any negative values, it returns -1. Note that the use of negative values when the value is "not found" is not a good idea. A better value would be to return None. Good test cases are listed below:: >>> print 'first negative in [1,2,-1] is at index', firstnegative([1,2,-1]) first negative in [1,2,-1] is at index 2 >>> print 'first negative in [-1,2,-1] is at index', firstnegative([-1,2,-1]) first negative in [-1,2,-1] is at index 0 >>> print 'first negative in [1,2,1] is at index', firstnegative([1,2,1]) first negative in [1,2,1] is at index -1 """ i = 0 while (i < len(L)): val = L[i] if val < 0: return i i += 1 return -1