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: print('not found')
@@jevardhanpolemoni7730 pos=-1 num=int(input("enter a num: ")) list=[5,8,4,6,9,2] i=0 for i in range(len(list)): globals()['pos']=i if list[i]==num: print("found",pos) break else: print("not found")
A simpler way to do the search function: def search(my_list, n): for i, num in enumerate(my_list): if num == n: globals()['position'] = i + 1 return True
list = [21,4,95,65,74,56] n=65 def search(list,n): for i in list: if i==n: print("Value Found at " , list.index(i)+1) return True if search(list,n): print("Thank you") else: print("Value Not Found") Output : Value Found at 4 Thank you
tq sir,i like the way u teach lst=[2,4,5,6,7,8] n=int(input('enter which nmber u want to find index:')) for i in range(len(lst)): if(lst[i]==n): print('{} found at index {}'.format(n,i)) break else: print('{} not found in the given list'.format(n))
val = [2,1,4,7,3,8,9] valueTosearch = 6 for i in range(len(val)): if val[i] == valueTosearch: print("Found the value at position", i+1) break else: print("Not Found in the list")
Good morning sir....sir this is first time I'm commenting on a TH-cam channel I have seen you teach better than our college teacher that's reason I'm kindly requesting to make video for students of bsc it regarding their projects for third year please sir show us a path please
def search(list,n): for i in range(len(list)): if (list[i]==n): return True else: return False values={1,2,3,4,5} if search("values",1): print("found") else: print("not found") this program is not working .... every time it says "not found" even if the element is present
if (list[i]==n): return True else: return False bro u cant use else here ....because everytime it is checking for list[i]==n and if it is not true it will move to else block and will return false that is not found ....even if it is found in the list
from array import * f1 = array('i',[]) x=int(input('enter the index')) for i in range(x): p=int(input('enter the elements')) f1.append(p) print(f1) r=int(input('enter the number')) for i in range(len(f1)): if(r == f1[i]): print('the number is found at ',i) else: print('not found')
def Linearsearch(lst, n): s = 0 for i in lst: s = s+1 if i == n: print("found at", s-1) s= s+1 break else: print('not found') lst = [2,9,8,7,6] n = 7 Linearsearch(lst,n)
pos=-1 def lsearch(list,n): for i in list: if i< len(list): if list[i]==n: globals()["pos"]=i return True list=[5,8,4,6,9,2] n=2 if lsearch(list,n): print("Found the number and the position is", pos) else: print("not found")
list=[213,23423,5342,1231,1233] n=int(input("enter the number you want to find : ")) for i in range(len(list)): if list[i]==n: print("FOUND") break else: print("not")
Thanks a lot sir! pos = -1 list = [10,20,30,40,50,60] n = int(input('enter a number')) for i in range(len(list)): if list[i] == n: globals()['pos'] = i print('found at no',pos) break else: print('not found')
I want to express my heartfelt gratitude for all the effort you put into simplifying the topics. Your presence and the way you explain things in the videos bring us joy and radiate positive energy. I hope you continue in this way, and I wish you all the best in everything you do.
def search(list, guess): for i in list: if i == guess: globals()['p'] = i return True return False list = [5,4,8,7,3,2] guess = int(input("Guess The Number(for loop method)")) if search(list, guess): print('Congratulation','Position is ->',list.index(p)+1) else: print('Not correct')
def linear_search(array,n,x ) : For i in range(0,n) : If(array[i]==x : return i Return -1 array=[1,2,89,9,5,7] N=len(array) X=5 Result=linear_search(array, n,x) If result ==-1 Print("Element to be not found") Else: Print(Element is found at)
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: print("Not Found")
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: print("not found")
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 ?
This really helpful thank you. list1 = [12,23,45,23,67] Element_to_search = 23 i = 0 for x in list1: i += 1 if x == Element_to_search: print(x) print("it is on") print(i)
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)
pos = -1 file = [1,2,3,4,5,6,7,8,9] n = 8 def search(x,y): for i in range(0,len(x)): if x[i]== n: globals() ["pos"] = i+1 return True return False if search(file,n): print("Yes it is at",pos) else: print("No")
pos=-1 def search(lis,n): for i in range(len(lis)): if lis[i]==n: globals()['pos']=i+1 return True return False lis=[5,9,10,8,33,46,1,35,48,12,66] n=int(input('Enter the number which you want to search: ')) print('List contains total of {} elements'.format(len(lis))) if search(lis,n): print('{} found at position {}'.format(n,pos)) else: print('{} is not found anywhere in the list'.format(n))
def funsearch(a,x): c=0 for i in range(0,len(a)): if a[i]==x: print("Found Loaction=",i) c=c+1 print("Frequency of element =",c) A1=eval(input('Enter a list')) B1=eval(input('Element to be searched')) funsearch(A1,B1)
One way of searching using for loop def Mysearch(list,n): for i in range(len(list)): if list[i] == n: print('Found ' + str(n)+' at position ' + str(i+1)) break else: print('Not Found') list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Mysearch(list,5) Output : Found 5 at position 5
#linearSearch list=[1,2,3,4,5] key=5 pos=0 def search(list,key): global pos for i in list: if i==key: return True pos+=1 else: return False if search(list,key): print("value found at list[%d]"%pos) else: print("value not found")
from numpy import * def linearSearch(arr, key): for i in range(len(arr)): if key == arr[i]: return i else: return "Not found" arr = array([2,3,5,1,2,6]) a = linearSearch(arr,7) print(a)
thanks sir....................... pos=0 def search(list, n): i=0 for i in range(len(list)): if list[i]==n: globals()['pos']=i return True; return False; list=[5,17,8,15,12,9,1] n=int(input("Enetr a number to search:")) if search(list,n): print("Found",pos+1) else: print("Not match")
short and easy with for loop def search(lst,n): for i in lst: if i == n: globals()['pos'] = lst.index(i) return True return False lst = [1,5,9,4,8,7,6,2,3] n = 7 if search(lst,n): print("found at position", pos+1) else: print("not found")
simple way without using func nums=[] x=int(input("how many numbers u want enter")) for k in range(x): nums.append(input()) print("ur list is",nums) x=int(input('enter element u want to search')) for i in nums: if str(i)==str(x): print(x,"is in list at position",nums.index(i)+1) break else: print("not in list")
I found ans,Sir You can see def search(list,n): for i in range(len(list)): if list[i] == n: globals()['pos'] = i return True return False list = [5,8,4,6,9,2] n = 9 if search(list,n): print('Found it',pos+1) else: print('Not Found')
def linear_search(list, target): for i in range(len(list)): if list[i] == target: return i return -1 list = [1,2,3,4,5,6,7,8,9] target = 7 position = linear_search(list, target) if position != -1: print("found at", position) else: print("not found")
def linear_search(list,n): for i in range(len(list)): if list[i]==n: return ('found') return ('not found') li1= [5,7,3,1,4,8,6,2] num1=8 linear_search(li1,num1)
def linearsearch(arr, x): for i in range(len(arr)): if arr[i] == x: return i return -1 arr = [1,2,3,4,5,6,7,8] x = 4 print("element found at index "+str(linearsearch(arr,x)))
We can also do it like this def search(l1, n): if n in l1: return True l1 = [10, 32, 22,652,45] print(l1) n = 32 if search(l1, n): print("Found") else: print("Not found")
Thank you Telusko. def search(iterable, n): try: index = iterable.index(n) if n in iterable: print(f"{n} was found at position {index+1}.") except ValueError: print(f"Sorry, {n} is not in the list.") nums = [13, 45, 12, 19, 90, 0, 10] user = int(input("What would you like to search: ")) search(nums, user)
i = -1 def search (list,n): for i in range(len(list)): if list[i] == n: globals()['i']=i return True list = [5,8,4,6,9,2] n = 9 if search(list,n): print("Found at",i+1) else: print("Not Found")
My sol for loop: pos=-1 def search(list,n): for i in range(len(list)): if list[i]==n: globals()["pos"]=i return True return False list=[5,8,4,6,9,2] n = 9 if search(list,n): print("Found at", pos+1) else: print("Not found")
from array import * list = array('i', [5, 8, 4, 6, 9, 2]) # array n = 10 pos=0 for i in range(len(list)): if n==list[i]: pos=i print("the element",n,"present at the index",pos) break else: print("the element", n, " is not present in the array")
def search(x,y): for n in x: global a a=0 if y==n: return True return False x=1,2,3,4 y=int(input('enter the element to search')) if search(x,y): print('element found',a+1) else: print('not found')
pos = -1 def search_num(nums, num): i = 0 for i in range(len(nums)): if nums[i] == num: globals()['pos'] = i print("found at: ", pos +1) else: print("no number") nums= [3,7,4,8,0,6] num = 3 search_num(nums, num)
loc=0 def search(list,n): for i in range(len(list)): if list[i]==n: globals()['loc']=i return True return False list=[5,8,4,6,9,2] n=9 if search(list,n): print('found at',loc+1) else: print('not found')
x = 5 list = [1,2,3,4,5] for i in range(len(list)): if(list[i] == x): print("Item found at index " + str(i)) elif(i >= len(list)-1): print("Item not found" + str(i)) else: continue
def search(list, n): for i in range(len(list)): if i == n: i-=1 print(list[i]) return list[i] list = [1,2,3,4,5,6,7,8] n = 3 if search(list,n): print("found") else: print("not found invaild syntax")
Sir,I have wrote a code by taking input from user for linear search using for loop p=0 n=int(input('enter length of list ')) a=[] for i in range(n): a.append(int(input('enter no '))) print(a) b=int(input('enter no u want to search ')) def search(a,b): for i in range(len(a)): if a[i]==b: globals()['p']=i return True return False if search(a,b): print('found at ',p+1) else: print('not found')
Assignment : def search(lst, n): for i in range(len(lst)): if lst[i] == n: print(i) return True return False l = [5, 8, 4, 6, 9, 2] value = 9 # the value we want to search for if search(l, value): print('Found') else: print('Not Found')
using for loop pos=-1 def search(list,n): for i in range(len(list)): if list[i]==n: globals() ['pos'] = i return True else: return False list=[5,8,4,6,9,2] n=2 if search(list,n): print("found at ",pos+1) else: print("not found ")
# Program : Linear search with "For Loop" pos = 0 def search(list, n): for i in list: if i == n: globals()['pos'] = list.index(n) return True return False list = [5,8,4,6,9,2] n = 9 if search(list, n): print("Found at position", pos+1) else: print("Not Found")
Maybe a little bit simpler: a=[1,5,9,7,8,3,13] n=8 for each_num in a: if each_num==8: print (str(each_num)+" is present at:"+str(a.index(each_num))) else: print ("specified number is not present")
pos = -1 def search(list, n): i = 0 for i in range(len(list)): if list[i] == n: globals()['pos'] =i return True list = [123, 45, 678, 456, 7876, 22345, 21] n = 21 if search(list, n): print("Found at Position",pos+1) else: print("Notfound")
def ls(l, n): for i in range(len(l)): if l[i] == n: print(f"found the value {n} at {i} ") break else: print("not found") l = [2,6,3,7,55,78,42,99] n = input("enter the value: ") ls(l, n)
pos = 0 def use_for_lop(lsit,value): for i in range(len(list)): if list [i] == value: globals()['pos']=i return True list = [1, 2, 3, 4, 5,6 ] value = 4 if use_for_lop(list,value): print('found at ', pos +1) else: print('not found')
# LS # just made 2 changes accordingly pos = -1 def search(list,n): for i in range(len(list)): globals() ['pos'] = i if list[i] == n: return True return False list = [1,3,6,9,10,4] n = 3 if search(list, n ): print("found",pos) else: print("not found")
position = -1 def linear(list, x): for i in range(len(list)): globals()['position'] = i if list[i] == x: return i return 0 list = [2, 3, 78, 21, 67, 73, 43, 90] x = 78 if linear(list, x): print("item is found at:", +position) else: print("item is not found..!") Output :- item is found at: 2
list = [7,9,9,1,7,4,8,5] num = int(input("search for the value")) print("The indexes of the value are:", end="") for i in range(0, len(list)): if list[i] == num: print(i, ",", end="") else: print("not found") Short and sweet, I hope
pos=-1 def search(list,n): for x in range (len(list)): if list[x]==n: globals()['pos']=x return True return False list=[3,2,5,6,9,8] n=9 if search(list,n): print("Found",pos+1) else: print("Not Found")
#Linear Search for a value in the list using for loop and function. indexposition = -1 def search(list, n): for i in range(len(list)): if list[i] == n: globals()['indexposition'] = i return True else: return False list = [23,12,73,33,19] n = 19 if search(list, n): print("Value is",n,"and is found at position no.", indexposition + 1 ) else: print("Value is",n,"and Not found in the list, try again")
thank you for another wondeful lesson Navin!! just one question: i tryied this code without the 'pos = -1' in the beginning and it works all the same.. so why do we actually need it for? thanks 🙂
pos = -1 def search (list,n): for i in range (len(list)): if list[i]==n: globals()['pos'] = i return True else: return False try: n = int(input("Enter the integer number to find in list ")) list = [2,4,1,5,7,8] if search(list,n): print ("found at position", pos) else: print ("not found") except: print ("Enter the valid integer")
Def search (list,n): For i in range(len(list)): If list[i] == n: Print(" index" ,list.index(n)) return true i += 1 List = [1,2,3,4,5,6] N =5 If search (list ,n): Print("found") Else: Print("Not Found ")
def linear_search(data, target): for i in range (len(data)): if data[i] == target: return True return False data = [12,23,34,45,56,67,78,89,90] target = 56 if linear_search (data, target): print("Found") else: print("Not Found")
def LinearSearch(list,num): for i,val in enumerate(list): if (val == num): return True return False list = [5,8,4,6,9,2] num = 9 if(LinearSearch(list,num)): print("Found") else: print("Not found")
list=[3,4,6,78,5] n=3 po=None def found(list,n): global po for i in range(len(list)): if list[i] == n: po=i return True else: return False if found(list,n): print('found at '+str(po)) else: print('not found')
pos = -1 def search(l,n): for i in range(len(l)): if l[i]== n: globals()["pos"] = i return True return False l = [8,5,6,1,2,3] n = 5 if search(l,n): print("found at ", pos+1) else: print("not found")
n=int(input()) arr=[] flag=0 for i in range(0,n): ele=int(input()) arr.append(ele) k=int(input()) for i in range(0,n): if(k==arr[i]): flag=1 if(flag==1): print("element found at",i+1) else: print("Element not found")
list=[2,8,7,9,6,4,3,5,1] n=int(input("please give the value between 1-9 : ")) def search(list,n): for i in list: if list[i]!=n: pass elif list[i]==n: print("found at ",i+1) return True search(list,n)
def search(list, n): for i in range(0, len(list)): if list[i] == n: return i return -1 list = [1,3,5,7,9,0,6] n = 7 result = search(list, n) if result != -1: print("Number found at ",result) else: print("Number not found")
list=(3,6,7,5,45,7,9,26,4) def search(list): value=int(input("Enter the value you want to search from list")) index=-1 for x in list: index += 1 if x==value: print("value exist in list"," , INDEX VALUE =",index) break else: print("not found") search(list)
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: print("Not found")
def search(lit,n): for i in range(len(lit)): globals()['pos']=i if lit[i] == n: return True return False list=[1,3,4,5] n=4 if search(list,n): print("i found that ",pos) else: print("i cant find that") n=4
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:
print('not found')
How would you know the position then?
tq
@@jevardhanpolemoni7730 for i in range(0,len(list))
If list[i]==key:
Return i
Return -1
@@jevardhanpolemoni7730 pos=-1
num=int(input("enter a num: "))
list=[5,8,4,6,9,2]
i=0
for i in range(len(list)):
globals()['pos']=i
if list[i]==num:
print("found",pos)
break
else:
print("not found")
def search(list,n):
for i in range(len(list)):
if list[i] == n:
return True
return False
Good work please keep uploading we are learning from you both javascript and python
A simpler way to do the search function:
def search(my_list, n):
for i, num in enumerate(my_list):
if num == n:
globals()['position'] = i + 1
return True
list = [21,4,95,65,74,56]
n=65
def search(list,n):
for i in list:
if i==n:
print("Value Found at " , list.index(i)+1)
return True
if search(list,n):
print("Thank you")
else:
print("Value Not Found")
Output : Value Found at 4
Thank you
Kuch b out put ni ata
this code has a complexity of N^2, although your code is correct but it's not so efficient as compared to the code with a complexity of N.
@@usaid4khan how n^2 ? we are using one for loop so 'n' and then another search list.index(i) now another 'n' that's why its n^2 right bro?
This code is so efficient..can you tell me what is the name of the way we printed the position.
l=[1,2,3,4,5]
x=int(input("enter number to search in",l))
i=flag=0
while i
tq sir,i like the way u teach
lst=[2,4,5,6,7,8]
n=int(input('enter which nmber u want to find index:'))
for i in range(len(lst)):
if(lst[i]==n):
print('{} found at index {}'.format(n,i))
break
else:
print('{} not found in the given list'.format(n))
val = [2,1,4,7,3,8,9]
valueTosearch = 6
for i in range(len(val)):
if val[i] == valueTosearch:
print("Found the value at position", i+1)
break
else:
print("Not Found in the list")
Good solution but you are not defining any function. You have to use function (for practice purposes) at this level of learning.
Actually, len function will give us the no of elements in the list , instead u should write "for i in val: "
make videos on python DATA STRUCTURE PLEASE SIR(SEARCHING,SORTING,ALGORITHMS)
yeah pls sir, plsss, my exam is on 25th so plss, sir
Kya sir ne data structures pe video banayi kya can anyone pls tell me
Yes sir pls make videos on DAta structure with python
Telusko means to know in Telugu
Adhi telusko kaadhu bangaram thelusuko
Good morning sir....sir this is first time I'm commenting on a TH-cam channel I have seen you teach better than our college teacher that's reason I'm kindly requesting to make video for students of bsc it regarding their projects for third year please sir show us a path please
#simplestCode
list,key=[1,2,3,4,5],3
if key in list:
print("found at index", list.index(key))
else:
print("not found")
Yeah ! Absolutely correct.
i also expect this method but why sir make it lenthier i cant undestand
This performs 2 searches.
For key in list
And
List.index(key)
def search(list,n):
for i in range(len(list)):
if (list[i]==n):
return True
else:
return False
values={1,2,3,4,5}
if search("values",1):
print("found")
else:
print("not found")
this program is not working .... every time it says "not found" even if the element is present
@@feynman8692 for i in range(len(list)):
if (list[i]==n):
return True
else:
return False
bro u cant use else here ....because everytime it is checking for list[i]==n and if it is not true it will move to else block and will return false that is not found ....even if it is found in the list
Sir it's love.. ur teaching is love in this quartine.. best use of time
from array import *
f1 = array('i',[])
x=int(input('enter the index'))
for i in range(x):
p=int(input('enter the elements'))
f1.append(p)
print(f1)
r=int(input('enter the number'))
for i in range(len(f1)):
if(r == f1[i]):
print('the number is found at ',i)
else:
print('not found')
def Linearsearch(lst, n):
s = 0
for i in lst:
s = s+1
if i == n:
print("found at", s-1)
s= s+1
break
else: print('not found')
lst = [2,9,8,7,6]
n = 7
Linearsearch(lst,n)
pos=-1
def lsearch(list,n):
for i in list:
if i< len(list):
if list[i]==n:
globals()["pos"]=i
return True
list=[5,8,4,6,9,2]
n=2
if lsearch(list,n):
print("Found the number and the position is", pos)
else:
print("not found")
list=[213,23423,5342,1231,1233]
n=int(input("enter the number you want to find : "))
for i in range(len(list)):
if list[i]==n:
print("FOUND")
break
else:
print("not")
Thanks a lot sir!
pos = -1
list = [10,20,30,40,50,60]
n = int(input('enter a number'))
for i in range(len(list)):
if list[i] == n:
globals()['pos'] = i
print('found at no',pos)
break
else:
print('not found')
Add i=I+1 next to return true it's not incrementing
assignment done sir !!!!
Answer : if i in range(6):
It's better to use stop as n in range like if i in range(n)
I want to express my heartfelt gratitude for all the effort you put into simplifying the topics. Your presence and the way you explain things in the videos bring us joy and radiate positive energy. I hope you continue in this way, and I wish you all the best in everything you do.
def search(list, guess):
for i in list:
if i == guess:
globals()['p'] = i
return True
return False
list = [5,4,8,7,3,2]
guess = int(input("Guess The Number(for loop method)"))
if search(list, guess):
print('Congratulation','Position is ->',list.index(p)+1)
else:
print('Not correct')
Sir please don't quit python.............javascript is very good but we need your videos on python
Telusko="best thing ever taught of"
thank you sir i appreciate you hard work
please do make a series on DSA with Python. Because sir, the way you explain things gives a complete different perspective and approach.
this is the starting point of DSA in python
def linear_search(array,n,x ) :
For i in range(0,n) :
If(array[i]==x :
return i
Return -1
array=[1,2,89,9,5,7]
N=len(array)
X=5
Result=linear_search(array, n,x)
If result ==-1
Print("Element to be not found")
Else:
Print(Element is found at)
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:
print("Not Found")
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:
print("not found")
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 ?
This really helpful thank you.
list1 = [12,23,45,23,67]
Element_to_search = 23
i = 0
for x in list1:
i += 1
if x == Element_to_search:
print(x)
print("it is on")
print(i)
this is the code that i wrote .
For loop me increment nahi hota hai bhai
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)
For i in range(len(list)):
If list[i]==n:
globals() ['pos']=i
return True
return false
increment isn't done
Awesome ...I doesn't hear like this from any people...such nice vedio about linear search 😍😍😍
pos = -1
file = [1,2,3,4,5,6,7,8,9]
n = 8
def search(x,y):
for i in range(0,len(x)):
if x[i]== n:
globals() ["pos"] = i+1
return True
return False
if search(file,n):
print("Yes it is at",pos)
else:
print("No")
Your voice, as well as style boost, encourage me to learn more
Asnw of assignment:-
pos=0
def search(x,n):
for i in range(len(x)):
globals()['pos']=i+1
if x[i]==n:
return 'true'
break
else:
return 'false'
x=[1,2,3,4,5,6]
n=6
y=search(x,n)
if y=='true':
print('found',pos)
else:
print('not found')
pos=-1
def search(lis,n):
for i in range(len(lis)):
if lis[i]==n:
globals()['pos']=i+1
return True
return False
lis=[5,9,10,8,33,46,1,35,48,12,66]
n=int(input('Enter the number which you want to search: '))
print('List contains total of {} elements'.format(len(lis)))
if search(lis,n):
print('{} found at position {}'.format(n,pos))
else:
print('{} is not found anywhere in the list'.format(n))
def funsearch(a,x):
c=0
for i in range(0,len(a)):
if a[i]==x:
print("Found Loaction=",i)
c=c+1
print("Frequency of element =",c)
A1=eval(input('Enter a list'))
B1=eval(input('Element to be searched'))
funsearch(A1,B1)
One way of searching using for loop
def Mysearch(list,n):
for i in range(len(list)):
if list[i] == n:
print('Found ' + str(n)+' at position ' + str(i+1))
break
else:
print('Not Found')
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Mysearch(list,5)
Output : Found 5 at position 5
As usual another great video sir 🙏🙏..
Good Evening, Sir!
I found this amazing video which cleared all my doubts.
You just didn't sound like a Telugu guy.
Thank you so much, Sir:)
#linearSearch
list=[1,2,3,4,5]
key=5
pos=0
def search(list,key):
global pos
for i in list:
if i==key:
return True
pos+=1
else:
return False
if search(list,key):
print("value found at list[%d]"%pos)
else:
print("value not found")
Thanks from TamilNadu!
from numpy import *
def linearSearch(arr, key):
for i in range(len(arr)):
if key == arr[i]:
return i
else:
return "Not found"
arr = array([2,3,5,1,2,6])
a = linearSearch(arr,7)
print(a)
😉😭😭
thanks sir.......................
pos=0
def search(list, n):
i=0
for i in range(len(list)):
if list[i]==n:
globals()['pos']=i
return True;
return False;
list=[5,17,8,15,12,9,1]
n=int(input("Enetr a number to search:"))
if search(list,n):
print("Found",pos+1)
else:
print("Not match")
short and easy with for loop
def search(lst,n):
for i in lst:
if i == n:
globals()['pos'] = lst.index(i)
return True
return False
lst = [1,5,9,4,8,7,6,2,3]
n = 7
if search(lst,n):
print("found at position", pos+1)
else:
print("not found")
simple way
without using func
nums=[]
x=int(input("how many numbers u want enter"))
for k in range(x):
nums.append(input())
print("ur list is",nums)
x=int(input('enter element u want to search'))
for i in nums:
if str(i)==str(x):
print(x,"is in list at position",nums.index(i)+1)
break
else:
print("not in list")
def lin(a,n):
for i in range (len(a)):
if a[i]==n:
pos=i
print('elemt found at index',pos)
lin([1,2,3,4,5,6],5)
I found ans,Sir
You can see
def search(list,n):
for i in range(len(list)):
if list[i] == n:
globals()['pos'] = i
return True
return False
list = [5,8,4,6,9,2]
n = 9
if search(list,n):
print('Found it',pos+1)
else:
print('Not Found')
def search(lst,n):
for i in range(len(lst)):
if lst[i]==n:
print("found at",i+1)
break
else:
print("not f")
lst=[1,2,3]
search(lst,2)
An another way: without using loop
a=[1,4,6,3,8,9]
b=6
try:
print( a.index(b))
except Exception as e:
print('no.not found')
finally:
print('thanks
def linear_search(list, target):
for i in range(len(list)):
if list[i] == target:
return i
return -1
list = [1,2,3,4,5,6,7,8,9]
target = 7
position = linear_search(list, target)
if position != -1:
print("found at", position)
else:
print("not found")
def search(list,n):
for i in range(len(list)):
if list[i]==n:
print('found',i)
break
else:
print('notfound')
list = [2,3,4,1,21]
n = 3
search(list,n)
def linear_search(list,n):
for i in range(len(list)):
if list[i]==n:
return ('found')
return ('not found')
li1= [5,7,3,1,4,8,6,2]
num1=8
linear_search(li1,num1)
def linearsearch(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = [1,2,3,4,5,6,7,8]
x = 4
print("element found at index "+str(linearsearch(arr,x)))
i am not understanding this -1 why we use it can you please explain thanks and also the last print statement getting error
Thank you Navin ...you made my day ....you are a champion mate and appreciate the work that is helping thousands ....
We can also do it like this
def search(l1, n):
if n in l1:
return True
l1 = [10, 32, 22,652,45]
print(l1)
n = 32
if search(l1, n):
print("Found")
else:
print("Not found")
Thank you Telusko.
def search(iterable, n):
try:
index = iterable.index(n)
if n in iterable:
print(f"{n} was found at position {index+1}.")
except ValueError:
print(f"Sorry, {n} is not in the list.")
nums = [13, 45, 12, 19, 90, 0, 10]
user = int(input("What would you like to search: "))
search(nums, user)
i = -1
def search (list,n):
for i in range(len(list)):
if list[i] == n:
globals()['i']=i
return True
list = [5,8,4,6,9,2]
n = 9
if search(list,n):
print("Found at",i+1)
else:
print("Not Found")
use p for position instead of i because i is already use by for loop as variable
My sol for loop:
pos=-1
def search(list,n):
for i in range(len(list)):
if list[i]==n:
globals()["pos"]=i
return True
return False
list=[5,8,4,6,9,2]
n = 9
if search(list,n):
print("Found at", pos+1)
else:
print("Not found")
from array import *
list = array('i', [5, 8, 4, 6, 9, 2]) # array
n = 10
pos=0
for i in range(len(list)):
if n==list[i]:
pos=i
print("the element",n,"present at the index",pos)
break
else:
print("the element", n, " is not present in the array")
def search(x,y):
for n in x:
global a
a=0
if y==n:
return True
return False
x=1,2,3,4
y=int(input('enter the element to search'))
if search(x,y):
print('element found',a+1)
else:
print('not found')
pos = -1
def search (list ,n):
#i=0
#while i
pos = -1
def search_num(nums, num):
i = 0
for i in range(len(nums)):
if nums[i] == num:
globals()['pos'] = i
print("found at: ", pos +1)
else:
print("no number")
nums= [3,7,4,8,0,6]
num = 3
search_num(nums, num)
loc=0
def search(list,n):
for i in range(len(list)):
if list[i]==n:
globals()['loc']=i
return True
return False
list=[5,8,4,6,9,2]
n=9
if search(list,n):
print('found at',loc+1)
else:
print('not found')
can you please post videos about data science and machine learning?
x = 5
list = [1,2,3,4,5]
for i in range(len(list)):
if(list[i] == x):
print("Item found at index " + str(i))
elif(i >= len(list)-1):
print("Item not found" + str(i))
else:
continue
def search(list, n):
for i in range(len(list)):
if i == n:
i-=1
print(list[i])
return list[i]
list = [1,2,3,4,5,6,7,8]
n = 3
if search(list,n):
print("found")
else:
print("not found
invaild syntax")
Sir,I have wrote a code by taking input from user for linear search using for loop
p=0
n=int(input('enter length of list '))
a=[]
for i in range(n):
a.append(int(input('enter no ')))
print(a)
b=int(input('enter no u want to search '))
def search(a,b):
for i in range(len(a)):
if a[i]==b:
globals()['p']=i
return True
return False
if search(a,b):
print('found at ',p+1)
else:
print('not found')
Assignment :
def search(lst, n):
for i in range(len(lst)):
if lst[i] == n:
print(i)
return True
return False
l = [5, 8, 4, 6, 9, 2]
value = 9 # the value we want to search for
if search(l, value):
print('Found')
else:
print('Not Found')
pos = -1
def search(list, n):
if list[i]==n:
globals()['pos'] = i
return True
return False
List1 = [5, 8, 4, 6, 9, 2]
n = 9
if search(list1, n):
print('Found')
else:
print('Not found')
using for loop
pos=-1
def search(list,n):
for i in range(len(list)):
if list[i]==n:
globals() ['pos'] = i
return True
else:
return False
list=[5,8,4,6,9,2]
n=2
if search(list,n):
print("found at ",pos+1)
else:
print("not found ")
# Program : Linear search with "For Loop"
pos = 0
def search(list, n):
for i in list:
if i == n:
globals()['pos'] = list.index(n)
return True
return False
list = [5,8,4,6,9,2]
n = 9
if search(list, n):
print("Found at position", pos+1)
else:
print("Not Found")
Maybe a little bit simpler:
a=[1,5,9,7,8,3,13]
n=8
for each_num in a:
if each_num==8:
print (str(each_num)+" is present at:"+str(a.index(each_num)))
else:
print ("specified number is not present")
pos = -1
def search(list, n):
i = 0
for i in range(len(list)):
if list[i] == n:
globals()['pos'] =i
return True
list = [123, 45, 678, 456, 7876, 22345, 21]
n = 21
if search(list, n):
print("Found at Position",pos+1)
else:
print("Notfound")
def ls(l, n):
for i in range(len(l)):
if l[i] == n:
print(f"found the value {n} at {i} ")
break
else:
print("not found")
l = [2,6,3,7,55,78,42,99]
n = input("enter the value: ")
ls(l, n)
def search(lst,n):
for i in range(0, 4):
if lst[i] == n:
print('t',i)
i += 1
break
else:
print('f')
lst = [2,4,2,7,9]
n=7
s=search(lst,n)
pos = 0
def use_for_lop(lsit,value):
for i in range(len(list)):
if list [i] == value:
globals()['pos']=i
return True
list = [1, 2, 3, 4, 5,6 ]
value = 4
if use_for_lop(list,value):
print('found at ', pos +1)
else:
print('not found')
Awesome tutorials. I liked watching tutorials. Planning to watch and learn all :) Again thanks for this wonderful video.
Very good explanation 👏
# LS
# just made 2 changes accordingly
pos = -1
def search(list,n):
for i in range(len(list)):
globals() ['pos'] = i
if list[i] == n:
return True
return False
list = [1,3,6,9,10,4]
n = 3
if search(list, n ):
print("found",pos)
else:
print("not found")
# linear search
pos = -1
def search(list,n):
i = 0
#while i
position = -1
def linear(list, x):
for i in range(len(list)):
globals()['position'] = i
if list[i] == x:
return i
return 0
list = [2, 3, 78, 21, 67, 73, 43, 90]
x = 78
if linear(list, x):
print("item is found at:", +position)
else:
print("item is not found..!")
Output :-
item is found at: 2
list = [7,9,9,1,7,4,8,5]
num = int(input("search for the value"))
print("The indexes of the value are:", end="")
for i in range(0, len(list)):
if list[i] == num:
print(i, ",", end="")
else:
print("not found")
Short and sweet, I hope
pos=-1
def search(list,n):
for x in range (len(list)):
if list[x]==n:
globals()['pos']=x
return True
return False
list=[3,2,5,6,9,8]
n=9
if search(list,n):
print("Found",pos+1)
else:
print("Not Found")
#Linear Search for a value in the list using for loop and function.
indexposition = -1
def search(list, n):
for i in range(len(list)):
if list[i] == n:
globals()['indexposition'] = i
return True
else:
return False
list = [23,12,73,33,19]
n = 19
if search(list, n):
print("Value is",n,"and is found at position no.", indexposition + 1 )
else:
print("Value is",n,"and Not found in the list, try again")
thank you for another wondeful lesson Navin!!
just one question:
i tryied this code without the 'pos = -1' in the beginning and it works all the same.. so why do we actually need it for? thanks 🙂
pos = -1
def search (list,n):
for i in range (len(list)):
if list[i]==n:
globals()['pos'] = i
return True
else:
return False
try:
n = int(input("Enter the integer number to find in list "))
list = [2,4,1,5,7,8]
if search(list,n):
print ("found at position", pos)
else:
print ("not found")
except:
print ("Enter the valid integer")
Def search (list,n):
For i in range(len(list)):
If list[i] == n:
Print(" index" ,list.index(n))
return true
i += 1
List = [1,2,3,4,5,6]
N =5
If search (list ,n):
Print("found")
Else:
Print("Not Found ")
def linear_search(data, target):
for i in range (len(data)):
if data[i] == target:
return True
return False
data = [12,23,34,45,56,67,78,89,90]
target = 56
if linear_search (data, target):
print("Found")
else:
print("Not Found")
def LinearSearch(list,num):
for i,val in enumerate(list):
if (val == num):
return True
return False
list = [5,8,4,6,9,2]
num = 9
if(LinearSearch(list,num)):
print("Found")
else:
print("Not found")
list=[3,4,6,78,5]
n=3
po=None
def found(list,n):
global po
for i in range(len(list)):
if list[i] == n:
po=i
return True
else:
return False
if found(list,n):
print('found at '+str(po))
else:
print('not found')
pos = -1
def search(l,n):
for i in range(len(l)):
if l[i]== n:
globals()["pos"] = i
return True
return False
l = [8,5,6,1,2,3]
n = 5
if search(l,n):
print("found at ", pos+1)
else:
print("not found")
n=int(input("Enter the no. of elements"))
list=[]
for i in range(n):
x=int(input("Enter next value"))
list.append(x)
print(list)
n=int(input("please enter the item to search"))
def search(list,n):
for j in range(len(list)):
if list[j]==n:
return True
return False
if search(list,n):
print("ya, got it!")
else:
print("please enter correct element")
n=int(input())
arr=[]
flag=0
for i in range(0,n):
ele=int(input())
arr.append(ele)
k=int(input())
for i in range(0,n):
if(k==arr[i]):
flag=1
if(flag==1):
print("element found at",i+1)
else:
print("Element not found")
list=[2,8,7,9,6,4,3,5,1]
n=int(input("please give the value between 1-9 : "))
def search(list,n):
for i in list:
if list[i]!=n:
pass
elif list[i]==n:
print("found at ",i+1)
return True
search(list,n)
def search(list, n):
for i in range(0, len(list)):
if list[i] == n:
return i
return -1
list = [1,3,5,7,9,0,6]
n = 7
result = search(list, n)
if result != -1:
print("Number found at ",result)
else:
print("Number not found")
list=(3,6,7,5,45,7,9,26,4)
def search(list):
value=int(input("Enter the value you want to search from list"))
index=-1
for x in list:
index += 1
if x==value:
print("value exist in list"," , INDEX VALUE =",index)
break
else:
print("not found")
search(list)
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:
print("Not found")
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')
Im a CS Student Sir Thank you so very much for you videos it helps me a lot your the best u deserve a like and a subscribe :)
def search(lit,n):
for i in range(len(lit)):
globals()['pos']=i
if lit[i] == n:
return True
return False
list=[1,3,4,5]
n=4
if search(list,n):
print("i found that ",pos)
else:
print("i cant find that")
n=4