Final Flashcards
How would you create a blank list in Python?
x = [ ]
What would the following code display?
psycho = [22,23,24,98,100,101]
numbers[-4] = 99
print(numbers)
error numbers not defined
if psycho instead of numbers the output would be
[22,23,99,24,98,100,101]
What would the following code display?
numbers = [1,2,3,4,5]
my_list = numbers[1:3]
print(my_list)
[2, 3]
Write a program that opens an output file with the filename my_name.txt, writes your name to
the file and then closes the file.
def main(): my_file = open('my_name.txt', 'w') my_file.write("Lillian") my_file.close() main()
A file that data is written to, is known as a(n)
a. input file
b. output file
c. sequential access file
d. binary file
b. output file
This function returns the length of a list.
a. highest
b. length
c. length of
d. len
d. len
Which of the following is a count controlled loop?
a. for
b. foreach
c. while
d. erst
a. for
A group of statements that exist within a program for the purpose of performing a specific task is a(n) __________.
a. block
b. parameter
c. function
d. expression
c. function
Each repetition of a loop is known as a (n) __________.
a. cycle
b. revolution
c. orbit
d. iteration
d. iteration
he += operator is an example of a(n) __________ operator.
a. relational
b. augmented assignment
c. complex
d. reverse
b. augmented assignment
A variable that is used to mark the end of a sequence of values is known as a (n)
a. accumulator
b. counter
c. terminator
d. sentinel
d. sentinel
The first line of a function definition is known as the \_\_\_\_\_\_\_\_\_\_. a. body b. introduction c. initialization d. header
d. header
A __________ is a variable that is created inside a function.
a. global variable
b. local variable
c. hidden variable
d. none of the above, you cannot create a variable in a function
b. local variable
A __________ is a piece of data that is sent into a function.
a. argument
b. parameter
c. header
d. packet
a. argument
Briefly describe an exception
An error that causes the program to stop
If an exception is raised and the program does not handle it with a try/except statement, what
happens?
An error message is displayed and the program crashes
What is a record? What is a field?
A record is a set of data based on one topic. A field is a single piece of data within a record.
What will happen with the following code:
try:
x = float(‘abcdef’)
print(‘The conversion is complete.’)
except:
print(‘This code caused an error.’)
else:
print(“This just happened.”)
finally:
print(‘An error may have happened’.)
try: except: #happens with an exception else: #this happens without an exception finally: #This always happens
answer:
This code caused an error.
An error may have happened.
Write a while loop that will print the sum of the numbers from 1 to 100.
while x <= 100:
total += x
x += 1
print (total)
Write a function that will take 2 different numbers as parameters. The function will return the largest of the numbers.
def largest (x,y): if x > y: return x else: return y