Lists and Tuples

Ace your homework & exams now with Quizwiz!

A list can be an element in another list. T or F

True

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

True

Tuples in Python are immutable. T or F

True

You can use the + operator to concatenate two lists. T or F

True

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. b. An Invalidindex exception is raised. c. The method returns - 1. d. Nothing happens. The program continues running at the next statement.

A ValueError exception is raised.

This will happen if you try to use an index that is out of range for a list. a. A ValueError exception will occur. b. An IndexError exception will occur. c. The list will be erased and the program will continue to run. d. Nothing-the invalid index will be ignored.

An IndexError exception will occur.

List

An object that contains multiple data items. Lists are mutable, which means that their contents can be changed during a program's execution.

A Tuple does not support what methods?

Tuples do not support methods such as append, remove, insert, reverse, and sort.

in Operator

can be used to determine whether an item is contained in a list.

append(item) Method

commonly used to add items to a list. The item that is passed as an argument is appended to the end of the list's existing elements.

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

element

Which of the following statements creates a tuple? a. values [1, 2, 3, 4] b. values {1, 2, 3, 4} c. values ( 1) d. values ( 1,)

values ( 1,)

This is the first index in a list. a. - 1 b. 1 c. 0 d. The size of the list minus one

0

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

By using the len Function. The len function takes the list as an argument and it returns the number of the elements present in the list .

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

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 T or F

False

Lists in Python are immutable. T or F

False

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

False

insert(index, item) Method

Inserts an item into the list at the specified index.

Give two reasons why tuples exist.

Processing a tuple is faster than processing a list. Tuples are safe. Because you are not allowed to change the contents of a tuple, you can store data in one and that data can not be modified (accidentally or otherwise) by any code in your program.

remove(item) Method

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

index( item) Method

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.

reverse() Method

Reverses the order of the items in the list.

sort() Method

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

max Function

The max function accepts a sequence, such as a list, as an argument and returns the item that has the highest value in the sequence.

min Function

The min function accepts a sequence, such as a list, as an argument and returns the item that has the lowest value in the sequence.

When the * operator's left operand is a list and its right operand is an integer, the operator becomes this. a. The multiplication operator b. The repetition operator c. The initialization operator d. Nothing-the operator does not support those types of operands.

The repetition operator

Repetition Operator

The repetition operator makes multiple copies of a list and joins them all together. The star symbol is used, when the operand on the left side of the * symbol is a sequence (such as a list) and the operand on the right side is an integer, it becomes a repetition operator.

This is the last index in a list. a. 1 b. 99 c. 0 d. The size of the list minus one

The size of the list minus one

len Function

a built-in function that returns the length of a sequence, such as a list.

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

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

two-dimensional list (nested lists)

a list that has other lists as its elements.

Sequence

an object that holds multiple items of data, stored one after the other. You can perform operations on a sequence to examine and manipulate the items stored in it.

This list method adds an item to the end of an existing list. a. add b. add to c. increase d. append

append

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

by using the min and/or the max functions

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

index

Tuple

is a sequence, very much like a list, but is an immutable sequence, which means that that once a tuple is created, its contents cannot be changed.

slice

is a span of items that are taken from a sequence. A slicing expression selects a range of elements from a sequence.

This function returns the length of a list. a. length b. size c. len d. lengthof

len

Built-in function to convert a tuple to a list

list ( )

This built-in function returns the highest value in a list. a. highest b. max c. greatest d. best of

max

Mutable

means their elements can be changed

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. mylist[0] = 'Labrador' b. mylist.insert(0, 'Labrador') c. mylist. append ( 'Labrador' ) d. mylist.insert('Labrador', 0)

mylist.insert(0, 'Labrador')

This file object method returns a list containing the file's contents. a. to_list b. getlist c. readline d. readlines

readlines

del Statement

removes an element from a specific index, regardless of the item that is stored at that index.

This removes an item at a specific index in a list. a. the remove method b. the delete method c. the del statement d. the kill method

the del statement

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

the remove method is used to remove a specific element in a list. the del method is used to remove any element that is present at a specific index, regardless of what that element is.

Built-in function to convert a list to a tuple

tuple ( )


Related study sets

FORCE AND LAW OF MOTION SECTION 3 REVIEWWW

View Set

Chapter 7 thru Chapter 12_Computer Network Fundamentals

View Set

Florida 2 15 Taxes, Retirement, and other Insurance Concepts

View Set

Depression and Suicidal Thoughts/Behaviors and Practice Q's

View Set

Legal and Social Environment - Finance Exam 1

View Set

Chapter 23 - The United States and The Cold War

View Set

Externalities and market failure

View Set

Chapter 37: Disorders of Gastrointestinal Function #1

View Set