DEV Community

lifeascoder
lifeascoder

Posted on

Make A calculator with Python with 2 lines of code.

Learning Python? Trying to make a Calculator? Then Read this post. For creating an easy calculator in python you just need two lines of code and you don't need to install any other packages for that.

AND THIS IS THE CODE _

x = input("Type your equation here : ")
print("Your Answer is : ", eval(x))
Enter fullscreen mode Exit fullscreen mode

So, What does this code means?

In the first line, you can see x which is a variable and input("Type your equation here : ") which means an input and when it prints, it's like the selected text in image given below.

Image description

And for the next line - print("Your Answer is : ", eval(x)). The first code, which is print("Your Answer is : ) shows the text inside the quotes. And , eval(x) Calculates the equation.

*RESULT - *

Image description
Image description
Image description

⚠️ WARNING - Always put space between every digits you put!

Top comments (3)

Collapse
 
curiousdev profile image
CuriousDev

Thank you, but I want to add something: If you look at what eval() actually does, you will find, that it can execute code, which can be harmful in an application.
In other words, it not only can do calculations, but execute other code, too.

Collapse
 
coderlifeisawesome profile image
lifeascoder

Oh, I see. Thank you for the information.

Collapse
 
coderlifeisawesome profile image
lifeascoder

Any doubts ?? Comment