1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/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'