Here's an example Python code for a simple calculator that can perform addition, subtraction, multiplication, and division:
# Define the calculator function
def calculator():
print("Welcome to the calculator program!")
# Get user input for the operation to perform
operation = input("Please enter the operation to perform (+, -, *, /): ")
# Get user input for the two numbers to use in the calculation
num1 = float(input("Please enter the first number: "))
num2 = float(input("Please enter the second number: "))
# Perform the calculation based on the chosen operation
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
result = num1 / num2
else:
print("Invalid operation entered. Please try again.")
return
# Print the result
print("The result of the calculation is: ", result)
# Call the calculator function
calculator()
To use this calculator code, simply copy and paste it into a Python file and run it. The program will prompt you for the operation to perform (+, -, *, /) and the two numbers to use in the calculation. It will then perform the calculation and print the result.
If you want to create a graphical user interface (GUI) for your Python calculator, you can use a GUI toolkit such as Tkinter. Here's an example code using Tkinter that creates a simple calculator with button.
This code defines a Calculator class that creates a GUI with a display label and buttons for the digits and operators. The press method is called when a button is pressed and it either adds a digit to the display or sets the operator. The calculate method is called when the equals button is pressed and it performs the calculation based on the current state of the calculator. Note that the lambda function is used to pass
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
self.master.title("Calculator")
# Create the display label
self.display = tk.Label(self.master, text="", anchor=tk.E, font=("Arial", 16), width=14, height=1, relief=tk.SUNKEN, borderwidth=3)
self.display.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# Create the buttons
buttons = [
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
]
row, col = 1, 0
for button in buttons:
command = lambda x=button: self.press(x)
tk.Button(self.master, text=button, width=4, height=2, command=command).grid(row=row, column=col, padx=5, pady=5)
col += 1
if col > 3:
row += 1
col = 0
# Initialize the state of the calculator
self.reset()
def reset(self):
self.num1 = None
self.num2 = None
self.operator = None
self.display.config(text="0")
def press(self, button):
if button.isdigit() or button == ".":
self.add_digit(button)
elif button in ["+", "-", "*", "/"]:
self.set_operator(button)
elif button == "=":
self.calculate()
else:
self.reset()
def add_digit(self, digit):
if self.operator is None:
if self.num1 is None:
self.num1 = digit
else:
self.num1 += digit
self.display.config(text=self.num1)
else:
if self.num2 is None:
self.num2 = digit
else:
self.num2 += digit
self.display.config(text=self.num2)
def set_operator(self, operator):
self.operator = operator
def calculate(self):
if self.num1 is not None and self.num2 is not None and self.operator is not None:
num1 = float(self.num1)
num2 = float(self.num2)
if self.operator == "+":
result = num1 + num2
elif self.operator == "-":
result = num1 - num2
elif self.operator == "*":
result = num1 * num2
elif self.operator == "/":
if num2 == 0:
result = "Error"
else:
result = num1 / num2
else:
result = "Error"
self.display.config(text=result)
self.reset()
# Create the main window
root = tk.Tk()
# Create the calculator
calculator = Calculator(root)
# Start the main loop
root.mainloop()
This code defines a Calculator class that creates a GUI with a display label and buttons for the digits and operators. The press method is called when a button is pressed and it either adds a digit to the display or sets the operator. The calculate method is called when the equals button is pressed and it performs the calculation based on the current state of the calculator. Note that the lambda function is used to pass