Chapter 7 - CIS 3389

Ace your homework & exams now with Quizwiz!

What will the following code display? numbers = [ 1, 2, 3, 4, 5 ] my_list = numbers[ -3: ] print(my_list)

[3, 4, 5]

Give two reasons why tuples exist.

1. Certain operations in python require a tuple 2. Processing tuples are faster than processing a list.

What will the following code display? numbers = [ 1, 2, 3, 4, 5 ] print(numbers[ -2 ])

4

What will the following code display? numbers = [ 1, 2, 3, 4, 5 ] my_list = numbers[ 1: ] print(my_list)

[2, 3]

What will the following code display? numbers = [ 1, 2, 3, 4, 5 ] my_list = numbers[ 1:3 ] print(my_list)

[2, 3]

What is the primary difference between a list and a tuple?

a tuple is immutable, which means that once it is created, its contents cannot be changed. a list is mutable, which means that a program can change its contents.

Describe the following list method: - index

a) INDEX - searches for an item in the list and returns the index of the first element containing that item

What will the following code display? numbers = list(range( 1, 10, 2 )) for n in numbers: print(n)

1 3 5 7 9

What will the following code display? names = ['Jim', 'Jill', 'John', 'Jasmine'] if 'Jasmine' not in names: print('Cannot find Jasmine.') else: print("Jasmine's family:") print(names)

Jasmine's family: ['Jim', 'Jill', 'John', 'Jasmine']

What is the difference between calling a list's REMOVE method and using the DEL statement to remove an element.

REMOVE searches for and removes an element containing a specific value. DEL statement removes an element at a specific index

How many rows and how many columns are in the list? numbers = [[1, 2], [10, 20], [100, 200], [1000, 2000]]

The list contains 4 rows and 2 columns

Assume the following statement appears in a program: names = [ ] Which of the following statements would you use to add the string 'Wendy' to the list at index 0? Why would you select this statement instead of the other? a. names[0] = 'Wendy' b. names.append('Wendy')

You would use statement b, names.append('Wendy'). This is because element 0 does not exist. If you try to use statement a, an error will occur

What will the following code display? numbers = list(range(3)) prints(numbers)

[0, 1, 2]

What will the following code display? numbers = [ 1, 2, 3, 4, 5 ] my_list = numbers[ : ] print(my_list)

[1, 2, 3, 4, 5]

What will the following code display? numbers1 = [ 1, 2, 3 ] numbers2 = [ 10, 20, 30 ] numbers2 += numbers1 print(numbers1) print(numbers2)

[1, 2, 3] [10, 20, 30, 1, 2, 3]

What will the following code display? numbers1 = [ 1, 2, 3 ] numbers2 = [ 10, 20, 30 ] numbers3 = numbers1 + numbers2 print(numbers1) print(numbers2) print(numbers3)

[1, 2, 3] [10, 20, 30] [1, 2, 3, 10, 20, 30]

What will the following code display? numbers = [ 1, 2, 3, 4, 5 ] numbers[2] = 99 print(numbers)

[1, 2, 99, 4, 5]

What will the following code display? numbers = [10] * 5 print(numbers)

[10, 10, 10, 10, 10]

What will the following code display? numbers = [ 1, 2, 3, 4, 5 ] my_list = numbers[ :1 ] print(my_list)

[1]

How do you find the lowest and highest values in a list?

by using the min and/or the max functions

Write a nested loop that display the contents of the numbers: numbers = [[1, 2], [10, 20], [100, 200], [1000, 2000]]

for r in range(4): for c in range(2): print(numbers[r][c])

Describe the following list method: - insert

inserts an item into the list at a specified index

Write a statement that creates a two-dimensional list with three rows and four columns. Each element should be assigned the value 0.

my list = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Assume that my_tuple references a tuple. Write a statement that converts it to a list.

my_list = list(my_tuple)

Assume that my_list references a list. Write a statement that converts it to a tuple.

my_tuple = tuple(my_list)

Describe the following list method: - reverse

reverses the order of the items in the list

Describe the following list method: - sort

sorts the items in the list to appear in ascending order

How do you find the elements in a list?

use the built-in *len* function


Related study sets

Lymphatic (Ch22) "Clicker Questions"

View Set

Chapter 29&30: Plants and Conquest of the Land, Gymnosperms and Angiosperms

View Set