• 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
  • Ruby
  • 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.  How to Save and Use Styles from Existing Word File With python-docx
  • 2.  Shift Values in a List Using Python
  • 3.  How to Group By Columns and Count Rows in pandas DataFrame
  • 4.  pandas idxmin – Find Index of Minimum Value of Series or DataFrame
  • 5.  Initialize Multiple Variables in Python
  • 6.  Keep Every Nth Element in List in Python
  • 7.  Using Python To Split String by Comma into List
  • 8.  Using Python to Count Even Numbers in List
  • 9.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 10.  How to Iterate over Everything in Word Document using python-docx

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy

x