6: is_prime question

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

This is the working code I came up with after consulting the forum.

My question is this, why is the last return True formatted like that as opposed to my previous code:

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

If I could have your feedback it would be appreciated, as I'm sure I'm missing some concept that will hold me back later.

#2

You are testing all your values between 2 and 'n', so if there was no value that made the statement False, it will continue (break out of the loop), and thus return True

It'd be the same as saying this:

``````if (for n in range(2,x):
#Do stuff here
else:
#No False statement Occured, we can say it's True.``````

But of course you can't do the 'if(for n in range...)' in the code, but ut would be the same as opposing it.
It a more simple way of writing less code. Though it depends on what you're trying to accomplish, because here it's more code. I hope this helped, if you have any question, just ask, else: Close thread and Like! Thanks in advance