Part 7 - Lists

This section introduces the concept of a List. It is an easy way to store and manage large sets of data of the same type. You will learn how to create, change, search through and use lists is various ways. They are fundamental for making larger applications and games.

Back to main

What to do/hand in?

1. Read through and complete the Part 7 - Introduction to Lists tutorial. Make sure to complete Exercise 1, and Exercise 2 which requires Chapter 7: Section 7.7 - Secret Codes from Programming Arcade Games.

2. Complete the Part 7 - Worksheet. Attempt to predict what the output will be without actually running the code. Verify your answers afterwards.

Submit:

1. Exercise #1 in part d) - Both parts (1 and 2) in a file called Part7_Ex1.py

2. Exercise #2 in part f) - In a file called Part7_Ex2.py. The user should enter the phrase that is to be encrypted.

3. Part 7 - Worksheet

Part 7 - Introduction to Lists.pdf

Hints and FAQ's

1. Use square bracksts '[ ]' when accessing an index on a list.

2. Use len(list_name) to tell you how many elements are stored in a list.

3. List indexes start at 0, and end at one less than the number of elements in the list. len(list_name) - 1

4. Lists can store numbers, strings, boolean types even other lists, and much more.

5. Here are some useful functions for working with lists:

        • list_name.append(item) - Adds a new item to the end of the list
        • list_name.insert(index, item) - adds a new item to the specified index
        • list_name.remove(item) - searches a list and removes the first occurrence of item
        • list_name.pop(index) - removes an item at the specified index and returns it

6. Use a for loop to iterate through a list. Here are two ways to do it:

Version 1

#Item in this example gives you access to each element in the list.
#Note that you cannot directly change the value of item in the original list
for item in listName:                 
    print(item)                       

or

Version 2

#Here you can use the index value to access an element in the list
#to either retrieve its value, or to change it in the original list
for index in range(len(listName)):     
    print(listName[index])