# 6. Is Prime

#1

Code is running fine, had some queries.

``````def is_prime(x):
if x < 2:
return False
elif x == 2:
return True
else:
for i in range(2, x):
if x % i == 0:
return False
return True``````

could you please explain me what this code below means? as in the instruction they had asked us range(2,x+1), but here its different.
and the return true in the last line, why didn't we put an else statement?

``````else:
for i in range(2, x):
if x % i == 0:
return False
return True``````

#2

no, the instructions ask you to do a loop from 2 till x-1, since you don't want to include `x`, but range doesn't include the `x` value, so you can just do `x`, it is a test of the exercise to test your knowlegde

because if you do, in the first run of the loop, either if is true, or else will run, no matter which of this two scenarios is true, a return keyword is reached in the first run of the loop, causing the function to end.

You only want to return True after the looped has finished running, because then you successfully determined that the number is a prime

#3

You can use else statement instead of return statement.Finally the results are same.
But the else statement should be of for loop.i.e.you should use for/else loop.

#4

i must have misread that, i thought he asked why didn't have an `if/else` statement. Yea, you can use else, if is a `for/else` indeed, i know this.

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.