FAQ: Learn Python - Practice Makes Perfect - factorial FAQ: Learn Python - Practice Makes Perfect - is_prime



This community-built FAQ covers the “s_prime” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise s_prime:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!


Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!


2 posts were split to a new topic: Why does this Return False When x = 9?


2 posts were split to a new topic: Is prime - return indent


2 posts were split to a new topic: Prevent the return False from breaking the loop


hey bros I don’t like this I am pretty sure there is an additional step being made which is not necessary and shouldn’t be there.

def is_prime(x):
    if x < 2:
        return False
        for n in range(2, x-1): #I MEAN THIS LINE RIGHT HERE
            if x % n == 0:
                return False
        return True

print is_prime(13)
print is_prime(10)

Why is there the (-1) I don’t think it should be there. Or I am just tired and it still counts it from 0… like. Range(2)… 0, 1 and then stop at x-1 for instance 10 which would mean 9, numbers 2 to 9 that is… 1, 2, 3, 4, 5, 6, 7, 8, 9… oh god damnit I see now, somebody please confirm I understand. Wait no need to I get it.


why does is_prime(3) equal none?


and actually why it isnt working right?


None means no value is returned. If x is greater then 2, the only possible return value is True. How should false be returned for values like 9 and 10 for example?


I don’t understand the solution they have given:

def is_prime(x):
##if x < 2:
####return False
####for n in range(2, x-1):
######if x % n == 0:
########return False
####return True

For is_prime(2) it somehow gives True, yet 2 /2 =1, so 2 % 2 == 0, which should return False. What am I missing?
Also, in the range function why is it not range(2, x) as range functions don’t include the number after the comma? I assume no number divided by the previous number gives % == 0 (except possibly 2), but still it seems more certain to use x.


i assume by so 2 % 2 == 0 you mean the if condition? Excepts the code never gets there, the for loop never makes any iteration, given the loop condition is false from the start

never understood that, range(2, x) works just as well.


Great thanks, of course there is no n in range(2,1) or range(2,2), so it ignores the iteration. Thanks for your help.


Why does the following code fail at -10?

def is_prime(x):
if x == 0 or x == 1:
return False
elif x == 2:
return True
for number in range(2, x):
if x % number == 0:
return False
return True


negative numbers can’t be prime numbers, so for negative numbers false must be returned

How do you think your code is currently handling that?