DEV Community

ABYS
ABYS

Posted on

1

Let's make a Calculator using Py.

Before we actually make a calculator, let's first see some basic mathematical expressions...

1. Add

num1 = 2
num2 = 3
print(num1+num2) 

5

Enter fullscreen mode Exit fullscreen mode

2. Subtract

num1 = 7
num2 = 5
print(num1-num2)

2

Enter fullscreen mode Exit fullscreen mode

3. Multiply

num1 = 5
num2 = 5
print(num1*num2)

25

Enter fullscreen mode Exit fullscreen mode

4. Divide

num1 = 100
num2 = 5
print(num1/num2)

20

Enter fullscreen mode Exit fullscreen mode

5. Modulus (nothing but remainder)

quotient = 5//2
remainder = 5 % 2
print(quotient , "," ,remainder)

2 , 1

Enter fullscreen mode Exit fullscreen mode

6.Exponentiate (powers)

For ex; a power b in python written as a**b

num1 = 3
num2 = 3
print(num1**num2)

27

Enter fullscreen mode Exit fullscreen mode

Input Datatype and Typecasting

# Addition

num1 = int(input("Enter Number 1 : "))
num2 = int(input("Enter Number 2 : "))

result = num1+num2
print("Result is : ", result)

Enter Number 1 : 1
Enter Number 2 : 4
Result is :  5

Enter fullscreen mode Exit fullscreen mode

Here it appears as steps but I'm unable to present it.
You can just do it and see.

# to make it possible in decimals too we use float

num1 = float(input("Enter Number 1 : "))
num2 = float(input("Enter Number 2 : "))

result = num1+num2
print("Result is : ", result) 

Enter Number 1 : 1.5
Enter Number 2 : 2.5
Result is :  4.0

Enter fullscreen mode Exit fullscreen mode

Similarly, we do for other operations.

Now, with this knowledge we gonna create a simple calculator by using the following code;

print("Simple Calculator")
print("Select Operation : ")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
print("6. Exponentiate")

choice = input("Enter choice (1/2/3/4/5/6) : ")
num1 = float(input("Enter first  Number : "))
num2 = float(input("Enter second Number : "))

if choice == "1" :
   result = num1 + num2
   print(result)
elif choice == "2" :
   result = num1 - num2
   print(result)
elif choice == "3" :
   result = num1 * num2
   print(result)
elif choice == "4" :
   result = num1 / num2
   print(result)
elif choice == "5" :
   result = num1 % num2
   print(result)
elif choice == "6" :
   result = num1 ** num2
   print(result)
else :
   print("option not available")

Enter fullscreen mode Exit fullscreen mode

So, that's what I learned under this topic.
You can use the same code above and chk whether it works for you too..

Platforms I use to try these codes :

  • W3 Schools Tryit Editor
  • Google Colaboratory
  • Visual Studio Code

.....

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Okay, let's go

Top comments (1)

Collapse
 
kavya-sahai-god profile image
Kavya Sahai •

The code is too long and not apt for actual use cases. A better version of the code could be:

print("Simple Calculator")
print("Select Operation: ")
print("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Modulus\n6. Exponentiate")

operations = {
    "1": lambda x, y: x + y,
    "2": lambda x, y: x - y,
    "3": lambda x, y: x * y,
    "4": lambda x, y: x / y,
    "5": lambda x, y: x % y,
    "6": lambda x, y: x ** y,
}

choice = input("Enter choice (1/2/3/4/5/6): ")
num1, num2 = float(input("Enter first number: ")), float(input("Enter second number: "))
result = operations.get(choice, lambda x, y: "Option not available")(num1, num2)
print(result)

Enter fullscreen mode Exit fullscreen mode

This reduces redundancy and makes it better, mate.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay