Python Programming Examples Tutorial Index
Python Number Programs

Python is often used to create basic programs that perform mathematical calculations. This tutorial describes how to create a simple calculator program using Python, capable of performing arithmetic operations such as addition, subtraction, multiplication, and division.

Here is a simple Python program that creates a basic calculator with the ability to add, subtract, multiply, and divide:

Example:

def calculator():
    while True:
        # Print options for the user
        print("Enter '+' to add two numbers")
        print("Enter '-' to subtract two numbers")
        print("Enter '*' to multiply two numbers")
        print("Enter '/' to divide two numbers")
        print("Enter 'quit' to end the program")
        
        # Get user input
        user_input = input(": ")

        # Check if the user wants to quit
        if user_input == "quit":
            break
        # Check if the user input is a valid operator
        elif user_input in ["+", "-", "*", "/"]:
            # Get first number
            num1 = float(input("Enter a number: "))
            # Get second number
            num2 = float(input("Enter another number: "))

            # Perform the operation based on the user input
            if user_input == "+":
                result = num1 + num2
                print(num1, "+", num2, "=", result)

            elif user_input == "-":
                result = num1 - num2
                print(num1, "-", num2, "=", result)

            elif user_input == "*":
                result = num1 * num2
                print(num1, "*", num2, "=", result)

            elif user_input == "/":
                result = num1 / num2
                print(num1, "/", num2, "=", result)
        else:
            # In case of invalid input
            print("Invalid Input")

# Call the calculator function to start the program
calculator()

Program Output:

Enter '+' to add two numbers
Enter '-' to subtract two numbers
Enter '*' to multiply two numbers
Enter '/' to divide two numbers
Enter 'quit' to end the program
: +
Enter a number: 10
Enter another number: 5
10.0 + 5.0 = 15.0

The above program is a simple calculator written in Python. The program defines a function called "calculator" which contains a while loop that continues until the user inputs "quit".

Inside the while loop, the program prints out options for the user to choose from, such as addition, subtraction, multiplication, and division. It then prompts the user to enter their desired operation using the "input" function.

The program then uses an "if" statement to check if the user input is equal to "quit", in which case it breaks out of the while loop and ends the program. If the user input is not "quit", the program uses another "if" statement to check if the user input is a valid operator (+, -, *, /).

The program prompts the user to enter two numbers using the "input" function if the input is a valid operator. The program then operates based on the user input using an "elif" statement. The result is printed out using the "print" function.

If the user input is not a valid operator or "quit", the program prints "Invalid Input" using the "print" function.

The program calls the "calculator" function at the end, which starts the program.