Output of Python Programs | Set 18 (List and Tuples)
Last Updated :
06 Sep, 2024
1) What is the output of the following program?
PYTHON
L = list('123456')
L[0] = L[5] = 0
L[3] = L[-2]
print(L)
a) [0, '2', '3', '4', '5', 0]
b) ['6', '2', '3', '5', '5', '6']
c) ['0', '2', '3', '5', '5', '0']
d) [0, '2', '3', '5', '5', 0]
Ans. (d)
Explanation: L[0] is '1' and L[5] is '6', both of these elements will be replaced by 0 in the List. L[3], which is 4 will be replaced L[-2] i.e. 5.
2) What is the output of the following program?
PYTHON3
T = 'geeks'
a, b, c, d, e = T
b = c = '*'
T = (a, b, c, d, e)
print(T)
a) ('g', '*', '*', 'k', 's')
b) ('g', 'e', 'e', 'k', 's')
c) ('geeks', '*', '*')
d) KeyError
Ans. (a)
Explanation: A tuple is created as T = ('g', 'e', 'e', 'k', 's'), then it is unpacked into a, b, c, d and e, mapping from 'g' to a and 's' to e. b and c which are both 'e' are equal to '*' and then the existing tuple is replaced by packing a, b, c, d and e into a tuple T.
3) What is the value of L at the end of execution of the following program?
PYTHON3
L = [2e-04, 'a', False, 87]
T = (6.22, 'boy', True, 554)
for i in range(len(L)):
if L[i]:
L[i] = L[i] + T[i]
else:
T[i] = L[i] + T[i]
break
a) [6.222e-04, 'aboy', True, 641]
b) [6.2202, 'aboy', 1, 641]
c) TypeError
d) [6.2202, 'aboy', False, 87]
Ans. (c)
Explanation: The for loop will run for i = 0 to i = 3, i.e. 4 times(len(L) = 4). 2e-04 is same as 0.0002, thus L[i] = 6.22 + 0.0002 = 6.2202. String addition will result in concatenation, 'a' + 'boy' = 'aboy'. False + True is True, it'll return the integer value of 1. As tuples are immutable, the code will end with TypeError, but elements of L will be updated.
4) What is the output of the following program?
PYTHON
T = (2e-04, True, False, 8, 1.001, True)
val = 0
for x in T:
val += int(x)
print(val)
a) 12
b) 11
c) 11.001199999999999
d) TypeError
Ans. (b)
Explanation: Integer value of 2e-04(0.0002) is 0, True holds a value 1 and False a 0, integer value of 1.001 is 1. Thus total 0 + 1 + 0 + 8 + 1 + 1 = 11.
5) Which of the options below could possibly be the output of the following program?
PYTHON
L = [3, 1, 2, 4]
T = ('A', 'b', 'c', 'd')
L.sort()
counter = 0
for x in T:
L[counter] += int(x)
counter += 1
break
print(L)
a) [66, 97, 99, 101]
b) [66, 68, 70, 72]
c) [66, 67, 68, 69]
d) ValueError
Ans. (d)
Explanation: After sort(L), L will be = [1, 2, 3, 4]. Counter = 0, L[0] i.e. 1, x = 'A', but Type Conversion of char 'A' to integer will throw error and the value cannot be stored in L[0], thus a ValueError.
Similar Reads
Output of Python programs | Set 10 (Exception Handling) Pre-requisite: Exception Handling in PythonNote: All the programs run on python version 3 and above. 1) What is the output of the following program?Python data = 50 try: data = data/0 except ZeroDivisionError: print('Cannot divide by 0 ', end = '') else: print('Division successful ', end = '') try:
3 min read
Output of python program | Set 11(Lists) Pre-requisite: List in python 1) What is the output of the following program? Python data = [2, 3, 9] temp = [[x for x in[data]] for x in range(3)] print (temp) a) [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]] b) [[2, 3, 9], [2, 3, 9], [2, 3, 9]] c) [[[2, 3, 9]], [[2, 3, 9]]] d) None of these Ans. (a) Exp
3 min read
Output of python program | Set 12(Lists and Tuples) Prerequisite: List and Tuples Note: Output of all these programs is tested on Python3 1) What is the output of the following program? PYTHON L1 = [] L1.append([1, [2, 3], 4]) L1.extend([7, 8, 9]) print(L1[0][1][1] + L1[2]) a) Type Error: can only concatenate list (not "int") to list b) 12 c) 11 d) 3
3 min read
Output of python program | Set 13(Lists and Tuples) Prerequisite: Lists and Tuples1) What is the output of the following program? PYTHON List = [True, 50, 10] List.insert(2, 5) print(List, "Sum is: ", sum(List)) a) [True, 50, 10, 5] Sum is: 66 b) [True, 50, 5, 10] Sum is: 65 c) TypeError: unsupported operand type(s) for +: 'int' and 'str' d) [True, 5
3 min read
Output of python program | Set 14 (Dictionary) Prerequisite: Dictionary Note: Output of all these programs is tested on Python31) What is the output of the following program? PYTHON3 D = dict() for x in enumerate(range(2)): D[x[0]] = x[1] D[x[1]+7] = x[0] print(D) a) KeyError b) {0: 1, 7: 0, 1: 1, 8: 0} c) {0: 0, 7: 0, 1: 1, 8: 1} d) {1: 1, 7: 2
3 min read
Output of python program | Set 15 (Modules) Prerequisite: Regular Expressions Note: Output of all these programs is tested on Python3 1) Which of the options below could possibly be the output of the following program? PYTHON from random import randrange L = list() for x in range(5): L.append(randrange(0, 100, 2)-10) # Choose which of outputs
3 min read
Output of Python program | Set 15 (Loops) Prerequisite - Loops in Python Predict the output of the following Python programs. 1) What is the output of the following program? Python x = ['ab', 'cd'] for i in x: i.upper() print(x) Output:['ab', 'cd']Explanation: The function upper() does not modify a string in place, but it returns a new stri
2 min read
Output of Python program | Set 16 (Threads) 1) What is the output of the following program? Python import threading barrier = threading.Barrier(4) class thread(threading.Thread): def __init__(self, thread_ID, thread_name): threading.Thread.__init__(self) self.thread_ID = thread_ID self.thread_name = thread_name def run(self): print("Thre
3 min read
Output of Python program | Set 17 Prerequisite - Tuples and Dictionaryin Python Predict the output of the following Python programs. 1.What is the output of the following program? Python numberGames = {} numberGames[(1,2,4)] = 8 numberGames[(4,2,1)] = 10 numberGames[(1,2)] = 12 sum = 0 for k in numberGames: sum += numberGames[k] pri
2 min read
Output of Python Programs | Set 18 (List and Tuples) 1) What is the output of the following program? PYTHON L = list('123456') L[0] = L[5] = 0 L[3] = L[-2] print(L) a) [0, '2', '3', '4', '5', 0] b) ['6', '2', '3', '5', '5', '6'] c) ['0', '2', '3', '5', '5', '0'] d) [0, '2', '3', '5', '5', 0] Ans. (d) Explanation: L[0] is '1' and L[5] is '6', both of t
3 min read