Add Two Numbers - Python Program
Python - Add Two Numbers
You can add two numbers in Python using Arithmetic Addition Operator +
. The addition operator accepts two operands and returns the result of addition.
The numbers in Python could be of datatype int, float or complex. We can take values of same datatype or combination of any of the Python supported numeric datatypes: int, float, and complex; to compute the sum.
Syntax
The syntax of arithmetic addition operator to find the sum of given two operands is is
result = operand1 + operand2
+
operator adds the numbers operand1, operand2; and returns the result. In the above syntax, we are storing the result in a variable named result.
To demonstrate this operation quickly, you may open Python Shell and run the addition operation as shown below.
Python Shell
>>> 25 + 63
88
>>> a = 32
>>> b = 87
>>> a + b
119
You have to make sure that the datatype of the variables or operands is numeric i.e., int, float or complex.
Examples
1. Add given two integer numbers
In the following example, we will take two numbers of integer datatype and add these two numbers.
Python Program
a = 1
b = 6
# Add two integer numbers
sum = a + b
# print the sum to console
print(sum)
Explanation
- Two integer variables,
a
andb
, are initialized with values1
and6
, respectively. - The
+
operator is used to adda
andb
, and the result (7
) is stored in the variablesum
. - The
print()
function is used to display the value ofsum
on the console.
Output
7
The datatype of both a
and b
is int. Therefore, the datatype of the result, sum
, is also int.
2. Add given two floating numbers
In the following example, we add two numbers of datatype float. The returned value will be of type float.
Python Program
a = 1.5
b = 6.3
# Add two floating numbers
sum = a + b
# Display the sum
print(sum)
Explanation
- Two floating-point numbers,
a
andb
, are initialized with the values1.5
and6.3
, respectively. - The
+
operator is used to adda
andb
, and the result is stored in the variablesum
. - The
print()
function is used to display the value ofsum
, which is the sum of the two numbers, on the console.
Output
7.8
3. Add an Int and Float
In the following example, we have a variable a
with data of type float and another variable b
holding data of type int. We will add these two numbers using +
operator.
Python Program
a = 1.5
b = 6
# Add a float and an int
sum = a + b
# Display the sum
print(sum)
Explanation
- Two variables are initialized:
a
as a floating-point number (1.5
) andb
as an integer (6
). - The
+
operator is used to adda
andb
. Python automatically performs type conversion (implicit typecasting) to add the float and integer, resulting in a float. - The sum is stored in the variable
sum
. - The
print()
function is used to display the result of the addition, which is7.5
, on the console.
Output
7.5
As already mentioned, the type of variable a is float and the type of variable b is int. When you add two numbers with different datatypes, the lower type is promoted to higher datatype, and this promotion happens implicitly. As in here, when you add int and float, int is promoted to float. The higher datatype of the operands becomes the datatype of result.
4. Add two given complex numbers
Python supports complex numbers. In this example, we take two complex numbers and find their sum.
Python Program
a = (1+8j)
b = (6+4j)
# Add two complex numbers
sum = a + b
# Display the sum
print(sum)
Explanation
- Two complex numbers are initialized:
a
as(1+8j)
andb
as(6+4j)
. In Python, complex numbers are represented asreal + imaginary j
. - The
+
operator is used to add the two complex numbers, combining their real parts and imaginary parts separately. In this case, the real parts (1 + 6
) and the imaginary parts (8 + 4
) are added. - The resulting complex number,
(7+12j)
, is stored in the variablesum
. - The
print()
function is used to display the result of the addition, which is(7+12j)
, on the console.
Output
(7+12j)
In complex number addition, the real parts of the two numbers get summed up and the imaginary parts of the two numbers gets summed up.
Summary
In this tutorial of Python Examples, we learned how to add two numbers that belong to same or different numeric datatypes in Python.