Let's make a Calculator using Py.

abys_learning_2024

ABYS

Posted on July 16, 2024

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

.....

💖 💪 🙅 🚩
abys_learning_2024
ABYS

Posted on July 16, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Let's make a Calculator using Py.
programming Let's make a Calculator using Py.

July 16, 2024