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}")


References:
- input(): https://docs.python.org/3/library/functions.html#input
- float(): https://docs.python.org/3/library/functions.html#float
- if-elif-else statements: https://docs.python.org/3/tutorial/controlflow.html#if-statements
- f-strings: https://docs.python.org/3/reference/lexical_analysis.html#f-strings
- Arithmetic operators: https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
Have Questions? Post them here!
- Program 11: Calculate Percentage - 1000+ Python Programs
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 1: Print Hello World! - 1000+ Python Programs
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Program 15: Find String is a Palindrome or Not - 1000+ Python Programs
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 13: Reverse a String - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 14: Sum of Even Numbers from 1 to 100 - 1000+ Python Programs
- Program 10: Modulo of Two Numbers - 1000+ Python Programs
- 16: Find the largest element in a List - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- Program 2: Print your name using print() function - 1000+ Python Programs
- 19: Simple Calculator using built-in functions - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- Program 4: Input Name and Age and Print - 1000+ Python Programs
- Program 3: Print the name of user as input - 1000+ Python Programs
- How to fix command not found brew (bash, zsh) on macOS Terminal - MacOS
- How to install and Configure sar sysstat tools in Ubuntu Linux - Linux
- PHP Script to Upload Images to Server - PHP
- Error : Facebook SDK AndroidRuntime?FATAL EXCEPTION: main - Android
- Android Studio Button onClickListener Example - Android-Studio
- [Solution] Notepad++ Compare option unavailable - NotepadPlusPlus
- NewApi error : Finds API accesses to APIs that are not supported in all targeted API versions - Android
- Calculate Volume of Cone - C-Program