Output of python program | Set 12(Lists and Tuples)
Last Updated :
11 Mar, 2022
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) 38
Ans: (c)
Explanation: In the print(), indexing is used. L1[0] denotes [1, [2, 3], 4], L1[0][1] denotes [2, 3],
L1[0][1][1] = 3 and L1[2] = 8. Thus, the two integers are added, 3 + 8 = 11 and output comes as 11.
2) What is the output of the following program?
PYTHON
L1 = [1, 1.33, 'GFG', 0, 'NO', None, 'G', True]
val1, val2 = 0, ''
for x in L1:
if(type(x) == int or type(x) == float):
val1 += x
else if(type(x) == str):
val2 += x
else:
break
print(val1, val2)
a) 2 GFGNO
b) 2.33 GFGNOG
c) 2.33 GFGNONoneGTrue
d) 2.33 GFGNO
Ans: (d)
Explanation: val1 will only have integer and floating values val1 = 1 + 1.33 + 0 = 2.33 and val2 will have string
values val2 ='GFG' + 'NO' = 'GFGNO'. String 'G' will not be part of val2 as the for loop will break at None,
thus 'G' will not be added to val2.
3) What is the output of the following program?
Python3
L1 = [1, 2, 3, 4]
L2 = L1
L3 = L1.copy()
L4 = L3
L1[0] = [5]
print(L1, L2, L3, L4)
a) [5, 2, 3, 4] [5, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
b) [[5], 2, 3, 4] [[5], 2, 3, 4] [[5], 2, 3, 4] [1, 2, 3, 4]
c) [5, 2, 3, 4] [5, 2, 3, 4] [5, 2, 3, 4] [1, 2, 3, 4]
d) [[5], 2, 3, 4] [[5], 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
Ans: (d)
Explanation: L2 is the reference pointing to the same object as L1,
while L3 and L4 are single recursive Copy(Shallow Copy) of List L1.
L1[0] = [5], implies that at index 0, list [5] will be present and not integer value 5.
4) What is the output of the following program?
PYTHON
import sys
L1 = tuple()
print(sys.getsizeof(L1), end = " ")
L1 = (1, 2)
print(sys.getsizeof(L1), end = " ")
L1 = (1, 3, (4, 5))
print(sys.getsizeof(L1), end = " ")
L1 = (1, 2, 3, 4, 5, [3, 4], 'p', '8', 9.777, (1, 3))
print(sys.getsizeof(L1))
a) 0 2 3 10
b) 32 34 35 42
c) 48 64 72 128
d) 48 144 192 480
Ans: (c)
Explanation: An Empty Tuple has 48 Bytes as Overhead size and each additional element requires 8 Bytes.
(1, 2) Size: 48 + 2 * 8 = 64
(1, 3, (4, 5)) Size: 48 + 3 * 8 = 72
(1, 2, 3, 4, 5, [3, 4], 'p', '8', 9.777, (1, 3)) Size: 48 + 10 * 8 = 128
5) What is the output of the following program?
PYTHON
T1 = (1)
T2 = (3, 4)
T1 += 5
print(T1)
print(T1 + T2)
a) TypeError
b) (1, 5, 3, 4)
c) 1 TypeError
d) 6 TypeError
Ans: (d)
Explanation: T1 is an integer while T2 is tuple. Thus T1 will become 1 + 5 = 6. But an integer and tuple cannot be added, it will throw TypeError.
Similar Reads
Output of Python Program | Set 3 Difficulty level : Intermediate Predict the output of following Python Programs. Program 1: Python3 class Geeks: def __init__(self, id): self.id = id manager = Geeks(100) manager.__dict__['life'] = 49 print (manager.life + len(manager.__dict__)) Output:51 Explanation : In the above program we are cr
2 min read
Output of Python Program | Set 4 Difficulty level : Intermediate Predict the output of the following Python Programs. Program 1: Python nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv'] print nameList[1][-1] Output: k Explanation: The index position -1 represents either the last element in a list or the last character in a String. In
2 min read
Output of Python program | Set 5 Predict the output of the following programs: Program 1: Python def gfgFunction(): "Geeksforgeeks is cool website for boosting up technical skills" return 1 print (gfgFunction.__doc__[17:21]) Output:coolExplanation: There is a docstring defined for this method, by putting a string
3 min read
Output of Python program | Set 6 (Lists) Prerequisite - Lists in Python Predict the output of the following Python programs. These question set will make you conversant with List Concepts in Python programming language. Program 1 Python list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]:
3 min read
Output of Python programs | Set 7 Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5
3 min read
Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list,
3 min read
Output of Python programs | Set 9 (Dictionary) Prerequisite: Dictionary 1) What is the output of the following program? Python dictionary = {'GFG' : 'geeksforgeeks.org', 'google' : 'google.com', 'facebook' : 'facebook.com' } del dictionary['google']; for key, values in dictionary.items(): print(key) dictionary.clear(); for key, values in diction
3 min read
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