19: Simple Calculator using built-in functions - 1000+ Python Programs


Our calculator will perform the following arithmetic operations using two numbers inputted by the user,

+ (addition) Add two operands
- (subtraction) Subtract the second number from the first number
* (multiplication) Multiplie two numbers
/ (division) Divide the first number by the second number
% (modulo) Get the remainder of the division of the first number by the second number
** (exponentiation) Raise the first number to the power of the second number
# Python Program no. 19
# Simple Calculator
# 1000+ Python Programs by Code2care.org

print("Simple Python Calculator")
print("------------------------")
input_operation = input("Enter an Operation you want to perform: + - * / % ** ")
input_number1 = float(input("Enter the 1st number: "))
input_number2 = float(input("Enter the 2nd number: "))


if input_operation == "+":
    calculation_result = input_number1 + input_number2
elif input_operation == "-":
    calculation_result = input_number1 - input_number2
elif input_operation == "*":
    calculation_result = input_number1 * input_number2
elif input_operation == "/":
    calculation_result = input_number1 / input_number2
elif input_operation == "%":
    calculation_result = input_number1 % input_number2
elif input_operation == "**":
    calculation_result = input_number1 ** input_number2
else:
    print("Invalid operation.")
    exit()

print(f"{input_number1} {input_operation} {input_number2} = {calculation_result}")
Python Calculator Program Output - Example 1
Python Calculator Program Output - Example 2

References:

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap