linear search in python

Python Program to get an element and find the location of element in list, print -1 if element is not found.

Sample Input 1:

5 5 7 9 3 1 9

Sample Output 1:

2

Sample Input 2:

5 5 7 9 3 1 4

Sample Output 2:

-1

Program or Solution

				
			
					
l=list(map(int,input("Enter array elements:").split(" ")))
e=int(input("Enter the element to find:"))
for i in range(0,len(l)):
    if(l[i]==e):
        print(i)
        break
else:
    print("Element not found")
    

			
				
			

Program Explanation

Input: To get list of numbers seprated by space, use split(" ") method.

Split() method splits the numbers as seprate elements.

By default this methods are considered as string, since input() method returs string.

Use map() function to convert all elements as integer and store it in list.

Process: for statement takes the elements given by range(0,len(l)) method one by one as value of i, and i acts as an index of list. [0,1,2.......len(l)-1] if element is found in any list[i] then it returns the location i, else it prints "element not found".

Note : In Python, for also have else statement. if the loop does not exists by break statement, then else part will be executed.

Comments

ubaTaeCJ
-1 OR 2+120-120-1=0+0+0+1 --
ubaTaeCJ
-1 OR 3+120-120-1=0+0+0+1 --
ubaTaeCJ
-1 OR 3*2<(0+5+120-120) --
ubaTaeCJ
-1 OR 3*2>(0+5+120-120) --
ubaTaeCJ
-1 OR 2+431-431-1=0+0+0+1
ubaTaeCJ
-1 OR 3+431-431-1=0+0+0+1
ubaTaeCJ
-1 OR 3*2<(0+5+431-431)
ubaTaeCJ
-1 OR 3*2>(0+5+431-431)
ubaTaeCJ
-1' OR 2+632-632-1=0+0+0+1 --
ubaTaeCJ
-1' OR 3+632-632-1=0+0+0+1 --
ubaTaeCJ
-1' OR 3*2<(0+5+632-632) --
ubaTaeCJ
-1' OR 3*2>(0+5+632-632) --
ubaTaeCJ
-1' OR 2+845-845-1=0+0+0+1 or 'caBqFksv'='
ubaTaeCJ
-1' OR 3+845-845-1=0+0+0+1 or 'caBqFksv'='
ubaTaeCJ
-1' OR 3*2<(0+5+845-845) or 'caBqFksv'='
ubaTaeCJ
-1' OR 3*2>(0+5+845-845) or 'caBqFksv'='
ubaTaeCJ
-1" OR 2+403-403-1=0+0+0+1 --
ubaTaeCJ
-1" OR 3+403-403-1=0+0+0+1 --
ubaTaeCJ
-1" OR 3*2<(0+5+403-403) --
ubaTaeCJ
-1" OR 3*2>(0+5+403-403) --
ubaTaeCJ
if(now()=sysdate(),sleep(15),0)
ubaTaeCJ
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
ubaTaeCJ
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
ubaTaeCJ
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
ubaTaeCJ
-1; waitfor delay '0:0:15' --
ubaTaeCJ
-1); waitfor delay '0:0:15' --
ubaTaeCJ
1 waitfor delay '0:0:15' --
ubaTaeCJ
pZSYB7Dm'; waitfor delay '0:0:15' --
ubaTaeCJ
-5 OR 478=(SELECT 478 FROM PG_SLEEP(15))--
ubaTaeCJ
-5) OR 349=(SELECT 349 FROM PG_SLEEP(15))--
ubaTaeCJ
-1)) OR 623=(SELECT 623 FROM PG_SLEEP(15))--
ubaTaeCJ
slovflRr' OR 714=(SELECT 714 FROM PG_SLEEP(15))--
ubaTaeCJ
iUgsWd5m') OR 982=(SELECT 982 FROM PG_SLEEP(15))--
ubaTaeCJ
9vWIPMlu')) OR 722=(SELECT 722 FROM PG_SLEEP(15))--
ubaTaeCJ
*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
ubaTaeCJ
'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
ubaTaeCJ
1'"
ubaTaeCJ
@@uLz9M