python chapter 7, 8, 9

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

You can store duplicate elements in a set. T or F

False

This operator can be used to find the intersection of two sets. a. I b. & c. - d. ^

&

How do you delete an element from a dictionary?

By using the del statement

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 .

How do you determine the number of elements that are stored in a dictionary?

By using the len function

What is the number of the first index in a dictionary?

Dictionaries are not indexed by number.

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

False

A list can be a dictionary key. 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

Sets are created using curly braces { }.

False

Sets are immutable.

False

The dictionary method popitem does not raise an exception if it is called on an empty dictionary. T or F

False

The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.

False

The following statement creates an empty set: myset = ( ) T or F

False

The isupper method converts a string to all uppercase characters. T or F

False

The keys in a dictionary must be mutable objects. T or F

False

The union of two sets is a set that contains only the elements that appear in both sets.

False

When you call a string's split method, the method divides the string into two substrings. T or F

False

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

False

This operator can be used to find the union of two sets. a. I b. & c. - d. ^

I

What is the difference between the remove and discard methods?

If the specified element to delete is not in the set, the remove method raises a KeyError exception, but the discard method does not raise an exception.

insert(index, item) Method

Inserts an item into the list at the specified index.

What does the get method do if the specified key is not found in the dictionary?

It returns a default value.

What does the items method return?

It returns all a dictionary's keys and their associated values as a sequence of tuples.

What does the keys method return?

It returns all the 'keys' in a dictionary as a sequence of tuples.

What does the values method return?

It returns all the 'values' in the dictionary as a sequence of tuples.

An element in a dictionary has two parts. What are they called?

Key and value

What will be the result of the following code? ages = {'Aaron' : 6, 'Kelly' : 3, 'Abigail' : 1 } value = ages['Brianna']

KeyError

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.

Which part of a dictionary element must be immutable?

The key

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.

What module do you import if you want to pickle objects?

The pickle module

What is the difference between the dictionary methods pop and popitem?

The pop method accepts a key as an argument, returns the value that is associated with that key, and removes that key-value pair from the dictionary. The popitem method returns a randomly selected key-value pair, as a tuple, and removes that key-value pair from the dictionary.

What is object serialization?

The process of converting the object to a stream of bytes that can be saved to a file for later retrieval.

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

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.

appending one string to the end of another string

concatenation

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

del Statement

A ______ is an object that stores a collection of data. Each element in a dictionary has two parts: a key and a value. You use a key to locate a specific value.

dictionary

This set method removes an element but does not raise an exception if the element is not found. a. remove b. discard c. delete d. erase

discard

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

element

The substring argument is a string. The method returns true if the string ends with substring.

endswith(substring)

A list cannot be passed as an argument to a function.

false

Indexing of a string starts at 1 so the index of the first character is 1, the index of the second character is 2, and so forth.

false

The following code will display 'yes + no': mystr = 'yes' yourstr = 'no' mystr += yourstr print(mystr)

false

The following expression is valid: string[i] = 'i'

false

The remove method removes all occurrences of an item from a list.

false

The sort method rearranges the elements of a list so they are in ascending or descending order.

false

The strip() method returns a copy of the string with all the leading whitespace characters removed but does not remove trailing whitespace characters.

false

To add a descriptive label to the X and Y axes of a graph when using the matplotlib package, you need to import the labels module.

false

When accessing each character in a string, such as for copying purposes, you would typically use a while loop.

false

This string method returns the lowest index in the string where a specified substring is found. a. first index of b. locate c. find d. index of

find

The substring argument is a string. The method returns the lowest index in the string where substring is found. If substring is not found, the method returns -1.

find(substring)

The ____ dictionary method returns the value associated with a specified key. If the key is not found, it returns a default value. a. pop( ) b. key( ) c. value( ) d. get( )

get( )

means that once they are created, they cannot be changed

immutable

In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the ________ operator.

in

This operator determines whether one string is contained inside another string. a. contains b. is in c. == d. in

in

You can use the ____ operator to determine whether a key exists in a dictionary. a. & b. in c. ^ d. ?

in

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.

Which method would you use to get all the elements in a dictionary returned as a list of tuples?

items

The ____ method returns all of a dictionary's keys and their associated values as a sequence of tuples. a. keys_values( ) b. values( ) c. items ( ) d. get( )

items ( )

In a dictionary, you use a(n) ________ to locate a specific value.

key

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

len

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

len

Which would you use to get the number of elements in a dictionary?

len

The ____ function returns the number of elements in a dictionary: a. size( ) b. len( ) c. elements( ) d. count( )

len( )

The following function returns the number of elements in a set: a. size( ) b. len( ) c. elements( ) d. count( )

len( )

Built-in function to convert a tuple to a list

list ( )

Returns a copy of the string with all alphabetic letters converted to lowercase. Any character that is already lowercase, or is not an alphabetic letter, is unchanged.

lower( )

This string method returns a copy of the string with all leading whitespace characters removed. a. lstrip b. rstrip c. remove d. strip_leading

lstrip

Returns a copy of the string with all leading whitespace characters removed. Leading whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the beginning of the string.

lstrip( )

The char argument is a string containing a character. Returns a copy of the string with all instances of char that appear at the beginning of the string removed.

lstrip(char)

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

max

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

What function do you call to pickle an object?

pickle.dump

What function do you call to retrieve and unpickle an object?

pickle.load

In Python, object serialization is called ______

pickling

What is the process used to convert an object to a stream of bytes that can be saved in a file?

pickling

Which method would you use to get the value associated with a specific key and remove that key-value pair from the dictionary?

pop

The ____ method returns the value associated with a specified key and removes that key-value pair from the dictionary . a. pop( ) b. random( ) c. popitem( ) d. rand_pop( )

pop( )

The ____ method returns a randomly selected key-value pair from a dictionary. a. pop( ) b. random( ) c. popitem( ) d. rand pop( )

popitem( )

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

readlines

This set method removes an element and raises an exception if the element is not found. a. remove b. discard c. delete d. erase

remove

A ______ contains a collection of unique values and works like a mathematical set.

set

Indexing works with both strings and lists.

true

1. What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.) cities = {'GA' : 'Atlanta', 'NY' : 'Albany', 'CA' : 'San Diego'} if 'CA' in cities: del cities['CA'] cities['CA'] = 'Sacramento' print(cities)

{'CA': 'Sacramento', 'NY': 'Albany', 'GA': 'Atlanta'}

What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.) cities = {'Ga':'Atl','NY':'Alb','CA':'San D'} if 'FL' in cities: del cities['FL'] cities['FL']='Tallah' print(cities)

{'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta'}

When you open a file for the purpose of retrieving a pickled object from it, what file access mode do you use?

'rb'

When you open a file for the purpose of saving a pickled object to it, what file access mode do you use?

'wb'

This operator can be used to find the difference of two sets. a. I b. & c. - d. ^

-

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

0

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

0

Does a set allow you to store duplicate elements?

No

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.

______ a object is the process of converting the object to a stream of bytes that can be saved to a file for later retrieval.

Serializing

sort() Method

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

Which of the following does not apply to sets?

The elements are in pairs.

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

A dictionary can include the same value several times but cannot include the same key several times.

True

A tuple can be a dictionary key. T or F

True

Dictionaries are not sequences. T or F

True

If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.

True

Sets store their elements in an unordered fashion. T or F

True

The difference of set1 and set2 is a set that contains only the elements that appear in set1 but do not appear in set2.

True

The following statement creates an empty dictionary: mydct = { } T or F

True

The issubset() method can be used to determine whether set1 is a subset of set2.

True

The remove method raises an exception if the specified element is not found in the set. T or F

True

The set remove and discard methods behave differently only when a specified item is not found in the set.

True

You would typically use a for loop to iterate over the elements in a set.

True

Are the elements of a set ordered or unordered?

Unordered

How do you create an empty set?

You call the built-in set function.

How can you determine whether a key-value pair exists in a dictionary?

You can use the in operator to test for a specific key.

How can you determine whether a specific element exists in a set?

You can use the in operator to test for the element.

How do you determine the number of elements in a set?

You pass the set as an argument to the len function.

This operator can be used to find the symmetric difference of two sets. a. I b. & c. - d. ^

^

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.

You can add one element to a set with this method. a. append b. add c. update d. merge

add

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

The substring argument is a string. The method returns true if the string starts with substring.

startswith(substring)

This string method returns a copy of the string with all leading and trailing whitespace characters removed. a. clean b. strip c. remove_whitespace d. rstrip

strip

You use ____ to delete an element from a dictionary. a. the remove method b. the erase method c. the delete method d. the del statement

the del statement

If a whole paragraph is included in a single string, the split() method can be used to obtain a list of the sentences in the paragraph.

true

In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead.

true

The index -1 identifies the last character of a string.

true

You can add a group of elements to a set with this method. a. append b. add c. update d. merge

update

What is the correct structure to create a dictionary of months where each month will be accessed by its month number (for example, January is month 1, April is month 4)?

{ 1 : 'January', 2 : 'February', ... 12 : 'December' }

You can use ____ to create an empty dictionary. a. { } b. ( ) c. [ ] d. empty( )

{ }

This string method returns true if a string contains only alphabetic characters and is at least one character in length. a. the isalpha method b. the alpha method c. the alphabetic method d. the isletters method

the isalpha method

This string method returns true if a string contains only numeric digits and is at least one character in length. a. the digit method b. the isdigit method c. the numeric method d. the isnumber method

the isdigit method

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.

If the + operator is used on strings, it produces a string that is a combination of the two strings used as its operands.

true

In order to create graphs using the matplotlib package, you need to import the pyplot module.

true

In slicing, if the end index specifies a position beyond the end of the list, Python will use the length of the list instead.

true

Invalid indexes do not cause slicing expressions to raise an exception.

true

The index -1 identifies the last element in a list.

true

To calculate the average of the numeric values in a list, the first step is to get the total of values in the list.

true

Built-in function to convert a list to a tuple

tuple ( )

Returns a copy of the string with all alphabetic letters converted to uppercase. Any character that is already uppercase, or is not an alphabetic letter, is unchanged.

upper( )

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

Mutable

means their elements can be changed

The old and new arguments are both strings. The method returns a copy of the string with all instances of old replaced by new.

replace(old, new)

Returns a copy of the string with all trailing whitespace characters removed. Trailing whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the end of the string.

rstrip( )

The char argument is a string containing a character. The method returns a copy of the string with all instances of char that appear at the end of the string removed.

rstrip(char)

Returns a copy of the string with all leading and trailing whitespace characters removed.

strip( )

Returns a copy of the string with all instances of char that appear at the beginning and the end of the string removed.

strip(char)

a slice from a string, you get a span of characters from within the string. String slices are also called

substrings

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

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

True

You can use the for loop to iterate over the individual characters in a string. T or F

True

A Tuple does not support what methods?

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

How do you find the length of a string?

Use the len( ) function

two-dimensional list (nested lists)

a list that has other lists as its elements.

What happens if you try to use an invalid index to access a character in a string?

An IndexError Exception occurs

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.

This will happen if you try to use an index that is out of range for a string. a. A ValueError exception will occur. b. An IndexError exception will occur. c. The string 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.

Lists in Python are immutable. T or F

False

What is the index of the first character in a string?

0

If a string has 10 characters, what is the index of the last character?

9

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 is the last index in a string. a. 1 b. 99 c. 0 d. The size of the string minus one

The size of the string minus one

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

True

Once a string is created, it cannot be changed. T or F

True

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

True

The repetition operator ( *) works with strings as well as with lists. T or F

True

Tuples in Python are immutable. T or F

True

The index of the first element in a list is 1, the index of the second element is 2, and so forth.

false

slice

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

Returns true if the string contains only alphabetic letters or digits and is at least one character in length. Returns false otherwise.

isalnum( )

Returns true if the string contains only alphabetic letters and is at least one character in length. Returns false otherwise.

isalpha( )

Returns true if the string contains only numeric digits and is at least one character in length. Returns false otherwise.

isdigit( )

Returns true if all of the alphabetic letters in the string are lowercase, and the string contains at least one alphabetic letter. Returns false otherwise.

islower( )

Returns true if the string contains only whitespace characters and is at least one character in length. Returns false otherwise. (Whitespace characters are spaces, newlines (\n), and tabs (\t).

isspace( )

Returns true if all of the alphabetic letters in the string are uppercase, and the string contains at least one alphabetic letter. Returns false otherwise.

isupper( )


Kaugnay na mga set ng pag-aaral

Crucible Act II Possible Test Questions

View Set

1.2 Given a scenario, install components within the display of a laptop

View Set

Civics & Economics - Midterm Review

View Set

Psychology 1020 Chapter 9 Review (Textbook & Blackboard Quizzes)

View Set

Chapter 15 - Property Management and Leasing

View Set