Skip to main content

How To Create A Simple Calculator using Python (Beginner - Friendly)

Hello! Welcome to my blog.

None of us is unfamiliar with the calculator in Math lessons. Today, by reading my blog, you'll create that calculator, but entirely by code! With only a few lines of Python code, you're going to make a calculator by yourself! You don't have to be a professional programmer, nor do you have to know complex algorithms - only need a computer and a little bit of curiosity. Don't waste time anymore, let's get into it!

Resource: Mart Production from Pexels
1. Prepare:

- Download Python: https://www.python.org/downloads/

When you download Python, it also has the code compiler (IDLE).

- Or: Visual Studio Code: https://code.visualstudio.com/download

IDLE of Python.
2. Write the program

Below is the code for the Python simple calculator. I'll leave the code, and in the next topic, I'm going to explain the code.

My code (Edited through Carbon)

You can view the full source code on my GitHub here:


3. Explain the code

- Program Introduction:

The code:

This line simply prints the title of the program, so the user knows what the script does.

- Infinite Loop:

The code:


We use an infinite loop so that the calculator can keep running until the users decide to exit.


- Getting User Input:

The code:


The input() function asks the users for data from the keyboard (as a string).
The float() function changes that string into a floating-point number so we can do calculations with decimals.

- Choosing An Operation:

The code:


The program asks the user to choose what operation they want: addition, subtraction, multiplication, or division.

- Performing The Calculation:

The code:




We use if..., elif..., else..., statements to check which operation the user chose.
If division is selected, we add a check to make sure num2 is not zero (to avoid errors).
If the user types an invalid symbol, the program prints an error message.

- Ask The User To Continue Or Not:

The code:


The program asks if the user wants to calculate again.
.lower() makes the input lowercase, so it works even if the user types "YES" or "Yes".
If the answer is not "yes" (or "YES", "Yes",...), the "break" commands the while loop to stop and stop the program.

Output:
Here's the output of this program ( this is the example):



Conclusion
And that's it, you've created a simple calculator using Python! This project is a fun and beginner-friendly way to practice your coding skills. You've learned how to take user input, perform basic calculations, and display the results. Projects like this help you understand the fundamentals of Python and give you confidence to try more complex programs in the future.
Now, fire up your Python editor and give it a try. Happy coding! 🚀

Connect with me:
Email: trinhlehoangphuoc@gmail.com

See you next Friday!































 

Comments

Post a Comment

Popular posts from this blog

Facts About Misunderstood Animal

  Hello! Welcome to my blog. Have you ever wondered why some animals are popular while others are unpopular? In our natural world, every animal has its place and purpose in which to live, such as us! Today I'll tell you some misconceptions about misunderstood animals that are not true. Don't waste time anymore, let's get to it! 1. Snakes are slimy and disgusting You may hear many people around you say that snakes are slimy and disgusting to touch. Not true! That's a common misconception that many people trust it. In fact, a snake's skin is smooth and dry, not as disgusting as you thought. That's the reason why some people love to touch snakes! 2. Cockroaches are dirty pests People think that cockroaches are dirty pests that like to hide in dark, filthy places such as your fridge's legs. They hate to see cockroaches eat rubbish and dead animals as they decay. People also believe that cockroaches' bodies are covered in germs. But in fact, cockroaches clean...

Review Film: "The Theory Of Everything"

  Hello! Welcome to my blog. If you're a fan of science, authentic knowledge, and heart-touching stories, you must watch "The Theory of Everything". It is the best choice for a memorable viewing experience. I watched this film and thought I hadn't seen it earlier! Today, I'll help you have a more detailed look at it. Introduction "The Theory Of Everything" is a biographical film, directed by James Marsh in 2014. Its stars are Eddie Redmayne and Felicity Jones - two main characters in the movie. It was nominated for a Hollywood Film Award in 2015 and 123.7 million USD was obtained from the filmmakers. Main Content The film is about the life of Stephen Hawking, one of my favorite scientists. It is based on the book  Travelling To Infinity: My Life With Stephen,  written by Jane Hawking, Stephen's wife. The film is set in England in the early 1960s and tells the story of Hawking's relationship with his wife, his diagnosis of motor neuron disease, an...

How To Make Avocado Ice Cream - The Savior Of The Summer

  Hello! Welcome to my blog. Have you ever tried a scoop of cool, greasy ice cream that melts right on your tongue and is filled with the natural flavor of ripe avocado? You don't need to buy avocado ice cream at expensive ice cream shops. Today, I'll tell you how to make avocado ice cream at home effortless dessert that is still delicious! Don't waste time anymore, let's get to it! Ingredients: - Two avocados - 200ml fresh milk - 100ml condensed milk - 200ml whipping cream - 50g sugar - A teaspoon of lemon juice - A little salt - Vani cream - Dried coconut The way to make: Step 1: Blend avocado mixture - Cut the avocado in half, throw the seeds, and take its flesh. - Put it in a blender with fresh milk, condensed milk, sugar, and lemon juice. - Blend that mixture until it is smooth and viscous. Step 2: Whip fresh cream Pour the whipping cream into a large bowl and add a pinch of salt. Whip the cream until it becomes soft and has a light texture. Step 3: Mix the avocado...