Index
Intro – Calculator in Python
Hello, Here we are going to create a Calculator in Python which is the basic project of Python. This Project Teaches you basic functions of mathematics like Addition, subtraction, multiplying, and Division.
Codes with Explanation
# Define a function for addition def add(x, y): return x + y
- This defines a function named
add
that takes two arguments,x
andy
. Functions are reusable blocks of code that perform specific tasks. - Inside the
add
function, thereturn
statement sends back the sum ofx
andy
. This value is used wherever the function is called.
# Define a function for subtraction def subtract(x, y): return x - y
- This defines another function,
subtract
, which performs subtraction. It also takes two argumentsx
andy
. - Inside the
subtract
function, it returns the result of subtractingy
fromx
.
# Define a function for multiplication def multiply(x, y): return x * y
- This function is defined to multiply two numbers
x
andy
. - Returns the product of
x
andy
.
# Define a function for division def divide(x, y): # Check if the denominator is not zero to avoid division by zero error if y == 0: return "Cannot divide by zero!" else: return x / y
- Defines a
divide
function, which will handle division. Again, it takes two arguments. - Checks if the second argument
y
is zero to avoid division by zero, which causes an error in Python. - If
y
is zero, the function returns a message instead of attempting the division. - If
y
is not zero, the code block followingelse
will execute. - Performs the division of
x
byy
and returns the result.
# Display a simple menu for the user print("Select operation:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide")
- Prints a message to prompt the user to select an operation.
- Prints “1. Add” as part of the menu options for the user.
- Prints “2. Subtract” as another option.
- Prints “3. Multiply” as another option.
- Prints “4. Divide” as the final option in the menu.
# Take input from the user for the operation choice = input("Enter choice(1/2/3/4): ")
- Takes the user’s choice (which operation they want to perform) as input. The
input()
function gets input from the user and returns it as a string.
More Programming Projects
# Take input from the user for the operation choice = input("Enter choice(1/2/3/4): ")
- Takes the first number from the user and converts it into a
float
(a number with a decimal point) for calculations. - Takes the second number from the user and converts it into a
float
.
if choice == '1': print(f"{num1} + {num2} = {add(num1, num2)}") elif choice == '2': print(f"{num1} - {num2} = {subtract(num1, num2)}") elif choice == '3': print(f"{num1} * {num2} = {multiply(num1, num2)}") elif choice == '4': print(f"{num1} / {num2} = {divide(num1, num2)}") else: print("Invalid input")
- Checks if the user’s choice is ‘1’ (Addition).
- Calls the
add
function withnum1
andnum2
, and prints the result using an f-string (formatted string). This prints both the expression and the result. - Checks if the user’s choice is ‘2’ (Subtraction).
- Calls the
subtract
function and prints the result in a similar way to addition. - Checks if the user’s choice is ‘3’ (Multiplication).
- Calls the
multiply
function and prints the result. - Checks if the user’s choice is ‘4’ (Division).
- Calls the
divide
function and prints the result. - If the user enters something other than ‘1’, ‘2’, ‘3’, or ‘4’, the code inside
else
runs. - Prints an error message indicating the input was not valid.
Complete Codes
# Simple Calculator in Python # Define a function for addition def add(x, y): return x + y # Define a function for subtraction def subtract(x, y): return x - y # Define a function for multiplication def multiply(x, y): return x * y # Define a function for division def divide(x, y): # Check if the denominator is not zero to avoid division by zero error if y == 0: return "Cannot divide by zero!" else: return x / y # Display a simple menu for the user print("Select operation:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") # Take input from the user for the operation choice = input("Enter choice(1/2/3/4): ") # Take input from the user for two numbers to perform the calculation on num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) # Perform the chosen operation and display the result if choice == '1': print(f"{num1} + {num2} = {add(num1, num2)}") elif choice == '2': print(f"{num1} - {num2} = {subtract(num1, num2)}") elif choice == '3': print(f"{num1} * {num2} = {multiply(num1, num2)}") elif choice == '4': print(f"{num1} / {num2} = {divide(num1, num2)}") else: print("Invalid input")