Header Ads Widget

Develop Your Creative Skill with
Free Design Tutorials

Our blog helps to Provide Latest Tips and Tricks tutorials about Blogging
Business, SEO Tips, much more. this Blog and stay tuned to this
blog for further updates.

Simple Calculator in Python Assignment

simple calculator in python assignment

 

In the world of programming, Python is considered one of the most popular languages. It's easy to learn, versatile, and has a vast range of applications. One of its many uses is creating calculators, and in this article, we will explore the steps to create a simple calculator in Python.

To begin with, you will need to install Python on your computer, which you can download from the official website. Once installed, you can open the Python interpreter or IDE of your choice and start writing code.


First, let's create a basic menu for our calculator. We will give the user four options to choose from: addition, subtraction, multiplication, and division. Here is the code to achieve this:

#####################

print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

#####################


The user will input the number corresponding to the operation they wish to perform. Next, we will prompt the user to input the two numbers they want to perform the operation on. Here is the code:

#####################

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


#####################


Note that we have used the float() function to ensure that the numbers entered are in decimal format. We will now use conditional statements to perform the desired operation based on the user's choice.

#####################

if choice == '1':
   result = num1 + num2
elif choice == '2':
   result = num1 - num2
elif choice == '3':
   result = num1 * num2
elif choice == '4':
   result = num1 / num2
else:
   print("Invalid input")



#####################


Finally, we will print the result using the print() function.

#####################

print("Result: ",result)


#####################


print("Result: ", result)That's it! You have created a simple calculator in Python. Here is the complete code:

#####################

print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

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

if choice == '1':
   result = num1 + num2
elif choice == '2':
   result = num1 - num2
elif choice == '3':
   result = num1 * num2
elif choice == '4':
   result = num1 / num2
else:
   print("Invalid input")

print("Result: ",result)


#####################

In conclusion, creating a simple calculator in Python is a great way to learn the language and its basic syntax. With the help of the code above, you can create your calculator, and with a little creativity and imagination, you can expand it to suit your needs.