Python

# How to Add Numbers in Python

One of the basic programs that every newcomer encounters as a would-be Python programmer is adding or summing two numbers. After going through the basic concepts like Python datatypes, variables, and arithmetic operators in Python, adding two values/numbers is the best approach to implement them. In Python, the arithmetic addition operator (+) is used to carry out the addition operation. When two operands are given to this operator, their sum is returned. This is because Python provides a wide variety of numeric data types. The two numeric values that require to be added may be of the same data type or may be of two different numeric data types such as float, int, and complex numbers. We’ll demonstrate how to add or find the sum of two numbers in Python in this tutorial.

## How to Add Two Numbers in Python

Python’s addition operator is +. It is used to add two operands/values. It requires the following syntax to be used each time you want to add two numbers:

Syntax: sum = number1 + number 2

In the syntax, the addition operator “+” performs the addition computation and returns the result. The outcome of the calculation must be saved in a variable so we can print it. The “sum” variable is used in the syntax.

Let’s first add two numbers without storing the sum of numbers in a variable.

Code:

12 + 8

Output: The result of the addition of numbers can be stored in a variable like “sum” for later use rather than simply adding the numbers directly.

Code:

num1 = 5
num2 = 9
sum = num1 + num2
print(sum)

Output: Now, let’s explore the results from adding the numeric values of the same and different datatypes.

The first variant of adding two numeric values in Python involves adding numbers that belong to the same data type. In this instance, we add two integer datatype values (int + int). The output of adding two int is also an integer:

Code:

n1 = 2
n2 = 13

print ("Datatype of n1 :",(type(n1)))
print ("Datatype of n2:",(type(n2)))

sum = n1 + n2

print("Sum :",sum)
print("Datatype of Sum:", type(sum))

Output: Two variables, n1 and n2, are defined with values of 2 and 13, respectively in the top two lines of the code. Second, we use the type() function to print the datatypes of n1 and n2. Both are integers in this example. The numbers that are stored in n1 and n2 are then added with the help of “+” operator. The result is saved in a “sum” variable. We then display our final result along with the data type of the “sum” variable. The data types of the variables n1 and n2 are both int. As a result, the sum is also an int datatype.

Similar to adding two integers, two floating point values can also be added and the result is also a floating number. This Python example demonstrates how to add two floating-point numbers.

Code:

f1 = 5.2
f2 = 6.4
print ("Datatype of n1 :",(type(f1)))
print ("Datatype of n2:",(type(f2)))
float_sum = f1 + f2
print("Sum :", float_sum)
print("Datatype of Sum:", type(float_sum))

Output: Two variables, f1 and f2, are created and their values are defined as 5.2 and 6.4, respectively at the start of the code. Then, we use the type() function to print the datatypes of f1 and f2. Both are floats in this instance. The values that are contained in variables f1 and f2 are then added using the “+” operator. The result is saved in a “float sum” variable. We then print our final result along with the data type of the “float_sum” variable. The outcome is a float because both variables are floats.

Two numbers (real and imaginary) are combined to generate the complex numbers. Typically, “i” is used to represent the real part and “j” is used to represent the imaginary part. The “x + yj” syntax is used to represent a complex number. Let’s add two complex numbers:

Code:

c1 = (5+3j)
c2 = (7+9j)
print ("Datatype of c2: ",(type(c1)))
print ("Datatype of c2: ",(type(c2)))
com_sum = c1 + c2
print("Sum: ",com_sum)
print("Datatype of Sum: ", type(com_sum))

Output: First, two variables, c1 and c2, are created and their values are specified as 5+3j and 7+9j, respectively. Second, we use the type() function to print the datatypes of c1 and c2. Both numbers are complex in this example. The numbers that are stored in the variables c1 and c2 are then added using the “+” operator. The result is stored in a new variable named “com_sum”. The “com sum” variable along with its datatype is printed. Our result has a complex data type since both variables were of complex data type.

Adding the numeric values with different data types is the second variation. In this instance, we add the int and float values. The lower data type is implicitly promoted to the higher data type when two values of different data types are added. The higher datatype of the operands determines the resultant value’s datatype.

Here is an example that demonstrates the addition of integer and float using the implicit type conversion:

Code:

f = 17.3
i = 29
print ("Datatype of f: ",(type(f)))
print ("Datatype of i: ",(type(i)))

sum = f + i

print("Sum: ",sum)
print("Datatype of Sum: ", type(sum))

Output: Two variables, f and i, are specified with values 17.3 and 29, respectively. Then, the type() method is used to print the datatypes of f and i. In this example, “i” is an integer and “f” is a float. The values that are contained in variables f and i are then added using the “+” operator. Finally, we print the output sum along with its datatype. The float datatype is returned because the float is a higher datatype than int.

We use the implicit type conversion in this example. We can also use the explicit type conversion to perform the same task. A type conversion that is explicitly defined inside a program is known as explicit type conversion (instead of being performed by a compiler like in implicit type conversion). When converting the data types using built-in functions like float(), int(), float(), str(), etc., the explicit type conversion requires the user input. Let’s try the explicit type conversion in the following illustration:

Code:

f = 8.6
n= 4
print("Datatype of f:",type(f))
print("Datatype of n:",type(n))
n = float(n)
print("Type of n after casting:",type(n))
n = f + n
print("Sum :",n)
print("Datatype of Sum :",type(n))

Output: We create two variables, f and n, and set their values as 8.6 and 4, respectively. Then, we use the type() method to print the datatypes of n and f variables. In this instance, n is an int and f is a float. The float() function is then used for the explicit conversion of the n value to a float. The type() method is used to verify that the datatype of n is also a float. Like what we did in the previous example, we add the numbers together using the “+” operator and then store the output of the addition in the “sum” variable . Finally, we print our output and the datatype of the output. The type of the variable f is a float and the type of the converted variable n is also a float, resulting in a float value.

## Adding Numbers with User Input

We can even take the input values from the user instead of specifying them ourselves.

Code:

n1 = input('Enter n1: ')
n2 = input('Enter n2: ')

sum = float(n1) + float(n2)
print(sum)

Output: The program asks for an input from the user when we execute the previous code. We take the input() using the built-in input() function. Since the input() returns a datatype string, we convert the string to a numeric datatype using the float() function. The sum of the two integers is then printed.

## Conclusion

In this post, we learned the syntax that must be used to add two numbers in Python. We also implemented a few examples to teach you how to add two complex numbers, two floats, and two integers. Both implicit and explicit type conversions are used to add the int and float values. Finally, we implemented a program to add the two values using the user input. We hope that you now understand how to add the numbers in Python by yourself using the “+” operator. 