Project 2: Calculator - Python Tutorials For Beginners #8
Overview
Introduction
In this tutorial, we’ll create a simple calculator program in Python. The calculator will take two numbers and an operator (+, -, *, /, **) as input and perform the corresponding mathematical operation.
This Python project is a basic calculator that performs arithmetic operations on two numbers. The project employs fundamental programming concepts like functions, If-Else Statements, Variables, and Datatypes to provide a simple calculator interface.
Here's a description of the project in simple language:
Code Implementation
1print("\nWellcome to Calculator.")
2
3# Input System
4x = input("Enter First Number :- ")
5y = input("Enter Second Number :- ")
6a = input("Enter The Symbol Of Operation(+,-,*,/,**) :- ")
7
8# Calculation Processor
9if (a == "+"):
10 print("\nLet's Calculate !!!")
11 print("Addition Of",x,"and",y,"is",eval(x)+eval(y))
12
13elif (a == "-" ):
14 print("\nLet's Calculate !!!")
15 print("Subtraction Of",x,"and",y,"is",eval(x)-eval(y))
16
17elif (a == "*"):
18 print("\nLet's Calculate !!!")
19 print("Multiplication Of",x,"and",y,"is",eval(x)*eval(y))
20
21elif (a== "/"):
22 print("\nLet's Calculate !!!")
23 print("Division Of",x,"and",y,"is",eval(x)/eval(y))
24
25elif (a=="**"):
26 print("\nSolution = ",eval(x)**eval(y))
27
28else :
29 print("Error : Invalid Operation.")
30
31
32print("Thank You.....")
Explanation
1. Introduction and Instructions :
The program begins by taking input from user.
2. Taking User Inputs :
The program prompts the user to enter two numbers and the desired arithmetic operation. These inputs are stored in variables x, y, and a, respectively.
3. Conditions and Calculations :
- The program uses if-else statements to determine the operation to perform based on the value of
a
(operation keyword). - If the operation is +, the program adds the two numbers and prints the result.
- If the operation is -, it subtracts the second number from the first and prints the result.
- If the operation is , * it multiplies the two numbers and prints the result.
- If the operation is /, it divides the first number by the second and prints the result.
- If the operation is **, it calculates the first number raised to the power of the second number and prints the result.
4. Invalid Input Handling :
If the input for the operation is not one of the specified symbol, the program prints "Invalid Input" to indicate that the input is not recognized.
Usage
- Run the script.
- Enter the first number, second number, and the operator.
- The result of the calculation will be displayed.
Feel free to customize and enhance this calculator as you like!.
Remember to test your code thoroughly and handle any edge cases you encounter. Happy coding! 😊
Summary
In summary, this project demonstrates a simple calculator that performs arithmetic calculations based on user inputs. The user is instructed to use specific symbol(+,_,*,/,**) for operations, and the program responds with the calculated result or an "Invalid Input" message. This project effectively utilizes programming concepts like functions for displaying instructions, if-else statements for decision-making, variables to store user inputs, and datatypes for numeric calculations.