SlideShare a Scribd company logo
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

More Related Content

PPTX
Graphing Polynomials
DOCX
Math IA
DOCX
SL Math IA
PDF
Pre-Cal 40S Slides May 29, 2007
PPT
Chapter 11 - Sorting and Searching
DOCX
Math ib ia lacsap's fraction
PPTX
Marcos reyes period 6
DOCX
Ao k math - math ia
Graphing Polynomials
Math IA
SL Math IA
Pre-Cal 40S Slides May 29, 2007
Chapter 11 - Sorting and Searching
Math ib ia lacsap's fraction
Marcos reyes period 6
Ao k math - math ia

What's hot (20)

PPTX
Applied Calculus Chapter 1 polar coordinates and vector
 
DOCX
Math IA
PDF
Chapter 3: Linear Systems and Matrices - Part 2/Slides
PDF
Chapter 3: Linear Systems and Matrices - Part 3/Slides
PDF
Chapter 3: Linear Systems and Matrices - Part 1/Slides
DOC
Math ia
PPTX
Matriks
PPT
Algebra 2 unit 12.1
PDF
Math ia
PDF
Chapter 4: Vector Spaces - Part 4/Slides By Pearson
PPT
Objective 1 - Identifying Functions
PPT
Algebra 2 unit 12
PPTX
Sorting (Bubble,Merge,Selection sort)
PDF
APM.pdf
PPTX
Matrix presentation By DHEERAJ KATARIA
PPT
PPTX
Rank of a matrix
PDF
Pre-Cal 40S Slides January 16, 2008
PPT
determinants.ppt
PPTX
Advanced algebra
Applied Calculus Chapter 1 polar coordinates and vector
 
Math IA
Chapter 3: Linear Systems and Matrices - Part 2/Slides
Chapter 3: Linear Systems and Matrices - Part 3/Slides
Chapter 3: Linear Systems and Matrices - Part 1/Slides
Math ia
Matriks
Algebra 2 unit 12.1
Math ia
Chapter 4: Vector Spaces - Part 4/Slides By Pearson
Objective 1 - Identifying Functions
Algebra 2 unit 12
Sorting (Bubble,Merge,Selection sort)
APM.pdf
Matrix presentation By DHEERAJ KATARIA
Rank of a matrix
Pre-Cal 40S Slides January 16, 2008
determinants.ppt
Advanced algebra
Ad

Similar to LIST IN PYTHON-PART 4[SEARCHING IN LIST] (20)

PPT
Data Structures- Part3 arrays and searching algorithms
PPTX
Lecture of algorithms and problem solving
PPTX
UNEC__1683196273.pptx
PPTX
Data structure using c module 3
PPT
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
PDF
Algorithms Lecture 6: Searching Algorithms
PPTX
sorting and searching.pptx
PPTX
Searching and Sorting algorithms and working
PPTX
Chapter3.pptx
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PPTX
Data Structure Searching.pptx
PPTX
Unit III Version I.pptx
PPTX
Insertion Sort, Quick Sort And Their complexity
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
PPT
1 D Arrays in C++
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
PPT
Computer notes - Binary Search
PPTX
Mca ii dfs u-3 linklist,stack,queue
PPT
computer notes - Data Structures - 32
PDF
CP PPT_Unit IV computer programming in c.pdf
Data Structures- Part3 arrays and searching algorithms
Lecture of algorithms and problem solving
UNEC__1683196273.pptx
Data structure using c module 3
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
Algorithms Lecture 6: Searching Algorithms
sorting and searching.pptx
Searching and Sorting algorithms and working
Chapter3.pptx
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
Data Structure Searching.pptx
Unit III Version I.pptx
Insertion Sort, Quick Sort And Their complexity
Searching Sorting-SELECTION ,BUBBBLE.ppt
1 D Arrays in C++
Bsc cs ii dfs u-2 linklist,stack,queue
Computer notes - Binary Search
Mca ii dfs u-3 linklist,stack,queue
computer notes - Data Structures - 32
CP PPT_Unit IV computer programming in c.pdf
Ad

More from vikram mahendra (20)

PPTX
Communication skill
PDF
Python Project On Cosmetic Shop system
PDF
Python Project on Computer Shop
PDF
PYTHON PROJECT ON CARSHOP SYSTEM
PDF
BOOK SHOP SYSTEM Project in Python
PPTX
FLOW OF CONTROL-NESTED IFS IN PYTHON
PPTX
FLOWOFCONTROL-IF..ELSE PYTHON
PPTX
FLOW OF CONTROL-INTRO PYTHON
PPTX
OPERATOR IN PYTHON-PART1
PPTX
OPERATOR IN PYTHON-PART2
PPTX
USE OF PRINT IN PYTHON PART 2
PPTX
DATA TYPE IN PYTHON
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
PPTX
USER DEFINE FUNCTIONS IN PYTHON
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
PPTX
INTRODUCTION TO FUNCTIONS IN PYTHON
PPTX
Python Introduction
PPTX
GREEN SKILL[PART-2]
PPTX
GREEN SKILLS[PART-1]
PPTX
Dictionary in python
Communication skill
Python Project On Cosmetic Shop system
Python Project on Computer Shop
PYTHON PROJECT ON CARSHOP SYSTEM
BOOK SHOP SYSTEM Project in Python
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
FLOW OF CONTROL-INTRO PYTHON
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART2
USE OF PRINT IN PYTHON PART 2
DATA TYPE IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
INTRODUCTION TO FUNCTIONS IN PYTHON
Python Introduction
GREEN SKILL[PART-2]
GREEN SKILLS[PART-1]
Dictionary in python

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
Lesson notes of climatology university.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Presentation on HIE in infants and its manifestations
PPTX
master seminar digital applications in india
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Structure & Organelles in detailed.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Complications of Minimal Access Surgery at WLH
Final Presentation General Medicine 03-08-2024.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
A systematic review of self-coping strategies used by university students to ...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Cell Types and Its function , kingdom of life
Lesson notes of climatology university.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Anesthesia in Laparoscopic Surgery in India
STATICS OF THE RIGID BODIES Hibbelers.pdf
VCE English Exam - Section C Student Revision Booklet
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Presentation on HIE in infants and its manifestations
master seminar digital applications in india
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Structure & Organelles in detailed.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

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? It is the process of finding the position of a particular element in a list. We can do searching in two ways:
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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.
  • 10. 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=
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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.
  • 18. 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.
  • 19. 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=
  • 20. 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
  • 21. 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.
  • 22. 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
  • 23. 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
  • 24. 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)
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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.
  • 29. 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.
  • 30. 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.
  • 31. 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