I am making a program which prints out all the numbers in the list which are prime.
First, it made a list from 1 to 10 using list comprehension and named it as num
.
Then I made a list called f
where the prime numbers are stored. Then I iterated over num
and then wrote a piece of code which divides each number of num
by a number which ranges from 2 to the number before it [range(2,x)
where x is the number itself]. If the number is not divisible by any of the number in its corresponding range, it is inserted into f
. Then, at last, the list f
is print.
HERE IS THE CODE
The output should be:
[3,5,7]
(NOTE: 2 won’t be there even though it is prime)
but I am getting the output like this
['3', '4', '5', '5', '5', '6', '6', '7', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '1', '0', '1', '0', '1', '0', '1', '0', '1', '0', '1', '0']
’
This is happening because as many times as the interpreter is seeing that the number is not divisible by a number, that many times it is inserting it into f
(for example, 5 will be in f
three times as it is not divisible by 2,3 and 4).
Therefore I request the community to give me suggestions so that I can rectify my code
THANK YOU