Advanced Calculator Using Python

# Function to perform addition
def add(x, y):
    return x + y

# Function to perform subtraction
def subtract(x, y):
    return x - y

# Function to perform multiplication
def multiply(x, y):
    return x * y

# Function to perform division
def divide(x, y):
    if y == 0:
        return "Cannot divide by zero"
    return x / y

# Function to perform exponentiation
def exponent(x, y):
    return x ** y

# Function to calculate the square root
def square_root(x):
    if x < 0:
        return "Cannot calculate the square root of a negative number"
    return x ** 0.5

# Main calculator function
def calculator():
    print("Select operation:")
    print("1. Addition")
    print("2. Subtraction")
    print("3. Multiplication")
    print("4. Division")
    print("5. Exponentiation")
    print("6. Square Root")
    print("7. Quit")

    while True:
        choice = input("Enter choice (1/2/3/4/5/6/7): ")

        if choice == '7':
            print("Calculator is exiting...")
            break

        if choice not in ('1', '2', '3', '4', '5', '6'):
            print("Invalid input. Please enter a valid option.")
            continue

        if choice == '6':
            num = float(input("Enter a number: "))
            result = square_root(num)
            print(f"Square root: {result}")
        else:
            num1 = float(input("Enter first number: "))
            num2 = float(input("Enter second number: "))

            if choice == '1':
                result = add(num1, num2)
                print(f"Result: {result}")
            elif choice == '2':
                result = subtract(num1, num2)
                print(f"Result: {result}")
            elif choice == '3':
                result = multiply(num1, num2)
                print(f"Result: {result}")
            elif choice == '4':
                result = divide(num1, num2)
                print(f"Result: {result}")
            elif choice == '5':
                result = exponent(num1, num2)
                print(f"Result: {result}")

if __name__ == "__main__":
    calculator()


#--------------------------------End Of Code--------------------------------#

Join our team, where career growth knows no limits.

For More Details

Our internship program guarantees personalized attention for optimal career development in the tech industry.

+91 97879 68375

-> You should bring your own laptop during the internship

Who can apply?

Internships are open for students who: B.E / B.Tech / M.E / M.Tech / B.Sc / M.Sc / M.C.A / B.C.A / Diploma.

  • Ongoing Students Only
  • days, 5days/ 10days or Based on Student Preference
  • Limited 1 to 10 students per slot
  • Registration fees : 1000 INR
  • Slots are filling quickly only fewer slots are available