# Python Program to Add Two Numbers

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

Examples:

`Input: num1 = 5, num2 = 3Output: 8Input: num1 = 13, num2 = 6Output: 19`

### Simple program to add two numbers

Here num1 and num2 is a variable and we are going to add the both variable with + operator.

## Python3

 `# Python3 program to add two numbers` `num1 ``=` `15` `num2 ``=` `12`   `# Adding two nos` `sum` `=` `num1 ``+` `num2`   `# printing values` `print``(``"Sum of"``, num1, ``"and"``, num2 , ``"is"``, ``sum``)`

Output:

`Sum of 15 and 12 is 27`

### Adding two numbers with user input

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

## Python3

 `# Python3 program to add two numbers`   `number1 ``=` `input``(``"First number: "``)` `number2 ``=` `input``(``"\nSecond number: "``)`   `# Adding two numbers` `# User might also enter float numbers` `sum` `=` `float``(number1) ``+` `float``(number2)`   `# Display the sum` `# will print value in float` `print``(``"The sum of {0} and {1} is {2}"` `.``format``(number1, ` `                                            ``number2, ``sum``))`

Output:

`First number: 13.5 Second number: 1.54The sum of 13.5 and 1.54 is 15.04`

## Python3

 `#To define a function that take two integers ` `# and return the sum of those two numbers` `def` `add(a,b):` `  ``return` `a``+``b`   `#initializing the variables` `num1 ``=` `10` `num2 ``=` `5`   `#function calling and store the result into sum_of_twonumbers` `sum_of_twonumbers ``=` `add(num1,num2)`   `#To print the result` `print``(``"Sum of {0} and {1} is {2};"` `.``format``(num1,` `                           ``num2, sum_of_twonumbers))`

Output

```Sum of 10 and 5 is 15;

```

Initialize two variables num1, and num2. Find sum using the operator.add() by passing num1, and num2 as arguments and assign to su. Display num1,num2 and su

## Python3

 `# Python3 program to add two numbers`   `num1 ``=` `15` `num2 ``=` `12`   `# Adding two nos` `import` `operator` `su ``=` `operator.add(num1,num2)`   `# printing values` `print``(``"Sum of {0} and {1} is {2}"` `.``format``(num1,` `                                       ``num2, su))`

Output

```Sum of 15 and 12 is 27

```

### Adding two number using lambda function

Here we are going to use Python Lambda function for adding two number with Python

## Python3

 `# Define a lambda function to add two numbers` `add_numbers ``=` `lambda` `x, y: x ``+` `y`   `# Take input from the user` `num1 ``=` `1` `num2 ``=` `2`   `# Call the lambda function to add the two numbers` `result ``=` `add_numbers(num1, num2)`   `# Print the result` `print``(``"The sum of"``, num1, ``"and"``, num2, ``"is"``, result)`

Output:

`The sum of 1 and 2 is 3`

### Python program to add two numbers with recursive function

This code defines a recursive function to add two numbers by incrementing one number and decrementing the other. User inputs two numbers, and the sum is calculated and displayed.

## Python3

 `# Define a recursive function to add two numbers` `def` `add_numbers_recursive(x, y):` `    ``if` `y ``=``=` `0``:` `        ``return` `x` `    ``else``:` `        ``return` `add_numbers_recursive(x ``+` `1``, y ``-` `1``)`   `# Take input from the user` `num1 ``=` `1` `num2 ``=` `2`   `# Call the recursive function to add the two numbers` `result ``=` `add_numbers_recursive(num1, num2)`   `# Print the result` `print``(``"The sum of"``, num1, ``"and"``, num2, ``"is"``, result)`

Output:

`The sum of 1 and 2 is 3`

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next