Learners will be able to:
- Learn how to manipulate lists
- How to combine Loops to check for data in a list
- Know more amazing Python
In this lesson you will learn more about lists and how to manipulate them. If you have not used lists before then check out the first 'list' lesson, if not, check out part 1.
Part 1: Making a List:
Creating a list is very simple, create a variable to store the list and then use [ and ] to complete the list, it is really that simple.
Part 1: Making a List:
Creating a list is very simple, create a variable to store the list and then use [ and ] to complete the list, it is really that simple.
- Look at the code below and run it,
- What is happening explain it to another students
- On line 2 change the None to 5, what happens?
Part 2: Looping in a List:
- Look at the code below and run it, explain what happens.
- On line 15 uncomment, remove the # from the code and run.
- What happens and why?
- Can you adapt the code to add Mark to the list?
Part 3: Appending to Lists:
A clean way to add to a list is to use append, this appends or adds items to a lists without and error occurring.
A clean way to add to a list is to use append, this appends or adds items to a lists without and error occurring.
- Run the code example below
- On lines 15 and 19 remove the comments and run the code.
- What happens and why?
- Use the code to add five more names or items of your choice to the list.
Part 4: Printing single entries in the list:
Sometimes you will want to print one single name or item from the list. This is simple print statement with the position number of the item that you want to print from the list. For example, print(names[4]) prints the forth item.
Sometimes you will want to print one single name or item from the list. This is simple print statement with the position number of the item that you want to print from the list. For example, print(names[4]) prints the forth item.
- Print the names John and Eric
- Print the first item in the list, does it work? what happens and why?
- Ask another student for a random number between 0 and 10, print that item place.
- Explain why it is between 0 and 10 and not 1 and 10.
Part 5: Printing out all the items in the List:
So how do you print all the items in the list? This uses a loop to iterate through each item and do something to it, it this case print it.
So how do you print all the items in the list? This uses a loop to iterate through each item and do something to it, it this case print it.
- Look at the code below and run it.
- Use a for loop to iterate through the list and print each item.
- You will need to find the length of the list using len(names).
- Change the items in the lits to numbers
- Loop through and add 5 to each number
- Print each new value out.
Part 6: Finding the position of an item in the List:
Now you can print an item from the list you can learn how to find the position, this is useful for checking if an item is present in the list.
print("John is in position",position)
Further reading here:
Now you can print an item from the list you can learn how to find the position, this is useful for checking if an item is present in the list.
- Look at the code below, what does line 13 - 16 do?
- Run the code, did it do what you expected?
- Change the name on line 13 to a name in the list, does it work?
- On line 18 write a line to ask the user to enter a name to search for, (name = .....)
- Adapt the code so that the program looks for the name entered in step 4.
- Now modify the code to look for the name and if the name is in the list, to print the position of the name. Use the code below as a starting point.
print("John is in position",position)
- What is the code index doing?
Further reading here: