Overview
In this project, we will learn How to create a simple Pythojn Calculator. But before moving ahead, I will explore the necessary terms like functions, conditional statements, etc.
Table of Contents
Prerequisites you need to know:
Assignment operator – An Assignment operator in Python is defined with a single equal sign (=) used to assign / store an object / value to the variable. E.g., Marks = 74, here; 74 is assigned to the variable ‘Marks’.
For more details: A Comprehensive Guide to Python’s Arithmetic Operators
int() – In Python, int() function is a class type which converts a value to <int class>. In other words, int() function is used to convert a specified value to an integer. E.g. number = int(10.2)
input() – In Python, input() is a built-in function, used to take an input or receive a response from the user. E.g., Name = input(‘enter your best friend name: ’)
def function – def is a reserved keyword, used to define the custom function name. A custom function name is being written just after the def keyword. And passing the parameter name is optional.
Syntax:
def function_name(parameters):
# Code block
return result # (optional) returns a value to the caller
Explanation:
function_name: The name you give to your function, used to call it later.
parameters: Optional inputs that the function takes to perform its task.
return: An optional statement to return a result from the function. If no return statement is given, the function will return None by default.
E.g.,
def python_calculator():
Result = 5*10
print('Calculation:', Result)
#calling the function
python_calculator()
Explanation: In the above example, python_calculator() is a custom function name defined by the user. Variable ‘Result’ store the task is to be done by function, once the function is called. And the print() function displays the result as the function is executed.
For more details: Python Def Function with Practical Examples
Comparison operator – A Comparison operator in Python is defined by double equal sign (==) used to check if both side (left and right) values are the same or not. E.g.,
Example:
age = 18
if age == 18:
print("You're eligible to drive a car.")
Explanation: In the above example, it is being checked whether the value stored in variable age (left side) is equal to 18 (right side) or not.
For more details: Guide to Python’s Comparison Operator
Conditional Statements (if..else) – In Python, conditional statements are defined by code of block i.e., if..elif…else. It is used to define the condition in Python, and execute specific blocks of code based on whether certain conditions are true or false.
Use cases:
if: Tests a condition. If true, executes the block of code inside.
elif: Short for “else if,” allows you to check multiple conditions if the initial if statement is false.
else: Executes a block of code if all preceding conditions are false.
Syntax:
if condition1:
# Code to execute if condition1 is true
elif condition2:
# Code to execute if condition1 is false and condition2 is true
else:
# Code to execute if all conditions are false
Example:
age = 17
if age < 18:
print("You can not drive a car.")
elif age == 18:
print("You need to first take a licence.")
else:
print("Congratulations!! You can drive a car.")
Explanation:
- If age is less than 18, it prints “You can not drive a car.”
- Another condition, if age is equal to 18, it prints “You need to first take a licence.”
- Otherwise, it prints “Congratulations!! You can drive a car.”
For more details: Python Conditional Statement
Project Python Calculator
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
operator = input("Choose any one operator: (+ , - , * , /): ")
def addition(a,b):
result = a+b
return result
def subtraction(a,b):
result = a-b
return result
def multiplication(a,b):
result = a*b
return result
def division(a,b):
result = a/b
return result
#addition
if operator == "+": #1+2=3
print(num1,"+",num2,"=",addition(num1,num2))
elif operator == "-":
print(num1,"-",num2,"=",subtraction(num1,num2))
elif operator == "*":
print(num1,"*",num2,"=",multiplication(num1,num2))
else:
print(num1,"/",num2,"=",division(num1,num2))
Explanation:
Here are the steps Python will follow to execute this code and provide the result:
- Initialize Variables: Python assigns 10 to num1 and 20 to num2.
- Get User Input: Python prompts the user to choose an operator (+, -, *, /). The input is stored in the variable operator.
Define Functions:
- addition(a, b): Adds a and b, and returns the result.
- subtraction(a, b): Subtracts b from a, and returns the result.
- multiplication(a, b): Multiplies a and b, and returns the result.
- division(a, b): Divides a by b, and returns the result.
Evaluate Conditions: Python checks the value of the operator to determine which operation to perform.
Execute the Matching Operation:
- If operator is ‘+‘, it calls addition(num1, num2), prints 10 + 20 = 30.
- If operator is ‘–‘, it calls subtraction(num1, num2), prints 10 – 20 = -10.
- If operator is ‘*‘, it calls multiplication(num1, num2), prints 10 * 20 = 200.
- If operator is ‘/‘, it calls division(num1, num2), prints 10 / 20 = 0.5.
Display the Result: Python executes the appropriate print statement based on the chosen operator, showing the calculation and result in the console.