Home » Programming » Python Program to Add Two Numbers

# Python Program to Add Two Numbers In this post, we will be investigating the Python Program to Add Two Numbers. We will add two numbers and clarify the linguistic structure simply known as the syntax of the program.

## Algorithm to add two numbers

Here, we will use variables “num1” and “num2” to store values of two numbers. We also need a third variable named “sum” to store the final value of addition of num1 and num2.

We will make use of one among the arithmetic operators available in Python and that is (`+`) addition operator.

``````Step 1: Start
Step 2: Take two inputs in variable num1 and num2
Step 3: Add num1 and num2 and assign it to sum i.e. sum = num1 + num2
Step 4: End``````

New to Python Programming? Get to know Python Programming by Clicking Here!

## Add Two Numbers in Python

Focusing on the Python program to add two numbers, let’s take us two examples; one with the constants and one with the variables (user input).

### Example 1:

```num1 = 5.2
num2 = 2.5

sum = num1 + num2

# Display Sum Type 1
print("Sum of", num1, "and", num2, "is", sum)

# Display Sum Type 2
print("Sum of {0} and {1} is {2}".format(num1, num2, sum))```

The output for the above code snippet is,

``````Sum of 5.2 and 2.5 is 7.7
Sum of 5.2 and 2.5 is 7.7``````

### Example 2:

The program below computes the sum of two numbers entered by the user.

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

sum = float(num1) + float(num2)

# Display Sum Type 1
print("Sum of", num1, "and", num2, "is", sum)

# Display Sum Type 2
print("Sum of {0} and {1} is {2}".format(num1, num2, sum))```

The output for the above code snippet will be,

``````Sum of 5.2 and 2.5 is 7.7
Sum of 5.2 and 2.5 is 7.7``````

## Explanation:

In the first example, we simply add the two numbers with the constant values as; num1 with the value 5.2 and num2 with the value 2.5 and finally displays the sum of two numbers.

In the second example, we brief the user to enter two numbers and the program lastly shows the amount of two numbers entered by the user.

We used the built-in function `input()`function to take the input from the user. Since, `input()` function returns a string, we need to convert the string into number format using the `float()`function.

Finally, the numbers (num1 and num2) are added, stored in the sum variable and displayed on the screen.

In order to print some statements we can use the `print()`function which may vary according to the need.

You can also check —>