Python Programming

Python Programming

List Lab

1 Purpose

This lab explores the use of lists and while loops. Even though for loops can be used for this, you are expected to use while loops for these exercises. If you use for loops, you will receive no credit for that part.

2 To Be Submitted

Each part requires you to write a separate program. You will not be submitting the complete programs. Instead, you will submit a single answer file that contains the code from each section. Label each answer with the name of the section. Your solutions should be named with your last name, first initial, -lab6, as in doej-lab6. Your file should be .rtf, .doc, or .odt (not .py, since it isn't a program).

3 Assignment

You should know how assignment of atomic variables works in Python. Enter the following to the interpreter and note the results. x=5 y=x x y x = 10 x y Now, enter the following. The only difference is that the variables represent lists instead of ints. x = [5, 10, 15, 20] y=x x y x[2] = 30 x y How do you explain this difference? The answer lies in how ints and lists are represented in memory. If you want to make a copy of a list to assign to another variable, assign a slice. Copy your answers into your answer file.

1

4 Accessing Elements

You often need to process each element of a list individually. From the above, you know that x[2] refers to the element in position 2 of the list.

The following while loop visits each element of a list and prints its square.

x = eval(input('Enter a list of integers: '))

i=0 while i < len(x):

print(x[i] ** 2) i=i+1 Note that the LCV is used as an index into the list. Also note the while condition that determines when the loop stops. Create a Python program for this code and run it. Make sure you understand how it works before continuing. The following sections are based on this code, which will be modified for each section.

5 Changing Elements

The previous section had you find squares of a list's elements and print them. Change the above program so that it replaces each element by its square. If x = [2, 4, 2, 6, 2, 8, 10], then x will be changed to x = [4, 16, 4, 36, 4, 64, 100].

Copy your code into your answer file.

6 Creating a Modified Copy of a List

If you've made it this far, you can access each element of a list, and can change each element using loops. Suppose you want a list of the squares of a list's elements, but don't want to change the original list?

Write code that - given a list of integers - creates a new list of the squares of the integers. When finished, there will be 2 lists: the original, and a new list with the squares. Start with an empty list. You can add new elements to the list using append, extend, insert, slices, etc. The choice is up to you. Use variable y for the new list.

Copy your code into your answer file.

7 Deleting All Occurrences of a Value

The method remove deletes the first occurrence of an element from a list (see notes/text). But what if you want to remove all occurrences of this element? This can be accomplished using a loop, where we remove an occurrence each time through the loop, stopping when there are no more. If you start with x = [2, 4, 2, 6, 2, 8, 10] and remove 2, x would equal [4, 6, 8, 10] after processing.

Keep in mind that remove generates an exception if its argument is not in the list, but you can control the loop with the count method or in test.

Copy your code into your answer file.

8 Replacing One Occurrence of a Value With Another

Write code that - given 2 values - replaces the first of occurrence of the first value with the second. For example, if x = [4, 2, 6, 2, 8, 10], and you want to replace 2 with 9, x would equal [4, 9, 6, 2, 8, 10] after the change.

Copy your code into your answer file.

2

9 Replacing All Occurrences of a Value With Another

Rewrite the previous code so that it replaces all occurrences of the one value by the other. For example, if x = [4, 2, 6, 2, 8, 10], and you want to replace 2 with 9, x would equal [4, 9, 6, 9, 8, 10] after the change. Copy your code into your answer file.

10 Flatten a List

Lists can have nested lists. For example: x = [2, 4, [6, 8], 10]. In this part, write code that flattens a list. This means that if there is an element that is a list, it is "unlistified". Using the example above, flattening x would create the list [2, 4, 6, 8, 10]. To do this, you need to check the data type of each element, and if the type is a list, loop through it adding the elements to the answer. Note that you create a new list and do not modify the original. Copy your code into your answer file.

11 Union

Suppose you have 2 lists and want to combine them so that there are no duplicate values (assuming each list has no duplicates in it). For example, if x = [4, 2, 6, 8, 10] and y = [5, 4, 7, 10, 9], the union would be [4, 2, 6, 8, 10, 5, 7, 9] (with the elements in no particular order). You could do this with nested loops. Another way would be to change each list into a set, perform set union, and convert the result back into a list. Write code to do this using each of the two approaches mentioned above. The original lists should not be altered: use a new list for the union. Copy your code into your answer file.

3

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download