Python Ch. 8

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

insert(index, item)

Inserts item into the list at the specified index.

reverse()

Reverses the order of the items in the list.

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

1 3 5 7 9

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

4

append(item)

Adds item to the end of the list.

sort()

Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value).

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

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

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

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

Use the built-in len function.

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

my_list = list(my_tuple)

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

my_tuple = tuple(my_list)

outfile = open('cities.txt', 'w') outfile.writelines(cities) outfile.close()

opens file and sets to write writes the list closes file

8.23 Give two reasons why tuples exist.

Here are three reasons: • 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. • 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. • There are certain operations in Python that require the use of a tuple.

remove(item)

Removes the first occurrence of item from the list. A ValueError exception is raised if item is not found in the list.

index(item)

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.

8.9 What will the following code display? 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)

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

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

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

[0, 1, 2]

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

[1, 2, 3, 4, 5]

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

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

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

[1, 2, 99, 4, 5]

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

[10, 10, 10, 10, 10]

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

[1]

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

[2, 3]

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

[3, 4, 5]


Set pelajaran terkait

APUSH period 3 CB Multiple choice

View Set

French School Subjects and School Supplies

View Set

Math Lesson 59 Subtracting Mixed Numbers with Renaming

View Set

ACCT 2220 - Chapter 3 Preview: Analysis of Cost, Volume & Pricing to Increase Profitability

View Set

Substance Abuse, Addiction, Alcohol

View Set

Economics Final Part 2 of #****me

View Set

A Day No Pigs Would Die Chapters 1-2

View Set