How to Write a Python Program to Add Two Numbers

python program to add two numbers

One of the most basic operations in programming is adding two numbers, and Python provides a simple and efficient way to do this. In this article, we will explain how to write a Python program to add two numbers, and provide code examples to illustrate its usage.

Basic Syntax for Adding Two Numbers in Python

The basic syntax for adding two numbers in Python is:

result = num1 + num2

Here, num1 and num2 are the two numbers that we want to add, and result is the variable that will store the sum of these two numbers. We can use this syntax to add any two numbers in Python.

Example 1: Adding Two Numbers in Python

Let’s start with a simple example. We want to add two numbers, 5 and 10. Here’s how we can do it in Python:

num1 = 5
num2 = 10
result = num1 + num2
print("The sum of", num1, "and", num2, "is:", result)

In this example, we first define two variables, num1 and num2, and assign them the values 5 and 10, respectively. Then, we add these two numbers using the + operator, and store the result in the variable result. Finally, we print out the result using the print() function.

The output of this program will be:

The sum of 5 and 10 is: 15

Example 2: Adding Floating-Point Numbers in Python

In Python, we can also add floating-point numbers, which are numbers with fractional parts. Here’s an example:

num1 = 3.14
num2 = 2.71
result = num1 + num2
print("The sum of", num1, "and", num2, "is:", result)

In this example, we define two floating-point numbers, num1 and num2, and assign them the values 3.14 and 2.71, respectively. Then, we add these two numbers using the + operator, and store the result in the variable result. Finally, we print out the result using the print() function.

The output of this program will be:

The sum of 3.14 and 2.71 is: 5.85

Example 3: Adding Complex Numbers in Python

Python also supports complex numbers, which are numbers with both real and imaginary parts. Here’s an example:

num1 = 2 + 3j
num2 = 4 - 2j
result = num1 + num2
print("The sum of", num1, "and", num2, "is:", result)

In this example, we define two complex numbers, num1 and num2, and assign them the values 2+3j and 4-2j, respectively. Then, we add these two numbers using the + operator, and store the result in the variable result. Finally, we print out the result using the print() function.

The output of this program will be:

The sum of (2+3j) and (4-2j) is: (6+1j)

Example 4: Adding Numbers Entered by the User

In many cases, we may want to add numbers that are entered by the user at runtime. We can do this using the input() function, which allows the user to enter a value from the keyboard. Here’s an example:

num1 = float(input("Enter the first number: ").strip())
num2 = float(input("Enter the second number: ").strip())
result = num1 + num2
print("The sum of", num1, "and", num2, "is:", result)

In this example, we use the input() function to get two numbers from the user. We strip any leading or trailing white space using the .strip() function before converting these values to floating-point numbers with the float() function, and assign them to the variables num1 and num2, respectively. Then, we add these two numbers using the + operator, and store the result in the variable result. Finally, we print out the result using the print() function.

When we run this program, it will prompt the user to enter two numbers, and then it will display the sum of these two numbers.

Example 5: Adding Numbers Using a Function

In Python, we can also define a function to add two numbers, which can be useful if we need to perform this operation multiple times in our program. Here’s an example:

def add_numbers(num1, num2):
    result = num1 + num2
    return result

num1 = 5
num2 = 10
sum = add_numbers(num1, num2)
print("The sum of", num1, "and", num2, "is:", sum)

In this example, we define a function called add_numbers() that takes two arguments, num1 and num2. Inside the function, we add these two numbers using the + operator, and store the result in the variable result. Then, we use the return statement to return the value of result to the caller.

Outside the function, we define two variables, num1 and num2, and assign them the values 5 and 10, respectively. Then, we call the add_numbers() function, passing in num1 and num2 as arguments. The function returns the sum of these two numbers, which we store in the variable sum. Finally, we print out the result using the print() function.

The output of this program will be:

The sum of 5 and 10 is: 15

Conclusion

In this article, we have explained how to write a Python program to add two numbers using the + operator. We have provided several code examples to illustrate its usage, including adding integers, floating-point numbers, and complex numbers, as well as adding numbers entered by the user and using a function to add numbers. By following these examples, you should now be able to write your own Python programs to add numbers, and understand the basic syntax and concepts involved.