-

Easy Python Learning: Basic calculator script in Python

 Easy Python Learning: Basic calculator script in Python

Easy Python Learning: Basic calculator script in Python

To create a calculator in Python, Below is a simple script that can perform addition, subtraction, multiplication, and division:

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    if y == 0:
        return "Error: Cannot divide by zero"
    return x / y

print("Basic Calculator")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

while True:
    choice = input("Enter choice (1/2/3/4) or 'q' to quit: ")

    if choice.lower() == 'q':
        print("Exiting the calculator.")
        break

    if choice not in ['1', '2', '3', '4']:
        print("Invalid input. Please try again.")
        continue

    num1 = float(input("Enter the first number: "))
    num2 = float(input("Enter the second number: "))

    if choice == '1':
        print("Result:", add(num1, num2))
    elif choice == '2':
        print("Result:", subtract(num1, num2))
    elif choice == '3':
        print("Result:", multiply(num1, num2))
    elif choice == '4':
        print("Result:", divide(num1, num2))
    else:
        print("Invalid input. Please try again.")

Save this code in a file with a .py extension, run the script, and you'll have a basic calculator in action. It will keep asking for the user's choice until the user decides to quit by entering 'q'. The calculator handles floating-point numbers and provides a message when attempting to divide by zero.

You may interested in

Creating a beautiful kaleidoscope using Python's turtle Creating a beautiful kaleidoscope using Python's turtle graphics can be a fun and mesmerizing experience. Below is a Python script to draw a kaleidoscope-like pattern using the turtle module:
Easy Python Learning: Calculator script in Python with GUI To create a basic calculator with a graphical user interface (GUI) in Python, we can use the tkinter library. Here's a simple Python script that implements a GUI-based calculator:
Easy Python Learning: Basic calculator script in Python To create a calculator in Python, Below is a simple script that can perform addition, subtraction, multiplication, and division:

Trending

Understanding the IPO Allotment Process and SEBI Guidelines in India Initial Public Offerings (IPOs) are a significant step for companies seeking to raise capital from the public market. Investors, in turn, are keen on participating in IPOs to gain from potential listing gains and long-term growth. However, the allotment process can be complex, and the Securities and Exchange Board of India (SEBI) has laid down comprehensive guidelines to ensure fairness and transparency. This article explores the IPO allotment process and SEBI’s regulations governing it.
Is USA heading towards recessions? What data says ? While the U.S. economy continues to grow, certain indicators, such as the rising unemployment rate and increased recession probabilities from reputable sources, suggest caution. The situation remains fluid, and close monitoring of economic indicators is essential to assess the potential for a recession in the near future.
Reason behind Indian market fall what investor should do In a volatile market like today's, investors can adopt a strategic and balanced approach to protect their portfolios while seeking long-term growth opportunities. Here are some key actions to consider:
What is an IPO and How It Works? An Initial Public Offering (IPO) is one of the most significant events in the financial world, marking a company's transition from being privately held to publicly traded. For businesses looking to raise capital, an IPO offers a unique opportunity to tap into a wide pool of investors. But for potential investors, an IPO can be an exciting opportunity to buy shares in a company at the early stages of its public market journey. In this blog, we'll break down what an IPO is, how it works, its significance, and the various aspects surrounding this process.
Why Financial Education is More Important Than Academic Education and Why It’s Missing in the Indian Education System Despite the growing importance of managing money and understanding personal finance, most educational systems, including India's, have largely overlooked this essential skill. Financial education equips individuals with the knowledge to manage money effectively, make informed decisions, and plan for a secure future.
Understanding Authentication Methods: OTP, TOTP, Passwords, PINs, Patterns, and More in 2025 In today’s digital age, safeguarding personal and organizational data is paramount. To achieve this, various methods of authentication are used to ensure that only authorized individuals can access sensitive information or services. Among these methods are One-Time Passwords (OTP), Time-Based One-Time Passwords (TOTP), Passwords, PINs, and Patterns. Each of these authentication techniques has distinct characteristics, use cases, and security considerations.