Output of python program | Set 11(Lists)
Last Updated :
22 Apr, 2022
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) Explanation: [x for x in[data] returns a new list copying the values in the list data and the outer for statement prints the newly created list 3 times. 2) What is the output of the following program?
Python
data = [x for x in range(5)]
temp = [x for x in range(7) if x in data and x%2==0]
print(temp)
a) [0, 2, 4, 6] b) [0, 2, 4] c) [0, 1, 2, 3, 4, 5] d) Runtime error Ans. (b) Explanation: The is statement checks whether the value lies in list data and if it does whether it's divisible by 2. It does so for x in (0, 7). 3) What is the output of the following program?
Python
temp = ['Geeks', 'for', 'Geeks']
arr = [i[0].upper() for i in temp]
print(arr)
a) ['G', 'F', 'G'] b) ['GEEKS'] c) ['GEEKS', 'FOR', 'GEEKS'] d) Compilation error Ans. (a) Explanation: The variable i is used to iterate over each element in list temp. i[0] represent the character at 0th index of i and .upper() function is used to capitalize the character present at i[0]. 4) What is the output of the following program?
Python
temp = 'Geeks 22536 for 445 Geeks'
data = [x for x in (int(x) for x in temp if x.isdigit()) if x%2 == 0]
print(data)
a) [2, 2, 6, 4, 4] b) Compilation error c) Runtime error d) ['2', '2', '5', '3', '6', '4', '4', '5'] Ans. (a) Explanation: This is an example of nested list comprehension. The inner list created contains a list of integer in temp. The outer list only procures those x which are a multiple of 2. 5) What is the output of the following program?
Python
data = [x for x in (x for x in 'Geeks 22966 for Geeks' if x.isdigit()) if
(x in ([x for x in range(20)]))]
print(data)
a) [2, 2, 9, 6, 6] b) [] c) Compilation error d) Runtime error Ans. (b) Explanation: Since here x have not been converted to int, the condition in the if statement fails and therefore, the list remains empty.
Similar Reads
Output of python program | Set 2 Difficulty level : IntermediatePredict the output of following Python Programs. Program 1: Python3 class Acc: def __init__(self, id): self.id = id id = 555 acc = Acc(111) print (acc.id) Output: 111 Explanation: Instantiation of the class "Acc" automatically calls the method __init__ and passes the o
2 min read
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