Python Practice › Function Programs › Program 10
Prime check function
Write a Python function that returns True if a number is prime.
Input
7, 12, 1
Output
[(7, True), (12, False), (1, False)]
Approach
- Test divisors up to the square root.
Solution
def is_prime(n): if n < 2: return False for d in range(2, int(n ** 0.5) + 1): if n % d == 0: return False return True print([(n, is_prime(n)) for n in [7, 12, 1]])Output
[(7, True), (12, False), (1, False)]
Learn the concept
New to this topic? Read the lesson, then come back.