Back to Practice
#0307

Check If a Number Is Prime

EasyPython15 min10 XP

Problem

Given an integer n, return True if n is prime and False otherwise. A prime number is greater than 1 and divisible only by 1 and itself.

Why This Matters

Prime checks teach early exits, edge cases, and why checking up to the square root is enough.

Function Signature

def is_prime(n):

Examples

Example 1
Inputn = 29
OutputTrue

29 has no divisor other than 1 and 29.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Prefer clear Python code before clever one-liners.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Prime number
Input29
ExpectedTrue

No factor up to sqrt(29) divides it.

Composite number
Input25
ExpectedFalse

25 is divisible by 5.

Hidden Test Categories
n <= 1n = 2large even number