Python Program to Add Two Numbers in Multiple Ways
|In this Python article, we will cover the program to add two numbers in Python.
1. How to Add 2 Numbers in Python?
We use arithmetic operators for numerous operations. Among such arithmetic operators, there exists an addition operator.
This simple addition can be implemented in multiple ways for example by using normal addition, a function, bitwise operator, etc.
Some of the topics which will be helpful for understanding the program implementation better are:
1.1. Addition of Two Numbers using Addition (+) Arithmetic Operator
Let’s add two numbers from the basic method by using the addition operator.
#Add two numbers using + operator num1 = int(input("Enter the First number: ")) num2 = int(input("Enter the Second number: ")) res = num1 + num2 print("The result after adding the given two numbers is: ",res)
Output Enter the First number: 34 Enter the Second number: 56 The result after adding the given two numbers is: 90
In the above program, the user will provide the value for the First number(num1
) and for the second number(num2
) and then the result of the sum will be provided as the output.
1.2. Addition of Two Numbers using Function
Let’s update the previous program to use a Function to add two numbers.
#Add two numbers using + operator def add_numbers(num2,num1): return num1 + num2 num1 = int(input("Enter the First number: ")) num2 = int(input("Enter the Second number: ")) print("The result after adding the given two numbers is: ", add_numbers(num1, num2))
Output Enter the First number: 22 Enter the Second number: 77 The result after adding the given two numbers is: 99
In the above program, we have used the function add_numbers
and specified the position of the arguments in the brackets.
1.3. Addition of Two Numbers Specified by the User
Let’s see a simpler way and direct way of adding two variables; variables value will be self-declared and it will not be taken from the user.
#Add two numbers using + operator num1 = 3 num2 = 6 print(num1 + num2)
Output 9
Let’s add two values using the string .format
method.
#Add two numbers using + operator with .format manner num1 = input("Enter the First number: ") num2 = input("Enter the Second number: ") n1 = float(num1) n2 = float(num2) print("The sum of {0} and {1} is {2}".format(n1, n2, n1+n2))
Output Enter the First number: 3.56 Enter the Second number: 8.76 The sum of 3.56 and 8.76 is 12.32
In the above program, the values were first taken as string input and then typecasted into float values. The {0}
will take the first value in .format
, followed by {1}
will take the second value in .format
and so on.
2. Addition of Two Numbers using Bitwise Operator
We can add two numbers using the Bitwise Operator such as XOR and AND. Bitwise operator converts numbers in bits.
The sum can be obtained using the Bitwise XOR calculation and the carry can be obtained using Bitwise AND calculation. Let’s implement the question using one program.
# Add two numbers using Bitwise Operator def add_two_numbers(n1, n2): if n2 == 0: return n1 else: return add_two_numbers(n1 ^ n2, (n1 & n2) << 1) n1 = int(input("Enter first number: ")) n2 = int(input("Enter second number: ")) print("The sum is:", add_two_numbers(n1, n2))
Output Enter first number: 34 Enter second number: 90 The sum is: 124
Here, in the above program, we have used the recursion method in the defined function.
3. Conclusion
In this article, we discussed how to add two numbers simply using the addition {+} arithmetic operator as well as without using the addition operator i.e. Bitwise Operator.
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?