Python ch 7

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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

[3, 4, 5]

7.21 Write a set of nested loops that display the contents of the numbers list shown in Checkpoint question 7.19.

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

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

7.22 The primary difference between tuples and lists is that tuples are immutable. That means that once a tuple is created, it cannot be changed.

7.23 Give two reasons why tuples exist.

7.23 Here are three reasons: r 1SPDFTTJOHBUVQMFJTGBTUFSUIBOQSPDFTTJOHBMJTU TPUVQMFTBSFHPPE choices when you are processing lots of data and that data will not be modified. r 5VQMFTBSFTBGF#FDBVTFZPVBSFOPUBMMPXFEUPDIBOHFUIFDPOUFOUTPGB tuple, you can store data in one and rest assured that it will not be modified (accidentally or otherwise) by any code in your program. r 5IFSFBSFDFSUBJOPQFSBUJPOTJO1ZUIPOUIBUSFRVJSFUIFVTFPGBUVQMF

7.15 What is the difference between calling a list's remove method and using the del statement to remove an element?

The remove method searches for and removes an element containing a specific value. The del statement removes an element at a specific index.

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

[0, 1, 2]

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

[10, 10, 10, 10, 10]

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

[1]

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

[2, 3]

7.18 Describe the following list methods: a. index b. insert c. sort d. reverse

a) The index method searches for an item in the list and returns the index of the first element containing that item. b) The insert method inserts an item into the list at a specified index. c) The sort method sorts the items in the list to appear in ascending order. d) The reverse method reverses the order of the items in the list.

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

mylist = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

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

4

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

1 3 5 7 9

1. This term refers to an individual item in a list. a. element b. bin c. cubbyhole d. slot

element

7.6 How do you find the number of elements in a list?

Use the built-in len function.

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

You can use the built-in min and max functions.

7.17 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.

2. This is a number that identifies an item in a list. a. element b. index c. bookmark d. identifier

index

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

my_list = list(my_tuple)

7.14 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']

7.19 Look at the following interactive session, in which a two-dimensional list is created. 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.

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

[2, 3]

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

[1, 2, 3, 4, 5]

7.8 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]

7.7 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]

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

[1, 2, 99, 4, 5]

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

my_tuple = tuple(my_list)


संबंधित स्टडी सेट्स

removing a peripheral IV catheter in adults

View Set

EASA Part 66 : Physics Question10

View Set