Lists
Up until now we have been using variables to store temporary data. For example, we used the variable FName for first name, LName for last name, i for a loop counter, and others.
What if we want to use a variable to hold more than one piece of information, like 10 first names? One solution may be to create 10 unique variable, like FName1, FName2, etc., but what if we want to store 1,000,000 first names?
The solution is to use lists, which is a data type that allows a variable to hold more than one piece of information, or "elements".
The following example demonstrate how a list can be used. Type this code into Python and experiment with it to try to understand everything about it.
# to generate random numbers
import random
# initialize the list
testList=[]
# create and print the elements in the array
for i in range(5):
testList.append(random.randint(0,100))
print ("testList[", i, "] = ", testList[i])
# print some information about the array
print ("The random numbers are", testList)
print ("The number of elements in the list (i.e. its length or size) are", len(testList))
print ("The lowest number in the list is", min(testList))
print ("The highest number in the list is", max(testList))
Assignment
Use a for loop to create and store 10 random numbers into a list, then a second for loop to print the numbers.
Save as array01.py.