Python Programming Questions & Answers focuses on “Lists”.
Q 1. What will be the output of the following Python code?
def f(i, values = []):
values.append(i)
return values
f(1)
f(2)
v = f(3)
print(v)
A. [1] [2] [3]
B. [1] [1, 2] [1, 2, 3]
C. [1, 2, 3]
D. 1 2 3
Show Answer
Answer:-C. [1, 2, 3]Explanation
Execute in the shell to verifyQ 2. What will be the output of the following Python code?
names1 = ['Amir', 'Bala', 'Chales']
if 'amir' in names1:
print(1)
else:
print(2)
A. 1
B. 2
C. Error
D. None
Show Answer
Answer:-B. 2Explanation
Execute in the shell to verify.Q 3. What will be the output of the following Python code?
names1 = ['Amir', 'Bala', 'Charlie']
names2 = [name.lower() for name in names1]
print(names2[2][0])
A. None
B. a
C. b
D. c
Show Answer
Answer:-D. cExplanation
List Comprehension are a shorthand for creating new lists.Q 4. What will be the output of the following Python code?
numbers = [1, 2, 3, 4]
numbers.append([5,6,7,8])
print(len(numbers))
A. 4
B. 5
C. 8
D. 12
Show Answer
Answer:-B. 5Explanation
A list is passed in append so the length is 5.Q 5. To which of the following the “in” operator can be used to check if an item is in it?
A. Lists
B. Dictionary
C. Set
D. All of the mentioned
Show Answer
Answer:-D. All of the mentionedExplanation
In can be used in all data structures.Q 6. What will be the output of the following Python code?
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print(len(list1 + list2))
A. 2
B. 4
C. 5
D. 8
Show Answer
Answer:-D. 8Explanation
+ appends all the elements individually into a new list.Q 7. What will be the output of the following Python code?
def addItem(listParam):
listParam += [1]
mylist = [1, 2, 3, 4]
addItem(mylist)
print(len(mylist))
A. 1
B. 4
C. 5
D. 8
Show Answer
Answer:-C. 5Explanation
+ will append the element to the list.Q 8. What will be the output of the following Python code?
def increment_items(L, increment):
i = 0
while i < len(L):
L[i] = L[i] + increment
i = i + 1
values = [1, 2, 3]
print(increment_items(values, 2))
print(values)
A. None
[3, 4, 5]
B. None
[1, 2, 3]
C. [3, 4, 5]
[1, 2, 3]
D. [3, 4, 5]
None
Show Answer
Answer:-A. None [3, 4, 5]Explanation
Execute in the shell to verify.Q 9. What will be the output of the following Python code?
def example(L):
''' (list) -> list
'''
i = 0
result = []
while i < len(L):
result.append(L[i])
i = i + 3
return result
A. Return a list containing every third item from L starting at index 0
B. Return an empty list
C. Return a list containing every third index from L starting at index 0
D. Return a list containing the items from L starting from index 0, omitting every third item
Show Answer
Answer:-A. Return a list containing every third item from L starting at index 0Explanation
Run the code to get a better understanding with many arguments.Q 10. What will be the output of the following Python code?
veggies = ['carrot', 'broccoli', 'potato', 'asparagus']
veggies.insert(veggies.index('broccoli'), 'celery')
print(veggies)
A. [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’]
B. [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’]
C. [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’]
D. [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]
Leave a Reply