PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Programs to check if a number is a Perfect Square

Python Programs to check if a number is a Perfect Square

Updated on: March 31, 2025 | Leave a Comment

In this article, we’ll explore how to write a Python program to determine whether a given number is a perfect square. This exercise will help you understand mathematical operations and conditional logic in Python.

A Perfect Square is a number that can be expressed as the square of an integer. In other words, if you multiply an integer by itself, the result is called a Perfect Square. For example, 4=2×2, so 4 is a perfect square. Similarly, 9=3×3, so 9 is a perfect square. And so on…

This tutorial covers various methods to check if a number is a perfect square in Python, with explanations and examples.

Table of contents

  • 1. How to check if Number is Perfect Square in Python
  • 2. Using math.isqrt function

1. How to check if Number is Perfect Square in Python

Steps to check if a number is a Perfect Square

  1. Set Number

    Set number or take an integer number as input from the user.

  2. Handle Negative and Zero Cases:

    If the number is negative, it cannot be a perfect square. Return False.
    If the number is 0, it is a perfect square (0 * 0 = 0). Return True.
    Ensure the number is non-negative because negative numbers cannot be perfect squares.

  3. Calculate Square Root

    Use math.sqrt(num) to compute the square root of the number. For Example, math.sqrt(16) = 4.0

  4. Check if the Square Root is an Integer

    Determine if the calculated square root is an integer. If the square root is an integer, the original number is a perfect square. Use the is_integer() method to verify if the square root is a whole number.

    For example, math.sqrt(15) = 3.872983346207417 returns False, whereas math.sqrt(16) = 4.0 returns True.

  5. Return the result

    Return True if the square root is an integer, and False otherwise.

Code Example

import math

def is_perfect_sq(num):
    if num < 0:
        return False  # Negative numbers cannot be perfect squares
    sqrt_num = math.sqrt(num)
    return sqrt_num.is_integer()  # Check if the square root is an integer

# Check if it's a perfect square
print(is_perfect_sq(15))    # False
print(is_perfect_sq(36))    # TrueCode language: Python (python)

2. Using math.isqrt function

math.isqrt() is a function in Python’s math module that computes the integer square root of a non-negative integer. It avoids floating-point precision issues by returning an exact integer.

For Example, a Square root of 15 is 3.872983346207417, but math.isqrt(15) returns 3.

This method uses the math.isqrt() function to calculate the square root of a number and check if the integer square root, when squared, equals the original number to determine if it is a perfect square in Python.

For instance, for num = 25, math.isqrt(25) returns 5. Multiplying 5 * 5 results in 25, validating it as a perfect square.

Code Example

import math

# Function to check if a number is a perfect square
def is_perfect_square(num):
    if num < 0:
        return False  # Negative numbers cannot be perfect squares

    sqrt_num = math.isqrt(num)  # Get the integer square root
    is_perfect_sq = sqrt_num * sqrt_num == num  # Verify if the square matches the number
    return is_perfect_sq

# Test cases
print(is_perfect_square(16))  # True
print(is_perfect_square(20))  # FalseCode language: Python (python)

Explanation

  • math.isqrt(): Computes the integer square root of a number, avoiding potential floating-point precision errors.
  • Validation check: Compute the integer square root using math.isqrt() and multiply it by itself. If the result equals the original number, it confirms that the number is a perfect square.
  • math.isqrt() is available from Python 3.8. For earlier versions, use int(math.sqrt(num))

Note:

  • Zero is considered a perfect square: 0×0=0
  • Negative numbers are never perfect squares because the square of any real number (positive or negative) is non-negative.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com