Chapter 6 - 12 CheckPoint Review Python

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What will the following code display? var = '$' print(var.upper())

$

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

'wb'

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

0

What will the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'} for k in stuff: print(k)

1 2 3

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

1 3 5 7 9

What three steps must be taken by a program when it uses a file?

1.Open the file. 2.Process the file. 3.Close the file.

What will the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'} print(len(stuff))

3

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

4

f s string has 10 characters, what is the index of the last character?9

9

What is an input file?

A file from which a program reads data. It is called an input file because the program receives input from it.

What is an output file?

A file to which a program writes data. It is called an output file because the program sends output to it.

What is a file's read position? Initially, where is the read position when an input file is opened?

A file's read position marked the location of the next item that will be read from the file. When an input file is opened, its read position is initially set to the first item in the file.

What is a record? What is a field?

A record is a complete set of data that described one item, and a field is a single piece of data within a record.

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

An IndexError exception will occur if you try to use an index that is out the range for a particular string.

Briefly describe what an exception is.

An exception is an error that occurs while a program is running. In most cases, an exception causes a program to abruptly halt.

What is the purpose of closing a file?

Closing a file disconnects the program from the file.

What type of exception does a program raise when it tries to open a nonexistent file?

IOError

What is the difference between the remove and discard methods?

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

Suppose a dictionary named inventory exists. What does the following statement do?del inventory[654]

It deleted the element that has the key 654.

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 of the keys in a dictionary as a sequence of tuples.

What does the values method return?

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

Suppose a dictionary named employee has been created. What does the following statement do? employee['id'] = 54321

It stores the key-value pair 'id' : 54321 in the employee dictionary.

After the following statement executes, what elements will be stored in the myset set?myset = set(25)

It will contain one element: 25

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)

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

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

Key and Value

Does a set allow you to store duplicate elements?

No

What is the purpose of opening a file?

Opening a file creates a connection between the file and the program. It also creates an association between the file and the file object.

What are the two types of file access? What is the difference between these two?

Sequential and Direct Access. When you work with a sequential access file, you access data from the beginning of the file to the end of the file. When you work with a direct access file, you can jump directly to any piece of data in the file without reading the data that comes before it.

Look at the following code: set1 = set([1, 2, 3, 4]) set2 = set([2, 3]) Which of the sets is a subset of the other?Which if the sets is a superset of the other?

Set2 is a subset of set1, and set1 is a superset of set2.

In general, what are the two types of files? What is the difference between these two types of files?

Text and Binary. A text file contains data that has been encoded as text using a scheme such as ASCII. Even if the file contains numbers, those numbers are stored in the file as a series of characters. As a result, the file may be opened and viewed in a text editor such as Notepad. A binary file contains data that has not been converted to text. As a consequence, you cannot view the contents of a binary file with a text editor.

Which part of a dictionary element must be immutable?

The Key

If a file already exists, what happens to it if you try to open it as an output file (using the 'w' mode)?

The file's contents are erased.

When writing a program that performs an operation on a file, what two file-associated names do you have to work with in your code?

The file's name on the disk and the name of a variable that references a file object.

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

The list contains 4 rows and 2 columns.

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 removed that key-value pair from the dictionary. The popitem method returns a randomly selected key-value pair, as a tuple, and removed that key-value pair from the dictionary.

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

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

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.

If an exception is raised and the program does not handle it with a try/except statement, what happens?

The program halts.

What does it mean when the readline method returns an empty string?

The readline method returns an empty string (' ') when it has attempted to read beyond the end of a file.

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 removed an element containing a specific value. The del statement removed an element at a specific position.

What is wrong with the following code? animal = 'Tiger' animal[0] = 'L'

The second statement attempts to assign a value to an invalid character in the string. Strings are immutable, however , so the expression animal[0] cannot appear on the left side of an assignment operator.

After the following statement executes, what elements will be stored in the myset set?myset = set('Jupiter')

The set will contain these elements (in no particular order): 'J', 'u', 'p', 'i', 't', 'e', and 'r'.

After the following statement executes, what elements will be stored in the myset set?myset = set('www xxx yyy zzz')

The set will contain these elements (in no particular order): 'w', ' ', 'x', 'y', and 'z'.

After the following statement executes, what elements will be stored in the myset set?myset = set(['www', 'xxx', 'yyy', 'zzz'])

The set will contain these elements (in no particular order): 'www', 'xxx', 'yyy', and 'zzz'.

After the following statement executes, what elements will be stored in the myset set?myset = set([1, 2, 2, 3, 4, 4, 4])

The set will contain these elements (in no particular order): 1, 2, 3, and 4

After the following statement executes, what elements will be stored in the myset set?myset = set([10, 9, 8]) myset.update('abc')

The set will contain these elements (in no particular order): 10, 9, 8, 'a', 'b', 'c'.

After the following statement executes, what elements will be stored in the myset set? myset = set([10, 9, 8]) myset.update([1, 2, 3])

The set will contain these elements (in no particular order): 10, 9, 8, 1, 2, and 3.

Suppose 'start' : 1472 is an element in a dictionary. What is the key? What is the value?

The string 'start' is the Key, and the integer 1472 is the Value.

Give two reasons why tuples exist

There 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 (accidently or otherwise) by any code in your program. -There are certain operations in Python that require the use of a tuple.

Are the elements of a set ordered or unordered?

Unordered

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

Use the built-in len function

How do you find the length of a string?

Use the built-in len function.

What type of exception does a program raise when it uses the float function to convert a non-numeric string to a number?

ValueError

How do you create an empty set?

You call the built-in set function.

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

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

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.

Describe the way that you use a temporary file in a program that modified a record in a sequential access file.

You copy all the original file's records to the temporary file, but when you get to the record that is to be modified, you do not write its old contents to the temporary file. Instead you write its new, modified values to the temporary file. Then, you finish copying the remaining records from the original file to the temporary file.

Describe the way that you use a temporary file in a program that deletes a record from a sequential file.

You copy all the original file's records to the temporary file, except for the record that is to be deleted. The temporary file then takes the place of the original file. You delete the original file and rename the temporary file, giving it the name the original file had on the computer's disk.

In what mode do you open a file if you want to write date to it, but you do not want to erase the file's existing content? When you write data to such a file, to what part of the file is the data written?

You open the file in append mode. When you write data to a file in append mode, the data is written to the end of the file's existing contents.

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

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

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 others?a)names[0] = 'Wendy' b)names.append('Wendy')

You would use statement b. This is because element 0 does not yet exist.

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

[0, 1, 2]

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

[1, 2, 3, 4, 5]

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]

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]

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

[1, 2, 99, 4, 5]

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

[10, 10, 10, 10, 10]

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)

[2, 3, 4, 5]

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[-3:] print(my_list)

[3, 4, 5]

What is the output of the following code? ch = 'a' ch2 = ch.upper() print(ch, ch2)

a A

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

a)The index method searches for an item in the list and returns the index of the first element containing that item. b)Theinsertmethodinsertsaniteminto the list at the specified index. c)The sort method sorts the items in the list to appear in ascending order. d)The reverse method reverses the order of the items in the list.

hat will the following code display? mystring = 'abcdefg' print(mystring[ :3])

abc

What will the following code display? mystring = 'abcdefg' print(mystring[ : ])

abcdefg

Write a loop that asks the user, "Do you want to repeat that program or quit? (R/Q)". The loop should repeat until the user has entered an R or Q (either upper or lowercase).

again = input('Do you want to repeat ' + 'the program or quit? (R/Q) ') while again.upper() != 'R' and again.upper() != 'Q': again = input('Do you want to repeat ' + 'the program or quit? (R/Q) ')

What will the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 :'ccc'} print(stuff[3])

ccc

What will the following code display? mystring = 'abcdefg' print(mystring[2:5])

cde

What will the following code display? mystring = 'abcdefg' print(mystring[3: ])

defg

Write a loop that counts the number of uppercase characters that appear in the string referenced by the variable 'mystring'.

for letter in mystring: if letter.isupper(): count += 1

Assume the variable 'name' references a string. Write a For loop that prints each character in the string.

for letter in name: print(letter)

Write a set of nested loops that display the contents of the numbers list shown in Checkpoint question 7.19 numbers = [[1, 2], [10, 20], [100, 200], [1000, 2000]]

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

Write code using the 'in' operator that determines wether 'd' is in mystring.

if 'd' in mystring: print('Yes, it is there.')

Write an 'if' statement that displays "Digit" if the string referenced by the variable 'ch' contains a numberic digit. Otherwise, it should display "No digit".

if ch.isdigit(): print('Digit') else: print('No digit')

Revise the program that you wrote for Checkpoint 6.14 to use the for loop instead of the while loop.

infile = open('numbers.txt', 'r') for line in infile: print(line) infile.close()

Assume the file data.txt exists and contains several lines of text. Write a short program using the while loop that displays each line in the file.

infile = open('numbers.txt', 'r') line = infile.readline() while line != ' ': print(line) line = infile.readline() infile.close()

Assume the variable 'big' references a string. Write a statement that converts the string it references to lowercase and assigns the converted string to the variable 'little'.

little = big.lower()

Assume the following statement appears in a program. days = 'Monday Tuesday Wednesday' Write a statement that splits the string, creating the following list: ['Monday', 'Tuesday', 'Wednesday']

my_list = days.split()

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

my_list = list(my_tuple)

Assume the following statement appears in a program: values = 'one$two$three$four' Write a statement that splits the string, creating the following list: ['one', 'two', 'three', 'four']

my_list = values.split('$')

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

my_tuple = tuple(my_list)

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

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

Write a short program that uses a for loop to write the numbers 1 through 10 to a file.

outfile = open('numbers.txt', 'w')for num in range(1, 11): outfile.write(str(num) + '\n')outfile.close()

After the following code executes, what elements will be members of set3?set1 = set(['a', 'b', 'c']) set2 = set(['b', 'c', 'd']) set3 = set1.symmetric_difference(set2)

{'a', 'd'}

After the following code executes, what elements will be members of set3?set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.difference(set2)

{1, 2}

After the following code executes, what elements will be members of set3? set1 = set([10, 20, 30]) set2 = set([100, 200, 300]) set3 = set1.union(set2)

{10, 20, 30, 100, 200, 300}

After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.intersection(set2)

{3, 4}

After the following code executes, what elements will be members of set3?set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set2.difference(set1)

{5, 6}


संबंधित स्टडी सेट्स

Sécurité des Systèmes d'Information ( SSI )

View Set

english 11a - unit 4: fight for your right

View Set

Online Health Test Physical Health

View Set

What Is the Story of Hello Kitty questions

View Set

Network Defense and Countermeasures Ch. 9

View Set