Chapter 7 Flashcards

1
Q

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

A

[1, 2, 99, 4, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

numbers = list(range(3))
print(numbers)

A

[0, 1, 2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

numbers = [10] * 5
print(numbers)

A

[10, 10, 10, 10, 10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

numbers = list(range(1, 10, 2))
for n in numbers:
print(n)

A

1
3
5
7
9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

Use the build-in len function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
numbers3 = numbers1 + numbers2
print(numbers1)
print(numbers2)
print(numbers3)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
numbers2 += numbers1
print(numbers1)
print(numbers2)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

[2, 3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

[2, 3, 4, 5] ? [2, 3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

[1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

[1, 2, 3, 4, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

[3, 4, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

names = [‘Jim’, ‘Jill’, ‘John’, ‘Jasmine’]
if ‘Jasmine’ not in names:
print(‘Cannot find Jasmine.’)
else:
print(“Jasmine’s family:”)
print(names)

A

Jasmine’s family:
[‘Jim’, ‘Jill’, ‘John’, ‘Jasmine’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

The remove method searches for and removes an element containing a specific value.

The del statement removes an element at a specific index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Describe the following list methods:

a. index
b. insert
c. sort
d. reverse

A
  • Index method searches for an item in the list and returns the index of the first element containing that item.
  • Insert method inserts an item into the list at a specified index.
  • Sort method sorts the items in the list to appear in ascending order.
  • Reverse method reverses the order of the items in the list.
19
Q

Look at the following list comprehension:

[x for x in my_list]

What is the result expression? What is the iteration expression?

A

The result expression is: x

The iteration expression is: for x in my_list

20
Q

After this code executes, what value will the list2 list hold

list1 = [1, 12, 2, 20, 3, 15, 4]
list2 = [n*2 for n in list1]

A

[2, 24, 4, 40, 6, 30, 8]

21
Q

After this code executes, what value will the list2 list hold?

list1 = [1, 12, 2, 20, 3, 15, 4]
list2 = [n for n in list1 if n > 10]

A

[12, 20, 15]

22
Q

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

A

The list contains 4 rows and 2 columns.

23
Q

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

A

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

24
Q

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

A

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

25
Q

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

A

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

26
Q

Give two reasons why tuples exist.

A

Processing a tuple is faster than processing a list

Tuples are safe. Because you are not allowed to change the contents of a tuple

27
Q

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

A

my_tuple = tuple(my_list)

28
Q

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

A

my_list = list(my_tuple)

29
Q

This term refers to an individual item in a list.

A

element -

bin

cubbyhole

slot

30
Q

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

A

index

31
Q

This is the first index in a list.

A

0

32
Q

This is the last index in a list.

A

The size of the list minus one

33
Q

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

A

An IndexError exception will occur.

34
Q

This function returns the length of a list.

A

len

35
Q

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

A

The repetition operator

36
Q

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

A

append

37
Q

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

A

the del statement

38
Q

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?

mylist[0] = ‘Labrador’

mylist.insert(0, ‘Labrador’)

mylist.append(‘Labrador’)

mylist.insert(‘Labrador’, 0)

A

mylist.insert(0, ‘Labrador’)

39
Q

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

A

A ValueError exception is raised.

40
Q

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

A

max

41
Q

This function in the random module returns a random element from a list.

A

choice

random_element = random.choice(my_list)

42
Q

This function in the random module returns multiple, nonduplicated random elements from a list.

A

sample

random_elements = random.sample(my_list, 3)

43
Q

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

A

readlines

with open(‘example.txt’, ‘r’) as file:
file_contents = file.readlines()
print(file_contents)

44
Q

Which of the following statements creates a tuple?

A

values = (1,)

In Python, tuples are defined using parentheses. The comma after the single element 1 distinguishes it as a tuple. A single element within parentheses without the comma would be interpreted as an expression in parentheses rather than a tuple.