Contents

## 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. To find the sum, you can take values of same datatype or combination of any of the Python supported numeric datatypes: int, float and complex.

### Syntax

The syntax of arithmetic addition operator 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
```

Run this program You have to make sure that the datatype of the variables or operands is numeric i.e., int, float or complex.

### Example 1: Add 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)
```

Run this program **Output**

`7`

The datatype of both `a`

and `b`

is int. Therefore, the datatype of the result, `sum`

, is also int.

### Example 2: Add 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)
```

Run this program **Output**

`7.8`

### Example 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)
```

Run this program **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.

### Example 4: Add two 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)
```

Run this program **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.