Computer Science

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

Python

Linear Lists

2 Likes

Answer

NumLst = [2, 5, 1, 7, 3, 6, 8, 9]
SqLst = []
for num in NumLst:
    SqLst.append(num * 2)
print(SqLst)
[4, 10, 2, 14, 6, 12, 16, 18]

Working

  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.

Answered By

1 Like


Related Questions