PYTHON

profileLITTLE
CSCI333_Assignment06.docx

CSCI 333.01W Assignment 06

Lists and Tuples

Deadline: 2/26/2020 Friday by 11:59pm

1. (20 points, each 2 points) True or False questions:

1) (True/False) List is collection that are ordered, changeable, and allows duplicate members, while tuple is ordered, unchangeable, and also allows duplicate members.

Answer:

2) (True/False) Slice operations that modify a sequence work identically for list, tuple and strings.

Answer:

3) (True/False) Even though a tuple is immutable, its elements can be mutable objects, such as lists.

Answer:

4) (True/False) When you pass a list (a mutable object) to a function, the function receives a reference to the original list object, and can use that reference to modify the original list’s contents

Answer:

5) (True/False) Not all sequences provide a sort() method. Immutable sequences like tuples and strings do not provide a sort method. However, you can sort any sequence like strings, tuples, lists, without modifying it by using build-in function sorted(), which returns a new list containing the sorted elements of its argument sequences.

Answer:

6) (True/False) All objects in Python has its own id, it’s guaranteed to be unique and remains constant for this object during its lifetime

Answer:

7) (True/False) myTuple = (1) myTuple is a Tuple

Answer:

8) (True/False) Removing individual tuple elements is not possible. But you can put together a new tuple with the undesired elements discarded

Answer:

9) (True/False) Lists and tuples can hold values of any type, strings are sequences of characters.

Answer:

10) (True/False) myTuple = (1) creates a tuple (1).

Answer:

2. (30 points, each 3 points) Multiple choice or Fill in blank questions:

1) Python’s string and tuple sequences are _______.

a) mutable

b) immutable

Answer:

2) Write output for the following statement:

mylist = [1, 2, a, b, c]

print (mylist[1:3])

Answer:

3) Given mylist[1,2,3,4,5,6,7,8,9,10], del mylist[-2] removes the value ____ from the list

a) 2

b) 3

c) 8

d) 9

Answer:

4) Assume you have a list called mylist. The slice expression ____ creates a new list with the element of names in reverse order:

Answer:

5) To sort a list in descending order, call list method sort with the optional keyword argument ________ set to true.

Answer:

6) What’s the result for the operation: 2 in [2, 3,4]

Answer:

7) What’s the result for the operation: numbers = [1, 2, 3, 4, 5] print(numbers.index(10))

Answer:

8) What’s the result for the operation: a = [1, 2, 3] print(a.index(3))

Answer:

9) What’s the result for the operation: print(max((1, 6, 3)))

Answer:

10) What’s the result for the operation: print(list(('a', 'b', 21)))

Answer:

3. (20 points) Hand-trace the following code. What is the output, or what error/problem do you observe and why?

1) (4 points)

mylist = [‘a’, ‘b’, ‘c’]

mylist.remove(‘a’)

print(mylist)

Output:

2) (4 points)

mylist = [1,2,3,4]

mylist.clear()

print(len(mylist))

Output:

3) (4 points)

my_list = []

For x in range(3,7):

my_list.append(x**2)

print(my_list)

Output:

4) (4 points)

list1=[1,5,3]
list2 = list1
list1.sort(reverse=True)
print(list1)
print(list2)

Output:

5) (4 points)

list1 = [1,5,3]

list2 = list1[:]

list1.append(20)

print(list1[::-1])

print(list2)

Output:

4. (20 points) Programming

· Create a list containing the odd elements between 0 and 20.

· Search for element 15, 25, if element exist, return the value of the element and its index position in the list. If element does not exist, ensure no ValueError occurs and output “cannot find the element” message and element value

· (15 points) Write your program here, or copy/paste a screenshot of your Program:

· (3 points) copy/paste Program Run:

· (2 points) Save the program as “searchList.py”. Upload the .py file as part of your submission.

5. (20 points) Use a list comprehension and the range function with a step to create a list of the multiples of 3 that are less than 30.

The output should be [3,6,9,12,15,18,21,24,27]

· (15 points) Write the program here, or copy/paste the screenshot of your program

· (3 points) Screenshot of a Program Run:

· (2 points) Save the program as “listComprehension.py”. Upload the .py file as part of your submission.

2