#68 Python Tutorial for Beginners | Linear Search using Python

///#68 Python Tutorial for Beginners | Linear Search using Python

#68 Python Tutorial for Beginners | Linear Search using Python

Python Tutorial to learn Python programming with examples
Complete Python Tutorial for Beginners Playlist :
Python Tutorial in Hindi :

Github :-

Editing Monitors :

Check out our website:

Telusko :
Navin Reddy :

Subscribe to our other channel:
Navin Reddy :
Telusko Hindi :

Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20

source

By |2020-11-10T10:35:43+00:00November 10th, 2020|Python Video Tutorials|45 Comments

1. Anirban Sinha November 10, 2020 at 10:35 am - Reply

//Can't we do this sir??

arr = [12,24,36,49,51]

num = int(input("Enter the number to search in the list: n"))
for i in arr:
if i==num:
print("number found");
else:

2. karthik keyan smk November 10, 2020 at 10:35 am - Reply

my assignment:

at = 0

def linerSearch(lst,n):

for b in range(len(lst)):

if lst[b] == n:

globals()['at'] = b

return True

else:

return False

lis = [5,6,7,8,9]

num = 9

if linerSearch(lis,num):

print("found",at+1)

else:

3. Rakshit Rakshit November 10, 2020 at 10:35 am - Reply

pos=-1

def search(lis,n):

i=0

for i in range(len(lis)):

if lis[i]==n:

globals()['pos']=i

return True

i+=1

return False

lis=[0,99,12,33,45]

n=int(input('Enter number'))

if search(lis,n):

print('Found',pos)

else:

4. Mo Shahid November 10, 2020 at 10:35 am - Reply

pos=-1

def search(list, n):

i = 0

for i in range(len(list)):

if list[i] == n:

globals() ['pos']=i

return True

i = i + 1

return False

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

n = 10

if search(list, n):

print('found at',pos+1)

else:

5. ProEpicGuy November 10, 2020 at 10:35 am - Reply

is it necessary to assign -1 to pos ??

6. Poomi Arasuh November 10, 2020 at 10:35 am - Reply

l=[1,2,3,45,65,6,7,8,9,97,61]

m=int(input("value to serch "))

k=-1

def search(list,n):

j=0

for i in list:

j+=1

if i==n :

globals()["k"]=j+k

return True

return False

if search(l,m) == True:

print("found in index number " ,k )

else:

7. Newton November 10, 2020 at 10:35 am - Reply

without function with an in-buit function

arr=[3,5,7,4,9]

x=int(input("Enter the number:>"))

for i in arr:

if i == x:

print(arr.index(i)+1)

break

else:

print("Not in the list")

8. vernanonix November 10, 2020 at 10:35 am - Reply

Decided to avoid needing a global variable:

def search(list,n):

for i in list:

if i==n:

pos=list.index(i)

return True, pos

return False, False

list = [5,8,4,6,9,2]

n=9

result, pos = search(list,n)

if result:

print("Found at position",pos+1)

else:

Sir, What if i have multiple "9(search key" ) in the list
def search(List , n):
i = 0
while i < len(List):
if n == List[i]:
print(n, "Found in the positon", i)
i=i+1
return False

List = [5, 6, 8, 4, 6, 9, 6]
se = 6
search (List, se)

10. manikandan s November 10, 2020 at 10:35 am - Reply

marks = [35, 50, 60, 70]

n = 70

def search(marks, n):

for i in marks:

if i == n:

return True

return search

if search(marks, n):

print("found", marks.index(n))

else:

11. Jabir Aziz November 10, 2020 at 10:35 am - Reply

SIR plz check this …

my_list = [1, 6, 4, 2, 5, 7, 8]

num = int(input('Find a number between 0-10: '))

for item in my_list:

if item == num:

print('Element Found')

print('Element is on', my_list.index(item)+1, 'Position')

break

else:

12. Sai Tharun November 10, 2020 at 10:35 am - Reply

For user defined:

def search(list,n):

for i in list:
if i==n:
print("value found at: ", list.index(i)+1)
return True

return False

list=[3,7,4,6,9,2]
n=int(input("Enter the number you want to search: "))

if search(list,n):
pass
else:

13. Rekha Vishwakarma November 10, 2020 at 10:35 am - Reply

14. Rohit nain November 10, 2020 at 10:35 am - Reply

pos = -1

def search(nums,n):

x = len(nums)

for i in range(x):

if nums[i]==n:

globals()['pos'] = i

return True

return False

nums = [10,5,9,8,2,7,6,4,3,1]

n = 2

if search(nums,n):

print("Number",n,"nfound at",pos+1)

else:

15. Golbert .S November 10, 2020 at 10:35 am - Reply

def check (list,n):
a = len(list)
for i in range(a):
if list[i] == n:
return True
return False
list = [1,2,3,4,5]
n = 6
if check (list,n):
print('matching')
else :
print('does not match')

Is it correct sir ?

16. Ashhh November 10, 2020 at 10:35 am - Reply

list=[3,5,76,565,4453,2,2]

n=2

def search(list,n):

for i in list:

if i==n:

print(f"number find at position {list.index(i)}")

break

else:

print("not find")

search(list,n)

17. dheeraj pandey November 10, 2020 at 10:35 am - Reply

y=0

def search(list,n):

for i in range(0,len(list)):

globals()['y']=i

if list[i]==n:

return True

else:

return False

list=[23,87,90,45,67]

n=int(input("Enter the value"))

if search(list,n):

print(n ,"Number found at ",y+1)

else:

18. PRATIK ROUT November 10, 2020 at 10:35 am - Reply

list = [4, 9, 7, 3, 2, 8]

n = 9

for i in list:

if i == n:

print("Found at ", list.index(i))

break

else:

19. PRATIK ROUT November 10, 2020 at 10:35 am - Reply

def search(list, n):

global i

i = 0

while i < len(list):

if list[i] == n:

return True

i += 1

return False

list = [4, 9, 7, 3, 2, 8]

n = 7

if search(list, n):

print("Found at",i)

else:

20. Siddharth Modi November 10, 2020 at 10:35 am - Reply

……………..# FOR LOOP SOLUTION…………..

pos=1

def search(list,n):

for i in list:

if i==n:

globals()['pos']=i

return True

return False

list=[1,2,3,4,5,6,7,8,9]

n=9

if search(list,n):

print('found at', pos)

else:

21. Niranjan Bethamalla November 10, 2020 at 10:35 am - Reply

What is the need of return false, the code is working fine without writing return false

22. CrossSavior ForLove November 10, 2020 at 10:35 am - Reply

"pos" here is short for "position"?

def search(list, n):

for i in list:

if i == n:

print("Found {} on index number {}".format(n, list.index(n)))

break

else:

lst = [1, 3, 4, 5, 6, 7, 9]

search(lst, 10)

24. Chandrima Mazumdar November 10, 2020 at 10:35 am - Reply

def search(l1,n):

for i in range(len(l1)):

if n==l1[i]:

print("found at index {}".format(i))

a=list(map(int,input("Enter numbers in list").split()))

b=int(input("Enter the number you wanna search"))

search(a,b)

25. Kevern Spann November 10, 2020 at 10:35 am - Reply

def search(List, n):

for i in List:

if i == n:

return True

# ——————————————————–

arr = [5, 8, 4, 6, 9, 2]

x = int(input('Enter a number to look for in the list: {} : '.format(list(arr))))

if search(arr, x):

print(f'Found it!. The value {x} is in position: {arr.index(x)}')

else:

print('Oops, that value is not in the list.')

26. 19951A05E1 KOTHURI RAHUL VAMSI November 10, 2020 at 10:35 am - Reply

i am not able to find the pos for 1st number

27. Kowshik Varma November 10, 2020 at 10:35 am - Reply

using for loop
pos=0
def element(li,n):
for i in range (len(li)):
if li[i] == n:
globals()["pos"]=i
return True
return False

li=[1,2,3,4,5]
n=3

if element(li,n):
print("element found at",pos+1)
else:

28. Ťəj ðHøÑí November 10, 2020 at 10:35 am - Reply

def search(li, n):

for i in li:

if i == n:

return True

return False

li = [1, 2, 3, 4, 5, 6, 7, 8]

n = 10

if search(li, n):

print("Found at", li.index(n))

else:

29. ms preethi November 10, 2020 at 10:35 am - Reply

ind=-1
def search(lis,n):
count=-1
for i in lis:
count+=1
if i==n:
globals()['ind']=count
return 1
return 0

lis=[2,3,4,5,6]
n=5
if search(lis,n):
print("found",ind)
else:

30. kiran sai November 10, 2020 at 10:35 am - Reply

l=[1,2,3,4,5]

n=int(input())

for i in range(len(l)):

if l[i]==n:

print("found",i,"position")

break

else:

31. Srivathsan M November 10, 2020 at 10:35 am - Reply

for i in listval:
if listval[i]==n:
return true
return False

32. MALLELA SHIVAJI November 10, 2020 at 10:35 am - Reply

Using for loop:

pos=-1
def search(list, n):
for i in range(len(list)):
if list[i]==n:
globals ()['pos'] = i
return True
i=i+1
return False

list=[? , ? , ? , ? , ?]
n=?

if search(list, n):
print("found at ", pos)
else:

🙂

33. Raman Das November 10, 2020 at 10:35 am - Reply

a=int(input('enter the number you want to search :'))
index=1

def lin_learch(list,n):
l=1
for i in list:
if i==n:
return True

break
else:
l+=1
globals()['index']=l

else:
return False

list=[12,10,9,33,11,16]

if lin_learch(list,a):
print('Found')
print('found at position',index)
else:

Output:-
enter the number you want to search :11
Found
found at position 5

34. Neeraj Nainwal November 10, 2020 at 10:35 am - Reply

List=[••••••••]
For n in range():
If list[n]== query?
Print('found',[n])

35. vikramraj nampalli November 10, 2020 at 10:35 am - Reply

Thank you Navin …you made my day ….you are a champion mate and appreciate the work that is helping thousands ….

36. vikramraj nampalli November 10, 2020 at 10:35 am - Reply

pos = -1

class Search:

def __init__(self,lst,n):

self.lst = lst

self.n=n

print("Initialized the values")

print("search value n is : {} and list is {}".format(n,lst))

def search(self):

for i in range(len(self.lst)):

if(self.n==self.lst[i]):

globals()["pos"] = i+1

return True

return False

lst = [1,3,56,78,99,45,86,43]

n=int(input("Enter the number you want to find :"))

srchobj = Search(lst,n)

if(srchobj.search()):

print("Search found ")

print("Position is {}".format(pos))

else:

37. venkatesh kathi November 10, 2020 at 10:35 am - Reply

list=[7,4,9]

n=10

def search():

for i in list:

if i==n:

print("We found")

break

else:

search()

Can we do liner search like this , Please let me know

38. Achintya Sharma November 10, 2020 at 10:35 am - Reply

why did he take position as -1?

39. Bella November 10, 2020 at 10:35 am - Reply

n=int(input("enter the length of list: "))
list=[]
for i in range(n):
r=int(input(f"enter {i+1} th element: "))
list.append(r)
print(list)
u=int(input("enter a number you need to search for: "))
for i,j in enumerate(list):
if list[i]==u:
print(f"the element {u} found at {i} th location")
break
else:

40. Battery Low November 10, 2020 at 10:35 am - Reply

Sir help me out …
I got struck…
It's giving error telling that 'list' object is not callable

41. Manikanta Perumalla November 10, 2020 at 10:35 am - Reply

list=[4,3,12,34,56,77,89]

n=int(input('enter a number'))

for i in range(len(list)):

if list[i]==n:

print('found at pos',i+1)

break

else:

42. Gujarathi November 10, 2020 at 10:35 am - Reply

def find(lst,value):

n = 0

for i in lst:

n = n + 1

if i == num:

print('Value found',n)

break

else:

print('we didnt find any value')

43. Atikul Islam Mithu November 10, 2020 at 10:35 am - Reply

# Solved by : Atikul Islam Mithu

pos = -1

def search(list,n):

for i in range(len(list)):

if list[i] == n:

globals()['pos'] = i

return True

return False

list = [1,2,5,9,7,4,3]

n = int(input("Enter an element for search : "))

if search(list,n):

print(n,"Found at",pos)

else:

44. Asoke Biswas November 10, 2020 at 10:35 am - Reply

POS=-1
def search (List, n):
for i in List:
if i==n:
global ['POS'] = i
return True

return False
List=[2,3,5,8,4,9,1]
n=8
If search(List,n):
print ("Found at", POS+1)
else:

45. Astha Gupta November 10, 2020 at 10:35 am - Reply

pos=-1

def Linear_Search(lst,ele):

i=0

for i in range(len(lst)-1):

if lst[i]==ele:

globals()['pos']=i+1

return True

return False

lst=[]

size=int(input("Enter the size of list:"))

for i in range(size):

x=int(input("Enter values:"))

lst.append(x)

ele=int(input("Enter the element to be search:"))

if Linear_Search(lst,ele):

print("Found at",pos)

else: