LIST IN PYTHON
CLASS : XII
COMPUTER SCIENCE(083) PART-4
SEARCHING IN LIST
What is searching?
It is the process of finding the position of a particular
element in a list.
We can do searching in two ways:
Linear search
In linear search, each element or item of list is compared
with the item or value to be searched for, one by one.
It is also known as Sequential search. It works in both
sorted and unsorted list of numbers.
Example Unsorted numbers:
10 15 40 18 29 38 5 37
Example sorted numbers:
10 15 17 18 29 38 45 67
Let’s Understand Linear Search with the help of
Example:
There are six batches in a sequence:
Block:1 Block:2 Block:3 Block:4 Block:5 Block:6
So if user want to find or search batch from the sequence and want its position, where found:
If user want to search Excellent from the batches in a sequence
Block:6
Let’s Understand Linear Search with the help of
Example:
Now in linear search the searching of excellent batch starts from first block to last block:
Block:1 Block:2 Block:3 Block:4 Block:5
So Excellent batch first compare with Block 1 batch:
Is Batch Excellent equal to Amazing. NO
Now its move to the next block that is Block 2
Block:6
Let’s Understand Linear Search with the help of
Example:
Now Excellent compare with Block 2 “Wonderful”
Block:1 Block:2 Block:3 Block:4 Block:5
Is Batch Excellent equal to Wonderful. NO
Now its move to the next block that is Block 3
Block:6
Let’s Understand Linear Search with the help of
Example:
Now Excellent compare with Block 3 “Super”
Block:1 Block:2 Block:3 Block:4 Block:5
Is Batch Excellent equal to Super. NO
Now its move to the next block that is Block 4
Block:6
Let’s Understand Linear Search with the help of
Example:
Now Excellent compare with Block 4 “Brilliant”
Block:1 Block:2 Block:3 Block:4 Block:5
Is Batch Excellent equal to Brilliant. NO
Now its move to the next block that is Block 5
Block:6
Let’s Understand Linear Search with the help of
Example:
Now Excellent compare with Block 5 “Excellent”
Block:1 Block:2 Block:3 Block:4 Block:5
Is Batch Excellent equal to Excellent. Yes
Now its Stop here only and exit from the Sequence(means loop)And Print the message that excellent exist in Sequence.
So now Let Us understand this linear
search with another example
If there is a sequence of numbers in a list
no=[10,15,40,18,29,38,5,37]
Example Unsorted numbers:
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
Now if user enter number 18 to be
searched from a list
18
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
val=
Now we have to search this number in a list no and if
number found then store the index value as the position
of number found.
Pos=-1 It will store the position of the number found or not found
So next is to accept the value from user in a variable val to be searched in a list
Loop starts from first index no 0
(zero) to last index no 7(seven)
18
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
val=
Pos=-1
LOOP
FROM 0 TO 7
Loop AT FIRST INDEX VALUE IS 0
18
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
val=
First value at index no 0(zero) that is 10 compare with val no 18
Pos=-1
Comparison of these values are same NO
If Both values are not same then it move to
next block of the list
Loop AT SECOND INDEX NO 1
18
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
val=
Second value at index no 1(One) that is 15 compare with val no 18
Pos=-1
Comparison of these values are same NO
If Both values are not same then it move
to next block of the list
Loop AT THIRD INDEX NO 2
18
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
val=
Third value at index no 2(Two) that is 40 compare with val no 18
Pos=-1
Comparison of these values are same NO
If Both values are not same then it move to next block of the list
Loop AT FOURTH INDEX NO 3
18
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
val=
Fourth value at index no 3(Three) that is 18 compare with val no 18
Pos=-1 Comparison of these values are same Yes
If Both values are same then it
will stop here and store the index
value inside the pos
Loop starts from first index no 0
(zero) to last index no 7(seven)
18
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
val=
Pos=3 Now pos is 3 on the basis of index number and -1 value is
overwrite with index number where value 18 found . And
then we check that value of pos is positive or negative.
Now let us understand the concept
of Linear search
To understand the linear search first we need to know the steps:
Step 1: Declare the list and initialize it with some values
Step 2: Declare one variable to store the position with -1 and value of pos.
will overwrite when number found in the list.
Step 3: Accept the value to be searched from the user and store in a variable.
Step 4: Start the loop from starting index to the last index no
Step 5: Compare the search no with the list of no.’s one by one using index
no.
Step 6: if no. found pos store the index number and loop will stop there only.
no=[10,15,40,18,29,38,5,37]
Step 1: Declare the list and initialize it with some
values
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
no=[10,15,40,18,29,38,5,37]
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
pos=-1 Step 2: Declare one variable to store the position with -1
and value of pos. will overwrite when number found in
the list.
Pos=-1
no=[10,15,40,18,29,38,5,37]
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
pos=-1
val=int(input(“Enter the value”))
Step 3: Accept the value to be searched from the user and
store in a variable.
no=[10,15,40,18,29,38,5,37]
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
Step 4: Start the loop from starting index to the last index
no
no=[10,15,40,18,29,38,5,37]
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
Step 5: Compare the search no with
the list of no.’s one by one using index
no.
if no[x]==val:
pos=x
break
no=[10,15,40,18,29,38,5,37]
0 1 2 3 4 5 6 7
10 15 40 18 29 38 5 37
no=
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
if no[x]==val:
pos=x
break
Step 6: if no. found pos store
the index number and loop
will stop there only and if pos
is negative it means number
not found and if positive then
number found
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1)
For loop with range:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
While loop:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
x=0
while x<k:
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
Step 1 We declare the list of numbers in a variable no
For loop with range:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
While loop:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
x=0
while x<k:
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
Step 2 we set the pos to negative value, if number not found it remains -1
means number not exists in a list
For loop with range:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
While loop:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
x=0
while x<k:
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
Next step is to accept the value in a variable val from user to be searched in a
list
For loop with range:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
While loop:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
x=0
while x<k:
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
Next find the length of list no and store it inside the variable k.
For loop with range:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
While loop:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
x=0
while x<k:
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
Next start the loop that starts from 0 to k means last number.
For loop with range:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
While loop:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
x=0
while x<k:
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1
Next check the number exists or not using if condition,if yes then loop stop and
pos will store the index number as position.
For loop with range:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
for x in range(0,k):
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1)
While loop:
no=[10,15,40,18,29,38,5,37]
pos=-1
val=int(input(“Enter the value”))
k=len(no)
x=0
while x<k:
if no[x]==val:
pos=x
break
if pos<0:
print(“No. not found”)
else:
print(“No found at pos=“,pos+1)
At last if the pos is still negative it means search number does not exist in a list
and if pos is positive, it means number exists in a list

LIST IN PYTHON-PART 4[SEARCHING IN LIST]

  • 1.
    LIST IN PYTHON CLASS: XII COMPUTER SCIENCE(083) PART-4 SEARCHING IN LIST
  • 2.
    What is searching? Itis the process of finding the position of a particular element in a list. We can do searching in two ways:
  • 3.
    Linear search In linearsearch, each element or item of list is compared with the item or value to be searched for, one by one. It is also known as Sequential search. It works in both sorted and unsorted list of numbers. Example Unsorted numbers: 10 15 40 18 29 38 5 37 Example sorted numbers: 10 15 17 18 29 38 45 67
  • 4.
    Let’s Understand LinearSearch with the help of Example: There are six batches in a sequence: Block:1 Block:2 Block:3 Block:4 Block:5 Block:6 So if user want to find or search batch from the sequence and want its position, where found: If user want to search Excellent from the batches in a sequence
  • 5.
    Block:6 Let’s Understand LinearSearch with the help of Example: Now in linear search the searching of excellent batch starts from first block to last block: Block:1 Block:2 Block:3 Block:4 Block:5 So Excellent batch first compare with Block 1 batch: Is Batch Excellent equal to Amazing. NO Now its move to the next block that is Block 2
  • 6.
    Block:6 Let’s Understand LinearSearch with the help of Example: Now Excellent compare with Block 2 “Wonderful” Block:1 Block:2 Block:3 Block:4 Block:5 Is Batch Excellent equal to Wonderful. NO Now its move to the next block that is Block 3
  • 7.
    Block:6 Let’s Understand LinearSearch with the help of Example: Now Excellent compare with Block 3 “Super” Block:1 Block:2 Block:3 Block:4 Block:5 Is Batch Excellent equal to Super. NO Now its move to the next block that is Block 4
  • 8.
    Block:6 Let’s Understand LinearSearch with the help of Example: Now Excellent compare with Block 4 “Brilliant” Block:1 Block:2 Block:3 Block:4 Block:5 Is Batch Excellent equal to Brilliant. NO Now its move to the next block that is Block 5
  • 9.
    Block:6 Let’s Understand LinearSearch with the help of Example: Now Excellent compare with Block 5 “Excellent” Block:1 Block:2 Block:3 Block:4 Block:5 Is Batch Excellent equal to Excellent. Yes Now its Stop here only and exit from the Sequence(means loop)And Print the message that excellent exist in Sequence.
  • 10.
    So now LetUs understand this linear search with another example If there is a sequence of numbers in a list no=[10,15,40,18,29,38,5,37] Example Unsorted numbers: 0 1 2 3 4 5 6 7 10 15 40 18 29 38 5 37 no=
  • 11.
    Now if userenter number 18 to be searched from a list 18 0 1 2 3 4 5 6 7 10 15 40 18 29 38 5 37 no= val= Now we have to search this number in a list no and if number found then store the index value as the position of number found. Pos=-1 It will store the position of the number found or not found So next is to accept the value from user in a variable val to be searched in a list
  • 12.
    Loop starts fromfirst index no 0 (zero) to last index no 7(seven) 18 0 1 2 3 4 5 6 7 10 15 40 18 29 38 5 37 no= val= Pos=-1 LOOP FROM 0 TO 7
  • 13.
    Loop AT FIRSTINDEX VALUE IS 0 18 0 1 2 3 4 5 6 7 10 15 40 18 29 38 5 37 no= val= First value at index no 0(zero) that is 10 compare with val no 18 Pos=-1 Comparison of these values are same NO If Both values are not same then it move to next block of the list
  • 14.
    Loop AT SECONDINDEX NO 1 18 0 1 2 3 4 5 6 7 10 15 40 18 29 38 5 37 no= val= Second value at index no 1(One) that is 15 compare with val no 18 Pos=-1 Comparison of these values are same NO If Both values are not same then it move to next block of the list
  • 15.
    Loop AT THIRDINDEX NO 2 18 0 1 2 3 4 5 6 7 10 15 40 18 29 38 5 37 no= val= Third value at index no 2(Two) that is 40 compare with val no 18 Pos=-1 Comparison of these values are same NO If Both values are not same then it move to next block of the list
  • 16.
    Loop AT FOURTHINDEX NO 3 18 0 1 2 3 4 5 6 7 10 15 40 18 29 38 5 37 no= val= Fourth value at index no 3(Three) that is 18 compare with val no 18 Pos=-1 Comparison of these values are same Yes If Both values are same then it will stop here and store the index value inside the pos
  • 17.
    Loop starts fromfirst index no 0 (zero) to last index no 7(seven) 18 0 1 2 3 4 5 6 7 10 15 40 18 29 38 5 37 no= val= Pos=3 Now pos is 3 on the basis of index number and -1 value is overwrite with index number where value 18 found . And then we check that value of pos is positive or negative.
  • 18.
    Now let usunderstand the concept of Linear search To understand the linear search first we need to know the steps: Step 1: Declare the list and initialize it with some values Step 2: Declare one variable to store the position with -1 and value of pos. will overwrite when number found in the list. Step 3: Accept the value to be searched from the user and store in a variable. Step 4: Start the loop from starting index to the last index no Step 5: Compare the search no with the list of no.’s one by one using index no. Step 6: if no. found pos store the index number and loop will stop there only.
  • 19.
    no=[10,15,40,18,29,38,5,37] Step 1: Declarethe list and initialize it with some values 0 1 2 3 4 5 6 7 10 15 40 18 29 38 5 37 no=
  • 20.
    no=[10,15,40,18,29,38,5,37] 0 1 23 4 5 6 7 10 15 40 18 29 38 5 37 no= pos=-1 Step 2: Declare one variable to store the position with -1 and value of pos. will overwrite when number found in the list. Pos=-1
  • 21.
    no=[10,15,40,18,29,38,5,37] 0 1 23 4 5 6 7 10 15 40 18 29 38 5 37 no= pos=-1 val=int(input(“Enter the value”)) Step 3: Accept the value to be searched from the user and store in a variable.
  • 22.
    no=[10,15,40,18,29,38,5,37] 0 1 23 4 5 6 7 10 15 40 18 29 38 5 37 no= pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): Step 4: Start the loop from starting index to the last index no
  • 23.
    no=[10,15,40,18,29,38,5,37] 0 1 23 4 5 6 7 10 15 40 18 29 38 5 37 no= pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): Step 5: Compare the search no with the list of no.’s one by one using index no. if no[x]==val: pos=x break
  • 24.
    no=[10,15,40,18,29,38,5,37] 0 1 23 4 5 6 7 10 15 40 18 29 38 5 37 no= pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): if no[x]==val: pos=x break Step 6: if no. found pos store the index number and loop will stop there only and if pos is negative it means number not found and if positive then number found if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1)
  • 25.
    For loop withrange: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 While loop: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) x=0 while x<k: if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 Step 1 We declare the list of numbers in a variable no
  • 26.
    For loop withrange: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 While loop: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) x=0 while x<k: if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 Step 2 we set the pos to negative value, if number not found it remains -1 means number not exists in a list
  • 27.
    For loop withrange: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 While loop: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) x=0 while x<k: if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 Next step is to accept the value in a variable val from user to be searched in a list
  • 28.
    For loop withrange: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 While loop: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) x=0 while x<k: if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 Next find the length of list no and store it inside the variable k.
  • 29.
    For loop withrange: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 While loop: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) x=0 while x<k: if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 Next start the loop that starts from 0 to k means last number.
  • 30.
    For loop withrange: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 While loop: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) x=0 while x<k: if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1 Next check the number exists or not using if condition,if yes then loop stop and pos will store the index number as position.
  • 31.
    For loop withrange: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) for x in range(0,k): if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1) While loop: no=[10,15,40,18,29,38,5,37] pos=-1 val=int(input(“Enter the value”)) k=len(no) x=0 while x<k: if no[x]==val: pos=x break if pos<0: print(“No. not found”) else: print(“No found at pos=“,pos+1) At last if the pos is still negative it means search number does not exist in a list and if pos is positive, it means number exists in a list