I think I understand why this code works, and I want to explain it in words to see if my line of thinking is correct. May someone validate whether or not my line of thinking is correct?
def is_prime(x): if x < 2: return False elif x == 2: return True else: for n in range(2, x - 1): if x % n == 0: return False return True
For instance, let's say x is equal to 21. I know 21 is not a prime number. It checks to see if it's less than 2, which 21 is not, so it moves on. 21 is not equal to 2, so it moves on. Then, it enters the for loop, starting with 21 modulo 2, which is not equal to zero. Next, does it automatically move n up to 3, and check to see if 21 modulo 3 is equal to zero (which it is, so it's not a prime number), as opposed to automatically moving on to "return True"? It will iterate through all of the possible values of n before moving on to the True statement, correct?