Categories
General Python python starter projects

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
 
print(is_square(20))
print(is_square(100))
print(is_square(625))
print(is_square(1337))
print(is_square(2025))

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:

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.

Categories
General Python python starter projects

Super Simple Python: Make Your Own Contacts List

Super Simple Python is a series dedicated to creating super simple Python projects for beginners to be able to do in under 15 minutes. In this episode, we’ll be covering building your own contacts list in Python and persisting it in a JSON file in under 20 lines of code!

Video version here:

Create Our Python Contact List

To get started, we’ll import the only library we need for this, json. In the video, I don’t define this as a function, but just for practice, let’s define a function. We’ll make a function that will take our contact (expected as a dictionary) and a filename (expected as a string) and save it in a JSON file. In this function, we’ll use “a” when opening the file. The “a” parameter stands for “append” and it tells the program to append to the file if it exists, else to create the file and then add to it.

def save_contact(contact: dict, filename: str):
    with open(filename, "a") as f:
        json.dump(contact, f)

For our contacts, we’ll have four fields. Their name, email, phone number, and your relationship with them.

print("This program saves a Contact in your Contacts List")
name = input("Contact name? ")
email = input("Contact email? ")
phone = input("Contact phone? ")
relationship = input("Contact relationship? ")

After we use input to get our contact information we’ll save it into a dictionary to match the schema that we gave our save_contact function we made above.

contact = {
    "name": name,
    "email": email,
    "phone": phone,
    "relationship": relationship
}

Finally, we’ll call the save_contact function we made and pass it the contact dictionary and a filename we want to save our contacts in.

save_contact(contact, "contacts.json")

Check Our JSON Contact List Python

Once we run our function, we will see something like the image below. Let’s say I want to add my friend Michael Jordan to my contact list, I’d simply pass his name, his email, his phone number, and our relationship into the program and it would create an entry for me.

adding a contact to the json contact list

After running the program, we can open up our JSON file to make sure it ran correctly and we should see something like the image below. You’ll notice I already have Tom Brady in here, that’s because I used him for a test earlier. In the video I also add Taylor Swift (my favorite celebrity). One thing to note here is that this is not correct JSON syntax. That’s okay though, it’s still readable and that’s what matters.

{
    "name": "Tom Brady",
    "email": "TB12@tb12.com",
    "phone": "7777777",
    "relationship": "Friend"
}{
    "name": "Michael Jordan",
    "email": "23@michaeljordan.com",
    "phone": "1232323",
    "relationship": "Friend"
}

Further Reading

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.