Check Perfect Square in Python

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • What a perfect square means with easy examples.
  • How to check a number using an integer loop.
  • How to use square root and list perfect squares in a range.

Overview

A perfect square is any number that can be written as k * k. This page shows two checking styles and a live preview.

Two programs

Loop check and square-root-based range scan.

Live preview

Try values like 15, 16, 1, and 0 instantly.

Interview clarity

Know both integer and root-based methods.

Prerequisites

Basic Python loops, integer multiplication, and import usage.

  • For-loop and if-condition basics.
  • Knowing that math.isqrt gives integer square root.

What is a perfect square?

A perfect square is a number equal to k squared for some integer k.

Examples: 1, 4, 9, 16, 25. Non-examples: 2, 3, 5, 6.

Quick examples

16Square
Reason
4 * 4 = 16
15Not square
Reason
Between 3^2 and 4^2
1Square
Reason
1 * 1 = 1

Live preview

Checks with integer logic, then reports verdict.

Use whole numbers n >= 0.

Live result
Press "Run check" to see result.

Algorithm

Goal: verify if n equals k squared for some integer k.

Loop method

Try i from 0 while i*i <= n and look for exact match.

Square-root method

Use integer root and compare root*root with n.

📜 Pseudocode

Pseudocode
function is_perfect_square_loop(n):
    if n < 0:
        return false
    i = 0
    while i * i <= n:
        if i * i == n:
            return true
        i = i + 1
    return false
1

Integer loop check

Simple and beginner-friendly perfect square check.

python
def is_perfect_square(number: int) -> bool:
    if number < 0:
        return False
    i = 0
    while i * i <= number:
        if i * i == number:
            return True
        i += 1
    return False


test_number = 16
if is_perfect_square(test_number):
    print(f"{test_number} is a perfect square.")
else:
    print(f"{test_number} is not a perfect square.")
2

Range scan using integer square root

Use math.isqrt and print all perfect squares from 1 to 50.

python
import math


def is_perfect_square(num: int) -> bool:
    if num < 0:
        return False
    root = math.isqrt(num)
    return root * root == num


print("Perfect Squares in the Range 1 to 50:")
for i in range(1, 51):
    if is_perfect_square(i):
        print(i, end=" ")

Notes

Prefer integer roots. math.isqrt avoids floating-point rounding issues.

Loop complexity. Loop approach takes O(sqrt(n)) checks.

Negative values. Return false immediately for negatives.

❓ FAQ

A whole number is a perfect square if it equals k * k for some whole number k.
Yes. 1 = 1 * 1.
The loop is easy to understand and avoids floating-point concerns.
It means we only test candidate roots whose square has not passed n.
Yes. It gives exact integer square root and is great for robust checks.
No. Perfect square is about k * k; perfect number is about divisor sums.

🔄 Input / output examples

Try these values in the first example.

ValueTypical result
16Perfect square
15Not perfect square
1Perfect square
0Perfect square

Edge cases

n = 0

Zero is square

0 = 0 * 0.

Negative n

Not a real integer square

Return false for negatives in this tutorial.

Large values

Avoid float precision pitfalls

Prefer math.isqrt over float sqrt for reliability.

⏱️ Time and space complexity

ApproachTime (single n)Extra space
Loop until i*i > nO(sqrt(n))O(1)
isqrt-based checkO(1) practicalO(1)
Range 1..U scanO(U) checksO(1)

Summary

  • Definition: n is square if n = k * k.
  • Methods: integer loop or math.isqrt.
  • Remember: 0 and 1 are perfect squares.
Did you know?

A perfect square can be arranged into a square grid with equal rows and columns. The gaps between consecutive squares (1, 4, 9, 16...) are odd numbers (3, 5, 7, 9...).

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful