• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Python to Check If a Number is a Perfect Square

Using Python to Check If a Number is a Perfect Square

June 20, 2022 Leave a Comment

In Python, you can easily check if a number is a perfect square by taking the square root and checking if the square root is an integer.

a = 49
b = 20
c = 16

def check_perfect_square(num):
    return int(num ** (1/2)) == num ** (1/2)

print(check_perfect_square(a))
print(check_perfect_square(b))
print(check_perfect_square(c))

#Output:
True
False
True

When working with numbers in Python, the ability to check certain properties of those numbers can be very useful.

One such property is if a number is a perfect square.

A number is said to be a perfect square number if the square root of that number is an integer. Some perfect squares include 1, 4, 9, 16, 25, 36, etc.

To check if a number is a perfect square in Python, you can do the following.

First, find the square root of the given number. Then, you can see if the square root is an integer by comparing the square root to the converted integer value of the square root.

Below is a simple function which will check if a number is a perfect square in Python.

a = 49
b = 20
c = 16

def check_perfect_square(num):
    return int(num ** (1/2)) == num ** (1/2)

print(check_perfect_square(a))
print(check_perfect_square(b))
print(check_perfect_square(c))

#Output:
True
False
True

Hopefully this article has been useful for you to learn how to check if a number is a perfect square using Python.

Other Articles You'll Also Like:

  • 1.  pandas to_pickle – Write DataFrame to Pickle File
  • 2.  Python Subtract Days from Date Using datetime timedelta() Function
  • 3.  Using Python to Remove Non-Empty Directory
  • 4.  Length of Tuple Python – Find Tuple Length with Python len() Function
  • 5.  Get First Key and Value in Dictionary with Python
  • 6.  Using Python to Increment Dictionary Value
  • 7.  Transpose DataFrame pandas – Using the pandas transpose Function
  • 8.  pandas fillna – Replace NaN in Dataframe using Python
  • 9.  Check if Number is Larger than N in Python
  • 10.  pandas floor – Find the Floor of a Column Using Numpy floor

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy