NEW
Font size
WorksheetsPython Unit 6 - Data and Sorting
Total questions: 33
Worksheet time: 17mins
Python function that connects to a file in order to access it in any way.
open
read
write
close
Python method that is used to put data into a file.
open
read
write
close
Python method that is used to get data from an existing file.
open
read
write
close
Python method that is used to make sure everything has been written to the file and releases the file for later use or use by another program.
open
read
write
close
To add data to the END of a list.
append
'\n'
strip
str
Python function to convert numeric values to a string.
append
'\n'
strip
str
Used to force a new line when writing to a file, indicates that the end of a line or record.
append
'\n'
strip
str
Removes whitespace (nonprintable) characters, tabs, line returns from a string.
append
'\n'
strip
str
A collection of data or information created using brackets [ ] that is one-dimensional (it only has length) and it's values can be added, changed, or deleted.
list
tuple
table
string
A collection of data or information created using parentheses ( ) that is one-dimensional (it only has length) and it's values cannot be added, changed, or deleted.
list
tuple
table
string
A “list of lists” comprised of multiple rows of lists. Items are accessed using rows & positions
list
tuple
table
string
You have a tuple defined as:
yummy = ("Krispie", "Kreme", "Donut")
After noticing it was misspelled, you entered this instruction:
yummy[0] = "Krispy"
Will it correct and replace the 1st item in the tuple?
Yes
No, it should be - yummy[1] = "Krispy"
No, that would add "Krispy" in front of "Krispie"
It would work except for that immutable thingy.
Method of identifying items in a list using numbers to represent the position of an item in list.
Indexing
Refactoring
Remixing
Bubble Sort
Algorithm to progressively arrange lists by comparing & swapping adjacent list items one at a time to put list in desired order
Indexing
Refactoring
Remixing
Bubble Sort
Rewriting a program to implement changes to simplify the code, improve efficiency, readability, or durability.
Indexing
Refactoring
Remixing
Bubble Sort
Rewriting a program to implement changes to the way a program operates including user input, output, user displays, and what the program does.
Indexing
Refactoring
Remixing
Bubble Sort
You have a program where a menu of choices is displayed and users enter a number to select what they want the program to do.
Which function in Python would be easiest to write and result in the most readable code and less typing for the programmer?
Use an if: and then elif: statements
Use combinations of if: and else: statements
Use the switch function
if statements with multiple "or" booleans ofr comparing values.
Before executing a function, your program needs to know how many items are in a list named - party_guests
Which is the most efficient way to do this?
len(party_guests)
count(party_guests)
Create a function to count the items in a list and call it.
use a for: loop with an index to count them up.
You're writing a program to save a list of your favorite movies to a file. When updating your list, you don't want the program to write over it or crash if you don't have your USB drive in the computer.
Which method should you use to avoid do this when your program starts up?
Use the isfile method.
Write a try: error handler to check for it.
Create a function to scan the usb for it.
You can't save Python data files to a USB.
You've written a try: error handler routine and want it to print: "On to the next task." anytime the try: is executed whether or not an exception occurs.
Which statement would be added to your error handler to do this?
finally:
always:
with:
close:
Your program is going to attempt to open a file and read the contents.
You've written a try: error handler routine and want to make sure the file will automatically be released or closed without having to add a separate close statement to the error handler.
Which statement would be added to your error handler to do this?
finally
release:
with
close
You've written the following code in order to save data to an file. What should be placed in location R of your code?
with
w
write
except
You've written the following code in order to save data to an file. What should be placed in location S of your code?
with
w
write
except
You've written the following code in order to save data to an file. What should be placed in location T of your code?
with
w
write
except
You've written the following code in order to save data to an file. What should be placed in location U of your code?
with
w
write
except
You have written the following piece of code but would like to add an error handler in case there is a file not found exception. Which segment of code should you use?
You have the following code:
f = open(‘mylist.txt’)
f.read.all()
Which exception will be raised?
AttributeError
SyntaxError
SystemError
EOFError
Your file, the_poem, contains the code shown here. Select the line of code that should be added to this code.
thewords = poem.readlines()
thewords = poem.load()
thewords = poem.read()
thewords = poem.readline()
You want to write a birthday message to a file called my_message that will be in this format:
Happy Birthday!
Which code segment should you use?
f = open('my_message','r')
f.write('Happy Birthday!')
f.close()
f = open('my_message','w')
f.write('Happy Birthday!')
f.close()
f = open('my_message','b')
f.write('Happy Birthday!')
f.close()
f = open('my_message')
f.write('Happy Birthday!')
f.close()
You have a file named game_scores.txt with the following data:
"Bob", 1000, 5000, 7500
"Bill", 9000, 6000, 3000
Your code should read all the contents of the file. Which code segment should be used?
with open("game_scores.txt","r") f:
file_data = f.read()
with open("game_scores.txt") as f:
file_data = f.read()
with open("game_scores.txt","w") as f:
file_data = f.read()
with open("game_scores,txt", “b”) as f:
file_data = f.read()
You are writing a program which will use a file named "free_throws.txt" to track you basketball team's free throws made or missed.
In your program, you use this line of code:
shots = open("free_thows.txt")
Since you didn't specify the file mode, what mode will Python use as a default in the program?
read 'r'
write 'w'
read and write 'w+'
read a binary file 'b'
A program has the code shown here. What will be displayed?
3500
9000
6000
500
You have the following code of the SEC games left to play in 2020. The first name in each row of your table is the team, followed by their opponents for the next 4 weeks.
What would be the code to print Florida's opponent for 3rd game in this list?
print(sec_games[2][3])
print(sec_games[3,4])
print(sec_games[3][4])
print(sec_games(2,3))
None of the selections are correct.
