Python Addition: Add 2 numbers in python using function

Here is the code to add 2 numbers in python using function- 

In this program, you will learn to add two numbers and display it using print() function.

Given two numbers num1 and num2. The task is to write a Python program to find the addition of these two numbers.

In the below program to add two numbers, the user is first asked to enter two numbers and the input is scanned using the input() function and stored in the variables number1 and number2. Then, the variables number1 and number2 are added using the arithmetic operator + and the result is stored in the variable sum.

Below is the Python program to add two numbers:

num1 = 2
num2 = 3

sum = 2+3

Code:
def sum(a,b):
    return a+b
a = 2b = 3res = sum(a,b)
print(res)

Output: 
5

Output = 5



To Format you can use below code - and output will be as :

Code:
num1 = 20num2 = 30
# Adding two nossum = num1 + num2

# printing valuesprint("Sum of {0} and {1} is {2}".format(num1, num2, sum))

Output: 
Sum of 20 and 30 is 50





Comments