WorksheetsPython Essentials_FT_Batch 3
Total questions: 20
Worksheet time: 12mins
Assume the following list definition:
a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
print(a[4::-2])
a[:] is a
max(a[2:4] + ['grault'])
print(a[-5:-3])
List a is defined as follows:
a = ['a', 'b', 'c']
Which of the following statements adds 'd' and 'e' to the end of a, so that it then equals ['a', 'b', 'c', 'd', 'e']:
a += 'de'
a += ['d', 'e']
a.append(['d', 'e'])
a[len(a):] = ['d', 'e']
a.extend(['d', 'e'])
names1 = ['Amir', 'Barry', 'Chales', 'Dao']
loc = names1.index("Edward")
print(loc)
-1
0
4
Edward
An exception is thrown
Which of the following are true of Python lists?
These represent the same list:['a', 'b', 'c'] ['c', 'a', 'b']
A given object may appear in a list more than once
All elements in a list must be of the same type
A list may contain any type of object except another list
There is no conceptual limit to the size of a list
You have a list a defined as follows:
a = [1, 2, 7, 8]
Write a Python statement using slice assignment that will fill in the missing values so that a equals [1, 2, 3, 4, 5, 6, 7, 8].
(a)
Same nested list as the previous question:
x = [10, [3.141, 20, [30, 'baz', 2.718]], 'foo']
What expression returns the list ['baz', 2.718]?
(a)
What is the purpose of the print() function in Python?
To display output to the console
To read user input
To create a variable
To define a function
Which symbol is used to create a list in Python?
( )
[ ]
{ }
< >
Which of the following is a valid variable name in Python?
1st_variable
my-variable
myVariable
my variable
How do you start a function definition in Python?
function myFunction():
def myFunction():
create myFunction():
myFunction():
What is the output of the following code? x = 5 print(x * 2)
10
5
25
Error
How do you start writing a for loop in Python?
for each i in x:
for i = 1 to x:
for i in x:
for i > x:
What would be the value of ‘new_score’ after running this code:
score = 44
total = 1
new_score = score * 2
88
44
45
22
What is the output of the following code? print("Hello " * 3)
Hello Hello Hello
Hello 3
Hello Hello Hello 3
Error
Which operator is used to check equality in Python?
==
=
===
!=
What will be the output of this code? x = [1, 2, 3] print(len(x))
3
2
1
Error
What will the following code output? x = "Hello" print(x[1])
H
e
Hello
Error
What sort of error occurs when the program runs but you do not get the correct answer you expected?
Answer Error
Syntax Error
Run-time Error
Logical Error
what does this code display:
counter = 1
while counter <4:
print (counter)
counter = counter + 1
1 2 3 4
1 2 3
0 1 2 3
0 1 2 3 4
Who developed the Python language?
Zim Den
Guido van Rossum
Niene Stom
Wick van Rossum
