#!/usr/bin/env python
# My list in which I'd like to search something.
L = [1, 2, 4, 8, 16, 32, 64]
# My value that I'm looking for: 2**X
X = 5
# The "Do-it-yourself"-method
# Using a while or for loop and checking every element yourself.
i = -1
while i+1 < len(L):
i += 1
if 2 ** X == L[i]:
print 'at index ', i
break
else:
print 2 ** X, ' not found'
for i in range(len(L)):
if 2 ** X == L[i]:
print 'at index ', i
break
else:
print 2 ** X, ' not found'
# The "Let-Python-do-the-work"-method
# Have you noticed? index() raises an exception, if its argument is not
# in the list!
if 2 ** X in L:
print 'at index ', L.index(2 ** X)
else:
print 2 ** X, ' not found'