Day 1: Calculator App with Python
Tags: 50daysofpythonprojects, 100daysofcode, python, webdev
Published: 2023-04-20 08:56:41

Python is a popular programming language that is widely used for various tasks such as data analysis, web development, and automation. One of the basic tasks that can be performed using Python is arithmetic calculations. In this tutorial, we will be creating a simple calculator program in Python that prompts the user for input, performs arithmetic calculations based on the user's input, and displays the result to the user. This tutorial is aimed at beginners who are new to Python and are looking to learn how to create basic programs in the language. We will be covering the essential Python programming concepts such as functions, user input, conditional statements, and printing output. By the end of this tutorial, you should have a good understanding of how to create a basic calculator program in Python.

## Requirements
Before getting started, you'll need to have Python installed on your machine. You can download and install Python from the official website: [https://www.python.org/downloads/](https://www.python.org/downloads/)

## Step 1: Create and open a new Python file
In this tutorial, we are going to create two files, the first one is for getting input from user and the second is for defining the function. Open a new Python file in your preferred code editor. You can name the file whatever you like. In this example, we'll name the first file main.py and the second one calculator.py.

## Step 2: Define a function for each operation in the `calculator.py` file

We are going to define a function for each of the four arithmetic operations that the calculator will support: addition, subtraction, multiplication, and division in the `calculation.py` file. For example:


```
def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def multiply(num1, num2):
    return num1 * num2

def divide(num1, num2):
    return num1 / num2`
```

## Step 3: Getting input from the user

In Python, we use the `input()` function to get input from the user. Now we are going to use this function to prompt the user to enter two numbers they want to perform the calculation on, and store these numbers in variables. For example:

```
first_number = float(input("Please enter the first number: "))
second_number = float(input("Please enter the second number: "))
```
**Note:** that we convert the user input to a float, since we want to be able to handle decimal numbers.

## Step 4: Get the operation choice from the user
Use the **`input()`** function to prompt the user to select an operation. Store the user's choice in a variable. For example:

```
print("\nPlease select an operation: \n1. Add \n2. Subtract \n3. Multiply \n4. Divide")
choice = input("\nEnter your choice (1/2/3/4): ")
```

## Step 5: Perform the calculation
Use a conditional statement to determine which function to call based on the user's operation choice. For example:

```
if choice == "1":
    result = add(first_number, second_number)
elif choice == "2":
    result = sub(first_number, second_number)
elif choice == "3":
    result = mul(first_number, second_number)
elif choice == "4":
    result = div(first_number, second_number)
else:
    print("Invalid choice")
```

## Step 6: Display the result
Use the **`print()`** function to display the result of the calculation to the user. For example:

```
print("The result is:", result)
```

## Step 7: Run the program
Save the file and run it from the command line using the following command:

```
python3 calculator.py
```

You should see the program prompt you for input and display the result of the calculation based on your input.

Congratulations, you have created a basic calculator program in Python that prompts the user for input and displays the result!

Github repository [50-days-of-python](https://github.com/ademto/50-days-of-python-projects)

- Follow me on twitter [Emmanuel Adetoro](https://twitter.com/agba_software)