Python Chapter 8 Terms and Concepts

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

false

A file object's writelines method automatically writes a newline ('\n') after writing each list item to the file.

true

A list can be an element in another list.

false

Assume list1 references a list. After the following statement executes, list1 and list2 will reference two identical but separate lists in memory: list2 = list1

my_tuple = tuple(my_list)

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

my_list = list(my_tuple)

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

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

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')

mylist.insert(0, 'Labrador')

Assume the following statement appears in a program: mylist = [] Which of the following statements would you use to add the string 'Labrador' to the list at index 0?

a. Index; Returns the index of the first element whose value is equal to item. A ValueError exception is raised if item is not found in the list. b. Insert; When an item is inserted into a list, the list is expanded in size to accommodate the new item. c. Sort; Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value). d. Reverse; Reverses the order of the items in the list.

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

1. Processing a tuple is faster than processing a list, so tuples are good choices when you are processing lots of data and that data will not be modified. 2.Tuples are safe. Because you are not allowed to change the contents of a 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

Give two reasons why tuples exist.

False

The del statement deletes an item at a specified index in a list.

max

This built-in function returns the highest value in a list.

readlines

This file object method returns a list containing the file's contents.

len

This function returns the length of a list.

index

This is a number that identifies an item in a list.

0

This is the first index in a list.

The size of the list minus one

This is the last index in a list.

add

This list method adds an item to the end of an existing list.

The del statement

This removes an item at a specific index in a list.

element

This term refers to an individual item in a list.

an IndexError exception will occur

This will happen if you try to use an index that is out of range for a list.

true

Tuples in Python are immutable.

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

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

Use the built-in len function.

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

A ValueError exception is raised

If you call the index method to locate an item in a list and the item is not found, this happens.

false

Lists in Python are immutable.

The list contains 4 rows and 2 columns.

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

true

You can use the + operator to concatenate two lists.

false

You can remove an element from a tuple by calling the tuple's remove method.

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

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

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

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

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

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)

[3, 4, 5]

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

numbers = [1, 2, 3, 4, 5] my_list = numbers[1:3] print(my_list)

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

[2, 3]

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

[1]

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

[1, 2, 3, 4, 5]

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

[1, 2, 99, 4, 5]

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

4

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

[10, 10, 10, 10, 10]

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

1 3 5 7 9

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

[0, 1, 2]

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

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

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, 10, 20, 30]

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

The repetition operator

When the * operator's left operand is a list and its right operand is an integer, the operator becomes this.

values = (1,)

Which of the following statements creates a tuple?

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

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

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

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


Ensembles d'études connexes

Common Fractions, Decimals and Percents

View Set

LINUX ESSENTIALS 010-150 BEST STUDY GUIDE 2019

View Set

MANA 4322 ORG STRATEGY EXAM ONE - 1,2,3

View Set