replit

Before you start this course, you must login to Replit

If you have forgotten your Replit account user name or password, ask your Coder Coach.

CoderSports


12.11 – Modify

In the modify tasks, students adapt the example code to start to create code of their own.

Make sure that the students add comments to explain what the code does.


In this unit you will learn about: Searching Lists

Loops can be user to search lists using the linear search technique.

There are three ways to do this. The first two are the traditional methods, and the third is a method built into Python.

There’s a lot to unpack with the code at first. Take your time and go through lots of examples to help understand it. Use this excellent python visualiser to show the steps involved.

Method 1 – Search The Whole List Regardless

This method inspects every item in the list.

  • It repeats for every item in the list, even if the item has already been found.
  • The counter variable is used to loop through each item in the list, going up by 1 each time the loop repeats.
  • The ‘found’ variable is used as a flag – it is set to False before the loop starts (we haven’t found anything at this point) and then True if the item is found.
  • Inside the loop is an if…else statement that sets the flag to True if the current item in the list is the one we are looking for.
  • Once the loop finishes, another selection if statement is used to check the found variable to see if it has been set to True during the search.
counter = 0
found  = False

while counter < len(item_list):
	if item_list[counter] == item_looking_for:
		found = True
	counter +=1

if found == True:
	print(item_looking_for + " has been found in the list")
else:
	print(item_looking_for + " is not in the list")

Method 2 – Stop When We Find It

This method uses the found variable as part of the condition.

The loop will only repeat while the counter is < the length of the list and the found variable is False. Once the matching item is found, the found varibale is set to True which ends the loop.

counter = 0
found  = False

while counter < len(item_list):
	if item_list[counter] == item_looking_for:
		found = True
	counter +=1

if found == True:
	print(item_looking_for + " has been found in the list")
else:
	print(item_looking_for + " is not in the list")

Method 3 – Python Hides The Complexity

This method does the hard work for the coder, but it also abstracts away the detail of how a linear search algorithm really works.

item_list = ["pencil", "ruler", "pen", "eraser", "calculator"]
item_looking_for = "pen"


if item_looking_for in item_list:
	print(item_looking_for + " has been found in the list")
else:
	print(item_looking_for + " is not in the list")

Help! My Code Does Not Work!

Make sure to check your code for the following things:

  • Square brackets for items in the list
  • Commas between each item in the list.
  • String items are in quotation marks
  • Range – the end value is NOT included in the output.
  • Search – not initialising counter OUTSIDE the loop.
  • Search – not incrementing counter INSIDE the loop.

Hints