Write a Python program to print the sum of two numbers.

In mathematics, summation is the addition of a sequence of numbers; the result is their sum or total. The numbers to be summed may be integers, rational numbers, real numbers, or complex numbers.

**Program 1: Add two numbers in python**

1 2 3 4 5 6 7 8 9 10 |
num1 = 10 num2 = 8.5 # Add two numbers sum = int(num1) + float(num2) # Result Sum print('{0} + {1} = {2}'.format(num1, num2, sum)) |

With the assignment operators added, this code will correctly calculate the sum of the two variables “num1” and “num2” and print the result to the console.

Here’s a brief explanation of how the code works:

- “num1” and “num2” are defined and given values. “num1” is an integer and “num2” is a float.
- The sum of the two variables is calculated by first casting “num1” to a float and then adding it to “num2”. This ensures that the result of the calculation is a float.
- The sum is printed to the console using string formatting. The “{0}, {1}, {2}” placeholders are replaced with the values of “num1”, “num2”, and “sum”, respectively.

**Program 2: Add two numbers entered by user in Python**

1 2 3 4 5 6 |
num1 = input('Enter #1 : ') num2= input('Enter #2 ') sum=float(num1)+float(num2) print("SUM:{0} ".format(sum)) |

This code prompts the user to enter two numbers and then calculates the sum of the two numbers. The sum is printed to the console.

Here’s a brief explanation of how the code works:

- The “num1” and “num2” variables are assigned the values entered by the user.
- The sum of the two variables is calculated by casting them to floats and adding them together.
- The sum is printed to the console using string formatting. The “{0}” placeholder is replaced with the value of “sum”.

[…] Add Two Numbers in Python […]