Skip to content

7 Easiest Ways to Write Python Program to Add Two Numbers

Python program to add two numbers

In this article, you’ll see all the possible ways to write a python program to add two numbers.

So, Let’s get started!

1. Python program to add two numbers using plus (+) operator:

One of the most easiest way to write a program to add two numbers in python is just by using the + operator.

In Python, you can directly add two operands (i.e., values or variables) with the help of plus (+) operator.

>>> 3 + 8
11
>>> a = 1   #assigning (storing a value to a variable) a to 1
>>> b = 4   #assigning b to 4
>>> a + b   #adding a and b with the help of + operator
5
>>>|                        

In the above code, we have used two variables a and b as operands and to add these variables we are using + operator.

First of all, we have assigned two different values to a and b. You can also assign a single value to two different variables.

After that, we are adding these two variables with the help of + operator which will gives us the output as 5.

2. Python program to add two numbers with user input:

num1 = int(input("Enter first number: "))      
num2 = int(input("Enter second number: "))      
sum = num1 + num2                                
print("Sum of",num1, "and",num2, "is:",sum)
        

Output:

Enter first number: 1234
Enter second number: 4321
Sum of 1234 and 4321 is: 5555

Explanation of the above code:

As you can see in the above example, we ask the user to enter two input values and we assign (store) that two input values entered by the user in two different variables num1 and num2.

To take the input values from the user we use a built-in (predefined) function called input().

What does input() function do?

The input() function basically takes input value entered by the user and returns that value as a string.

Which means, If you enter any type of data (value), then the input() function always returns the string format of that entered value.

But, One question arises here,

How will you know that when you have to enter your input values.

You’ll know by seeing some message (text) onto your computer screen, such as:Enter your value“, etc.

And, by using the input() function you can also display a message onto your computer screen by passing prompt (string) inside the parentheses of input function, here the prompt must be inside either single or double quotes.

There is a specialty about input() function.

And, the specialty is that, if you want to change the default return type (i.e., string type) of this function, then you can do it easily just by writing the name of the type you want before the input() function.

As we have done in the above code.

We have changed the type of the input value into integer type just by writing the int keyword before input() function. Like this: int(input("Enter the first value: ")).

And, we have assigned the input values taken by the user to two different variables named num1 and num2 so that we can access or use the entered value by the user to calculating the sum.

After that, We are adding “num1” and “num2” using + operator and assigning it to a new variable named “sum”.

And, at last, we are displaying the result i.e., sum of num1 and num2 using the print() function.

3. Python program to add two numbers using function:

def add(a,b):
    return(a+b)  

Output:

add(9,11)
20

Explanation of the above code:

In python or in any programming languages, If you would like to work with functions to perform a specific task, the first thing you need to do is to define a function.

And, To define a function you must declare the function first.

? Note: A function declaration tells the interpreter about a function’s name, It’s arguments, and the return type whereas a function definition provides the actual body of the function along with the declaration of the function.

The way we declare a function is just by using the def keyword follow up with the function name.

Here, we are giving the function name as “add”. You can use any name for your function, It’s upon you.

But, for simplicity and to make it recognizable for any user who works on this function I’m using “add” here.

After that, We are passing (adding) the parameters (arguments) to the function: they should be within the parentheses of the function name. Parameters are simply the variables which we will be using within our function. End your line with a colon (:) (It is compulsory for a function definition in Python).

And, at last we are returning the sum of a and b with the help of return keyword.

The return keyword in Python is used to end the execution of a function and “returns” the result (value of the evaluated expression inside the parentheses of return keywords) to the caller (where we call the function). If you don’t pass any expression inside the parentheses of return keyword, then the special value None is returned. 

So, whenever we call the add function along with the parameters a and b then it will always return the sum of a and b in this case.

4. Python program to add two numbers using function and taking input from the user:

def sum():
    x = int(input("Enter first number: "))
    y = int(input("Enter second number: "))
    z = x + y
    print("The sum of two numbers is: ", z)

Output:

sum()
Enter first number: 147
Enter second number: 258
The sum of two numbers is: 405

Explanation of the above code:

The above code is self explantory, because we have already seen in our previous cases, how to define a function and how to take input values from the user.

The only thing to keep in mind here is, In the above code we are not passing any parameter at the time of function definition because, Here we are expecting the values from the user and to use that values we are assigning that values to two different variables (i.e., x and y).

5. Python program to add two numbers using lambda function:

f = lambda x,y:x+y
f(2,3)

Output:

5

Explanation of the above code:

A lambda function is an anonymous function (function without any name).

As we already know, the def keyword is used to define a function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python. 

This function can take any number of arguments (parameters), but can only have one expression.

The expression is executed and the result is returned back to the function object.

Syntax of lambda function:

lambda arguments : expression

So, as you can see in the above code example x, y are the arguments, x+y is the expression and the variable “f” is the function object in which we are assigning the complete statement (i.e., lambda x,y:x+y).

? Note: You can use any name for your function object. It’s upon you. For simplicity I’m using “f” here.

Now, One more question arises here: Why we are assigning the whole statement to the function object f. Right?

After doing this, We will be able to access or use this f to calculate the sum of two arguments x and y by simply passing the arguments inside the parentheses of f and calling it.

And, To calculate the sum of 2 and 3 we are calling the function object f by passing the argument 2, 3 inside the parentheses of f.

Which will gives us 5 as an output.

6. Python program to add two numbers without using any variables and only in a single statement:

print("The sum of two numbers is: %.1f" %(float(input("Enter first number: "))+float(input("Enter second number: "))))

Output:

Enter first number: 7
Enter second number: 8
The sum of two numbers is: 15.0

The above program is more memory efficient than the rest of the other examples, as it is not using any variable.

But, it’s not quite readable.

7. Python program to add two numbers without using + operator:

def add(a,b):
	if a!=b:
		return(a*a-b*b)//(a-b)
	else:
		return(2*a)

Output:

add(3,9)
12

add(5,5)
10

Explanation of the above code:

In the above example, we are using if-else statement for decision making.

So, In the statement 2nd (i.e., if statement) is used to check if a is not equal to b then, we wish to execute the statement 3rd (i.e., return(a*a-b*b)//(a-b)) else we wish to execute the statement 5th (i.e., return(2*a)).

And, all the operations between the operands are very straightforward as we are just using some simple python operators.


Conclusion:

Congratulation! ?

In this article, you learned how to write a python program to add two numbers in all the possible ways along with detailed explanation.

I really hope that you liked my article and got a lot of value from it.

Now, It’s your turn!

If you have any question, feel free to ask in the comment section below right now.

And, and, and…

Don’t forget to share this article to your lovely friends.

Sharing is Caring! ?


Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *