CBSE Class 12 Computer Science Question 74 of 120

Review of Python Basics — Question 26

Back to all questions
26
Question

Question 21

A list Num contains the following elements:

3, 25, 13, 6, 35, 8, 14, 45

Write a program to swap the content with the next value divisible by 5 so that the resultant list will look like:

25, 3, 13, 35, 6, 8, 45, 14
Solution
Num = [3, 25, 13, 6, 35, 8, 14, 45]
for i in range(len(Num) - 1):
    if Num[i] % 5 != 0 and Num[i + 1] % 5 == 0:
        Num[i], Num[i + 1] = Num[i + 1], Num[i]
print("Resultant List:", Num)
Output
Resultant List: [25, 3, 13, 35, 6, 8, 45, 14]
Answer