Hi! I got really confused with this this code that I found for the is_prime exercise.
Could anyone walk me through it line by line? Thanks!

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


Technically we don't need the else: since the line above returns.

if ___:
    return ___
for n in range(2, x):
    if ____:
        return ___
return True

Values less than 2 will not be tested any further, and if x == 2 it will be rejected from the loop, so will fall through and return True, as it should. Any values that survive the loop must also be Primes, and they will return True, as well.


def is_prime(x):

This line is obviously defining the function is_prime. It has one parameter, x, and is pretty simple.

if x < 2:

This is the beginning of an if statement, it's saying if the argument, x, is less than two, then do this next line with is

Returning false would be the end of this function, but the next line says else, So if x is greater than two, the for loop will happen. The for loop starts pretty simple,

for n in range(2, x):

n is just a random variable name, like any of the variables that you name when you're writing code.
It is iterating through the numbers 2 through x, (however large the argument is)
So for 2, 3, 4, 5, 6, ..., x:

 if x % n == 0:

if x, the argument, divided by a number from 2 to x, the function will end and

return False

If nothing gets returned false, then the program will return true at the end, but the number IS prime.


I get it but I don't understand how x is divided by all the numbers up to x starting from 2 and returns true or false. Let's say x is 5. Does the code check: 5 - 2 == 0, 5 - 3 == 0, 5 - 4 == 0. These all return True because they are not equal to 0 but if I said x is 4 then for example 4 - 3 wouldn't equal to 0 either. So it wouldn't be False. What am I missing?


This is a little harder to explain. x is divided by each number, but the value of x won't change.
nothing will be returned until 5 is divided by each number.
5 % 2 is 1, because one is the remainder
5 % 3 is 2,because two is the remainder
5 % 4 is 1
Because none of these remainders are zero, 5 is prime. Does this help?


Oh right! Now I remember. (I don't know why I put minuses rather than modulos, oh well!) So if x is 6 then for example 6 % 3 won't have a remainder (so it isn't prime) but 5 % 3 will. (it's prime)


Yes! very nice! :slight_smile: Glad I could help


If x is 6 the loop won't iterate but once since 6 is even and 6 % 2 is 0. All even numbers will be ejected this way.


Hey guys, had a question on this one as well. Why is there no else: at the end of the if statement in the for loop? Line 8, specifically. How come that goes under the first else, and not one within the for loop?


You don't always need an else after an if. Is this what you're asking? If there's no else, nothing will happen to other options, basically. Additionally, the first else isn't even needed in this program.


3 posts were split to a new topic: What can be the problem?


I think range(2, x) means from 2 to x-1. Am I right?


Very good job, thank you!:clap:


That is correct, and possibly what the author was hinting at. This is the simplest brute force approach of ellimination. If we think about it a moment, we discover the range can be much smaller and more dialed in, but that is for another day. When you have time to explore this concept (testing primeness) look for other types of ranges that are more selective and more efficient.


A post was split to a new topic: Need help, my code is running elsewhere but i get error


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