Addition of Two Numbers in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

Adding two numbers means computing the sum of two numbers or making simple addition of two numbers. Numbers could be integer, float or complex.

Scope

In this article, we will learn how to add two numbers of different forms in Python.

Introduction

As an aspiring Pythoneer, adding two numbers in Python is one of the rudimentary programs that every beginner comes across. After learning the fundamental concepts of variables, Python data types, and Python arithmetic operators, adding two numbers is the best way to implement them. The addition operation is performed using the arithmetic addition operator, '+'. This operator accepts two operands and returns their sum.

There can be various variations in adding two numbers in Python. This is due to the reason that Python supports many numeric data types.

The two numbers that need to be added can be the same numeric datatype or a combination of two distinct numeric data types among int, float, and complex numbers.

Another concern is can the two operands be taken as input from the user, or do they always need to be hardcoded? In this article, we will look at both of these cases.

Syntax for Adding Two Numbers in Python

The following syntax must be followed each time for adding two numbers in Python.

Explanation:

In the syntax, the ‘+’ operator is responsible for computing the addition and returning the result. Post the calculation, the result needs to be stored in a variable so that we can print it. For this, we have used the variable called ‘sum’.

Example of adding two numbers in Python

Example 1: We can directly add numeric values without storing them in any variable, as shown.

Code:

Output:

Example 2:

Instead of directly adding, we can store the value of our addition in a variable just like “sum” for later use.

Code:

Output:

If you run these codes, you will get the output as 126 and 100, respectively, as shown.

Addition of two Integers in Python

This is the first variation of adding two numbers in Python, which is the addition of numbers of the same numeric data type. In this particular case, we will be adding two numbers that are of integer data type, that is, int + int. When we add two integers, we get the output as an integer.

Code:

Output:

Explanation:

  • In the first two lines of code, we have taken two variables, a and b and have predefined their values as 1 and 6, respectively.
  • Secondly, we have printed the datatypes of a and b using the type() function. In this case, both are integers.
  • Next, we are adding the values stored in variables a and b using the ‘+’ operator and storing the result of the addition in a new variable called ‘sum’.
  • Finally, we print our result, the ‘sum’ variable and its data type. The variables a and b are both of the same data type int. Therefore, the sum's result also belongs to the int data type.

Addition of Two Floating Numbers in Python

Like integer addition, we can also add two floating numbers, and the resultant will be a floating point number itself. This example shows the addition of two floating numbers in Python.

Code:

Output:

Explanation:

  • In the first two lines of code, we have taken two variables, x and y, and have hardcoded their values as 8.1 and 9.5, respectively.
  • Secondly, we have printed the datatypes of x and y using the type() function. In this case, both are floats.
  • Next, we are adding the values stored in variables x and y using the ‘+’ operator and storing the result of the addition in a new variable called ‘sum’.
  • Finally, we print our result, the ‘sum’ variable and its data type. Since both variables are floats, the resultant is a float.

Addition of int and float in Python

The second variation is adding numbers with different numeric data types. In this case, we’ll be looking at the addition of int and float numbers.

When two numbers with different data types are added, it implicitly promotes the lower data to the higher data type. When you combine int with float, as in this case, int is promoted to float. The data type of the result is determined by the higher data type of the operands. This conversion of one data type into another comes under the topic type conversion in Python.

Example to show the addition of int and float in Python with implicit type conversion

Code:

Output:

Explanation:

  • In the first two lines of code, we have taken two variables, p and q, and have hardcoded their values as 1.5 and 6, respectively.
  • Secondly, we have printed the datatypes of x and y using the type() function. In this case, p is a float while q is an integer.
  • Next, we are adding the values stored in variables p and q using the ‘+’ operator and storing the result of the addition in a new variable called ‘sum’.
  • Finally, we print our result, that is, the ‘sum’ variable and its data type. The type of variable p is float, and the type of variable q is int, and the data type of result is hence a float.

The same example can be done with explicit type conversion too. Explicit type conversion is a type conversion explicitly defined within a program (instead of being done by a compiler for implicit type conversion). In explicit type conversion, user involvement is necessary for converting the data types with predefined functions like int(), float(), str(), etc.

Let us see how explicit type conversion can be applied to the previous example itself.

Example to show the addition of int and float in Python with explicit type conversion

Code:

Output:

Explanation:

  • In the first two lines of code, we have taken two variables, num_float and num_int and have hard coded their values as 1.5 and 6, respectively.
  • Secondly, we have printed the datatypes of both the variables using the type() function. In this case, num_float is a float while num_int is an integer.
  • Next up, we explicitly convert the value in the num_int to a float using the float() function. Now, the datatype of num_int is also a float that we’ll showcase using the type() function.
  • Following the same, we are adding the values using the ‘+’ operator and storing the result of the addition in a new variable called ‘sum’. Finally, we move on to printing our result, that is the ‘sum’ variable and its datatype as well. The type of variable num_float is float, and the type of variable num_int is also a float after conversion and hence, resulting in a float.

Note: We can also convert num_float into an int and get the resultant as an integer.

Adding Two Complex Numbers in Python

Code:

Output:

Explanation:

  • In the first two lines of code, we have taken two variables, m and n, and have hardcoded their values as 1+8j and 6+4j, respectively.
  • Secondly, we have printed the datatypes of m and n using the type() function. In this case, both are complex numbers.
  • Next, we are adding the values stored in the variable m and n using the ‘+’ operator and storing the result of the addition in a new variable called ‘sum’.
  • Finally, we move on to printing our result, that is, the ‘sum’ variable and its datatype as well. Since both variables are of the same complex data type, hence our result is also of complex data type.

Click Here, To know more about Complex() in Python.

Adding Two Numbers With User Input in Python

Instead of hardcoding the input values every single time, we can even ask the user to enter the input values.

Code:

Output:

Explanation:

  • In the program shown above, we asked the user to enter two numbers num 1 and num 2. To take the input, we used the built-in function: input().
  • Since, input() function returns a string, we used the float() function to convert the string to a number.
  • The numbers are then added together and stored in variable ‘sum’ which is printed to get the resultant addition of two numbers.

Conclusion

  • The addition of two numbers in Python, as stated previously, is one of those programs that cross the path of every beginner. This is because many related concepts such as taking input from the user, various Python numeric data types, combinations among them for addition, storing the summation in a variable, and printing the result get clarified in the process.
  • In this article, we have learnt the syntax that needs to be followed for the addition of two numbers in Python and how to add two integers, two floats, or two complex numbers together.
  • We have walked past the addition of int and float using implicit as well as explicit type conversion. Lastly, we have also tried our hands-on how to take input from the user for the addition of two numbers in Python.

See Also: