Menu Close
Python Program To Add Two Numbers

In this article, we are going to write a Python program to add two numbers. Here we will see two ways to add two numbers first we assigned value to the variable and second, we will ask for value from the user and then add.

To understand this example you should have basic knowledge of Python Programming.

How to add two numbers using Python?

Here we will add two numbers using two different ways, First is assigned by default value and the second is asked for value from the user.

Example 1:

In this example, we have two values and a and b, and use the + operator to add these values.

# Assigned by default values to both a and b variables.
a = 12.12
b = 20.12

result = a + b
print("Output:- ", result)

Output

#Output of the above program will be:
Output:- 32.24

In the above program, we defined two variables a and b, and added both the variables using the + operator.

Example 2:

In this example, we use the input() function to take input from the user and then convert that input to a floating-point number using the float() function. After converting we + operator to add two values.


#Ask a and b value from the user using input() function
a = float(input("Enter first number:- "))
b = float(input("Enter second number:- "))

# Add two numbers
result = a + b
print("Output:- ", result)

Output

#Output of the above program will be:
Enter first number:- 12.30
Enter second number:- 12.40
Output:- 24.70

Conclusion

In this tutorial, you have learned how to add two numbers in two different ways. I hope this article will help you, I think you don’t have any confusion regarding How to add two numbers using python. If you like this article, please share it with your friends who want to learn Python programming from scratch to advanced.

For More Information:- Click Here

Previous Article
Next Article

Related Posts