6. is_prime


#1

I've completed this exercise but it's bugging me. Lets say is_prime is 9, what will n be? Or what value does n in this code take?

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

#2

range() produces a list, so n will be:

2
3
4
5
6
7
8

for x=9, except when n=3, 9%3==0 evaluate to true, so false returns, ending the function (and thus breaking the loop), so n will never be higher value then 3

but when a number is a prime number, n will be all values in range 2 till x (exclusive, given range doesn't include its stop value)

using range() is nothing new, right?


#3

Right right. It all makes sense now. Thanks


#4