Linear Search using python
<p>Hi I am new to python and I am trying to learn it by writing linear search in python here is the code and this is the code I have written so far. </p>

<pre><code>a = [100]

n = int(raw_input("Enter the no. of elements: "))

print "Enter the elements: "
for i in range(0,n):
    print i

item = int(raw_input("Enter the item to be searched: "))

for i in range(0,10):

    if a[i] == item:
        print "Item ", item, "found at location", i+1
        break;

if i == n:
    print "Item", item, "not found"
</code></pre>

<p>I get a error saying list index out of range, where am I going wrong? </p>

<p>You have one element in your list. The second look tries to access elements 1 though 9, which are out of bounds. </p>

<p>I'd recommend using <code>enumerate</code>, which returns pairs of (index, element) up to the length of the iterable </p>

<pre><code>for i, x in enumerate(a):
    if x == item:
        # found item at position i
</code></pre>

<p>If you want to input number to generate a list of elements, then </p>

<pre><code>n = int(raw_input("Enter the no. of elements: "))
# build your list of n elements here 
for i in range(0,n):
    print i
</code></pre>

<p>For example, <code>a = range(0, n)</code> or import the <code>random</code> module if you don't want a linear range of numbers </p>

python