For loop


#1

def is_prime(x):
if x < 2:
return False
for n in range(2, x+1):
if x/n == 1:
return True
elif x%n == 0:
return False
it seems that in the for loop, the function only considers the value x = n, because if the others values were considered, the program would have a lot of options to evaluate, but i don't understand why it is in that way, someone knows anything about it? if someone could explain me how this code works i would be forever grateful


#2

@carlospdea take a look below I have commented on your code what each line is doing

#a prime number is a number that
#has only two factors
# 1 and the number itself
def is_prime(x):
    #prime numbers start from 2
    #if the number is less than 2
    #then it would not be a prime number
    if x < 2:
        return False
    
    #now we are going to consider all the numbers
    #from 2 to that number
    for n in range(2, x+1):
        #remember a prime number has only 2 factors
        #if we didvide the given number by 
        #all the numbers from 2 to that same number
        #and we get a remainder of 1 for each then it is a prime number
        if x/n == 1:
            return True
            
        #if we didvide the given number by 
        #all the numbers from 2 to that same number
        #and we get a remainder of 0 for anyone of those numbers
        #then it means the given number has another factor other than
       #1 & itself hence it is not prime
        elif x%n == 0:
            return False
            
            
print is_prime(1)