Super Simple Python: Is this a Square Number?

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’ll be covering how to check if a number is a square in just 5 lines of Python!

For the video version of checking if a number is a square in Python:

So far, this series has covered a lot of posts using the random library like the Random Number Generator, the Dice Roll Simulator, and the Password Generator. This is the first episode of Super Simple Python that is under 10 lines of code. We’re going to do this with the help of the math library. 

Check if a Number is Square

There are many ways to check if a number is a square in real life. What’s the easiest way? Take a square root and check if the square root is an integer or not. Luckily, we can just use the sqrt function from the math Python library to do that. We don’t even have to know how to do the actual math for this program.

We’ll start off our program by importing the math library. The math library is native to Python so we don’t have to install it. Next, let’s create a function to check if a number is a square or not. The function will take one parameter, num, which we expect to be an integer. This is the cool part. We can handle all the logic in one line using the math library. We use the sqrt function to get the square root of the number, and then we call the is_integer() function on that to check if the result is an integer. If the result is an integer, we return True. If it’s not, then we return False by default. Note that we can also choose to use an else statement to return False, but we don’t need to.

import math
def is_square(num: int):
    if math.sqrt(num).is_integer():
        return True
    return False

If we test the above numbers, 20, 100, 625, 1337, and 2025 we should get False, True, True, False, True as shown in the image below:

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!


Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount


Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: