Unit: Unit 3: Loops • Interactive Practice
Interactive Python programming tutorial and practice in your browser.
# Search for a prime number or confirm primality using for-else
candidate = 7
for divisor in range(2, candidate):
if candidate % divisor == 0:
print(candidate, "is composite (divisible by", divisor, ")")
break
else:
# Executes ONLY if the loop completed naturally without hitting break!
print(candidate, "is a prime number!")