Computer Science
Predict the output.
b = [[9, 6], [4, 5], [7, 7]]
x = b[:2]
x.append(10)
print(x)
Answer
[[9, 6], [4, 5], 10]
Working
b = [[9, 6], [4, 5], [7, 7]]
— This line initializes a listb
containing three sublists, each containing two elements.x = b[:2]
— This line creates a new listx
by slicing the listb
from index 0 to index 1. So,x
will contain the first two sublists ofb
. At this point,x
will be[[9, 6], [4, 5]]
.x.append(10)
— This line appends the integer 10 to the end of the listx
.x
now becomes[[9, 6], [4, 5], 10]
.print(x)
— This line prints the listx
.
Related Questions
Predict the output :
L1= [x ** 2 for x in range(10) if x % 3 == 0] L2 = L1 L1.append(len(L1)) print(L1) print(L2) L2.remove(len(L2) - 1) print(L1)
Predict the output :
def even(n): return n % 2 == 0 list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] ev = [n for n in list1 if n % 2 == 0] evp = [n for n in list1 if even(n)] print(evp)
Predict the output.
b = [[9, 6], [4, 5], [7, 7]] x = b[:2] x[1].append(10) print(x)
Find the Error. Consider the following code, which runs correctly at times but gives error at other times. Find the error and its reason.
Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88] Lst2 = [] print("List1 originally is: ", Lst1) ch = int(input("Enter 1/2/3 and \ predict which operation was performed?")) if ch == 1: Lst1.append(100) Lst2.append(100) elif ch == 2: print(Lst1.index(100)) print(Lst2.index(100)) elif ch == 3: print(Lst1.pop()) print(Lst2.pop())