CBSE Class 12 Computer Science Question 46 of 68

Data Structures - I : Linear Lists — Question 2

Back to all questions
2
Question

Question 2

Change the above code so that it works as stated in previous question.

Answer
NumLst = [2, 5, 1, 7, 3, 6, 8, 9]
SqLst = []
for num in NumLst:
    SqLst.append(num * 2)
print(SqLst)
Output
[4, 10, 2, 14, 6, 12, 16, 18]
Explanation
  1. NumLst = [2, 5, 1, 7, 3, 6, 8, 9] — This line creates a list called NumLst containing the given numbers [2, 5, 1, 7, 3, 6, 8, 9].
  2. SqLst = [] — This line initializes an empty list called SqLst.
  3. for num in NumLst: — This line starts a for loop that iterates over each element num in the NumLst list.
  4. SqLst.append(num * 2) — Inside the loop, each num is multiplied by 2, and the result is appended to the SqLst list.
  5. print(SqLst) — This line prints the resulting list SqLst.