KnowledgeBoat Logo

Computer Science

What is the output of the following code?

numbers = list(range(0, 51, 4))
results = []
for number in numbers:
    if not number % 3:
        results.append(number)
print(results)

Python

Python List Manipulation

32 Likes

Answer

[0, 12, 24, 36, 48]

Working

list(range(0, 51, 4)) will create a list from 0 to 48 with a step of 4 so numbers will be [0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48]. For loop will traverse the list one number at a time. if not number % 3 means if number % 3 is equal to 0 i.e. number is divisible by 3. The numbers divisible by 3 are added to the results list and after the loop results list is printed.

Answered By

17 Likes


Related Questions