CBSE Class 11 Computer Science Question 17 of 25

List Manipulation — Question 1

Back to all questions
1
Question

Question 1

Write a program to increment the elements of a list with a number.

Solution
lst = eval(input("Enter a list: "))
print("Existing list is:", lst)

n = int(input("Enter a number: "))

for i in range(len(lst)):
    lst[i] += n

print("List after increment:", lst)
Output
Enter a list: [1, 2, 3, 4, 5]
Existing list is: [1, 2, 3, 4, 5]
Enter a number: 10
List after increment: [11, 12, 13, 14, 15]
Answer

lst
=
eval
(
input
(
"Enter a list: "
))
print
(
"Existing list is:"
,
lst
)
n
=
int
(
input
(
"Enter a number: "
))
for
i
in
range
(
len
(
lst
)):
lst
[
i
]
+=
n
print
(
"List after increment:"
,
lst
)
Output
Enter a list: [1, 2, 3, 4, 5]
Existing list is: [1, 2, 3, 4, 5]
Enter a number: 10
List after increment: [11, 12, 13, 14, 15]