Lists and Sequences

Lists

1.  Make a list of grades.

grades_list = [89, 78, 99, 85]

write a program that prints out the grades, each on its own line.  Use a for loop.

Save this program as lists0.py

write a program using a for loop that adds up all the elements in the array.  One way to do it is with a variable “total”.

Save this program as lists1.py

2.  Make a program that asks you for grades and stores them in a list until you say “done”.  (Use a while loop for this, and grades_list.append to get the values into the list. Remember you can make an empty list with

grades_list = [ ]

When you’re done entering grades, have the program find and print out  the average grade.

Save this program as lists2.py

3.  Write a program (lists3.py) that asks the user to enter one word at a time, until the user presses “Enter” with no word.   Then the program prints out the words in an alphabetized list, with each word capitalized (whether the user put them in capitalized or not.)

Sequences

Sequences are like lists, but you can’t add to them or remove stuff from them.   They look the same too, but with parentheses (5, 7. 8) instead of square brackets [5, 7, 8].   One example of a sequence you’ve already seen is colors in the turtle module.  Write a program that asks the user for three numbers between 0 and 255. The first will be the amount of red, the second the amount of green, and the third the amount of blue.  Once the user has entered all three numbers, have the turtle draw a square and fill it with the color entered.  Save this program as sequence.py.

And More…

Make a new program (lists4.py) that reads the information from a text file.  Use a paper or something you’ve written for another class and print out the words alphabetically.   Make sure you’re reading a text file and not a document.  You might have to copy and paste into a text editor   You may have to look some stuff up to make this work.

Resources

A good website showing some of what you can do with lists.