Python program to add two numbers
In this post, you will learn different ways to write Python programs to add two numbers.
The addition of numbers is the most basic operation that is applicable in most of the programs. Such type of programming can improve your logical skills and make us perfect for coding challenges. There are many approaches in Python to perform addition operation.
Python program to add two numbers using + operator
In the given Python program, we have used the (+) operator to add two numbers.
# Python program to add two numbers
num1 = 1200
num2 = 232
# Using + operator to add two numbers
sum = num1 + num2
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Output of the above code-
The sum of 1200 and 232 is 1432
Python program to add two numbers using user inputs
In the given Python program, the user is first asked to enter two numbers. We have stored the numbers in two variables and added them using the arithmetic plus (+) operator, and the result is stored in the variable sum. As the input() function returns a string, so we need to convert the string into a number using the float() function.
# Python program to add two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# adding two numbers
sum = num1+ num2
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Output of the above code-
Enter the first number: 212
Enter the second number: 42312
The sum of 212.0 and 42312.0 is 42524.0
Python single statement to add two numbers
In the above programs, we have used multiple lines of code to get the sum of two numbers. But, we can perform this operation in just a single statement without using any variables as follows.
print('The sum is %.1f' %(float(input('Enter first number: '))+float(input('Enter second number: '))))
Output of the above code-
Enter first number: 210
Enter second number: 3
The sum is 213.0
Python program to add two numbers using lambda function
Lambda is an anonymous function that contains any number of arguments, but only one expression. As we know, the def keyword is used to define a function. Similarly, the lambda keyword is used to define an anonymous function in Python. In this, the single expression is executed and the result is returned back to the function object. In the given program, we calculate the sum of 98 and 39 by calling the lambda function num_addition.
# Python3 program to add two numbers
# using lambda function
# Driver Code
if __name__ == "__main__" :
num1 = 98
num2 = 39
# Adding two numbers
num_addition = lambda num1, num2 : num1 + num2
# printing values
print("Sum of {0} and {1}: {2}" .format(num1, num2, num_addition(num1, num2)))
Output of the above code:
Sum of 98 and 39: 137
Related Articles
Python program to print multiplication table from 1 to 10
Multiply all elements in list Python
Python Check if a Number is Odd or Even
Python program to multiply two numbers
numpy dot product
Prime factors of a number in Python
Find square of a number in Python
How to find average of n numbers in Python
Python program for Prime Number
Power of a number using recursion in Python
Sum of n numbers in python using for loop
Sum of n numbers in Python using while loop
Swapping of two numbers in Python
Python iterate list with index
Python add list to list
Python random choice
Python check Palindrome strings and numbers
Remove character from string Python
Python raise keyword