# Simple Python Program to add Two numbers

Write a Simple Python Program to add two numbers and floating-point values using the Arithmetic Operators with an example.

In this program, we declared two variables a and b of values 10 and 99. Next, this Python program uses the arithmetic operator + to perform addition or find the sum of those two numbers.

```a = 10
b = 99

sum = a + b
print(sum)
print(a +b)```
``````109
109``````

## Simple Python Program to add Two numbers With User Input

This example allows the user to enter two values of string type. Next, this Python program will convert them to floating-point numbers and add those two and assign the total to the variable sum.

```number1 = input(" Please Enter the First Number: ")
number2 = input(" Please Enter the second number: ")

# Using arithmetic + Operator
sum = float(number1) + float(number2)
print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))```

`````` Please Enter the First Number: 22
Please Enter the second number: 44
The sum of 22 and 44 is 66.0``````

In this example, the following statements ask the user to enter two integer numbers and store the user entered values in variables number 1 and number2.

```number1 = input(" Please Enter the First Number: ")
number2 = input(" Please Enter the second number: ")```

Next line, we used Arithmetic Operators ‘+’ to add number1 and number2 and then assigned that total to the sum.

From the below statement, you can observe that we used float type cast to convert the user input values to float.

It is because by default user entered values will be of string type. And if we use the + operator in between two string values, it will concat those two numbers instead of adding them.

`sum = float(number1) + float(number2)`

The Following Python print statement will print the sum variable as output (22 + 44 = 66).

`print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))`

This program to add two numbers worked well while adding two positive integers. How about adding positive and negative integers? Let us see the output.

## Simple Python Program to Add Two Floating-Point Numbers

It worked fine with negative numbers too! An alternative way of writing the above Python add two numbers program is:

```number1 = float(input(" Please Enter the First : "))
number2 = float(input(" Please Enter the second : "))

# Using arithmetic + Operator
sum = number1 + number2
print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))```
`````` Please Enter the First : 25
Please Enter the second : 91
The sum of 25.0 and 91.0 is 116.0``````

NOTE: Above simple program will restrict the user, Not to enter string values as input

## Python program to add two numbers using functions

The user-defined function addNumbers() accepts two arguments and returns the addition of them.

```def addNumbers(a, b):
return a + b

a = float(input("Enter the First Value  = "))
b = float(input("Enter the second Value = "))

print(result)```
``````Enter the First Value  = 22.5
Enter the second Value = 99.3
121.8``````

## Python program to add two numbers without using Operator

This program converts or adds the user given values to a list. Next, the list sum() function will perform the addition.

```a = float(input("Enter the First Value  = "))
b = float(input("Enter the second Value = "))

numList = [a,b]
result = sum(numList)
print(result)```
``````Enter the First Value  = 222
Enter the second Value = 981.98
1203.98``````

The below program accepts the two numbers at the same time and performs the addition using list comprehension and sum.

```nums = input("Enter Two Numbers Seperated by Space = ")

numList = [float(n) for n in nums.split()]

result = sum(numList)
print(result)```
``````Enter Two Numbers Seperated by Space = 44.5  99.8
144.3``````

## Addition of two numbers in Python using for loop

This example uses the for loop to iterate from 0 to 1 and accepts two floating-point numbers. Within the for loop, we add user-inputs.

```result = 0

for i in range(2):
n = float(input("Enter a Value = "))
result += n

print(result)```
``````Enter a Value = 19
Enter a Value = 32.8
51.8``````

## Addition of two numbers in Python using While loop

```result = 0
i = 0

while i < 2:
n = float(input("Enter a Value = "))
result += n
i += 1

print(result)```
``````Enter a Value = 125.9
Enter a Value = 237.4
363.3``````

## Python Program to Add Two Numbers using Lambda Function

It is the simplest approach. If you want, you can allow the user inputs.

```result = lambda a, b: a + b

print(result(30.5, 40))```
``70.5``

The below example code accepts the two numbers and uses the lambda function to perform addition.

```x = float(input("Enter the First Value  = "))
y = float(input("Enter the second Value = "))

result = lambda a, b: a + b
print(result(x, y))```
``````Enter the First Value  = 22
Enter the second Value = 44
66.0``````

It uses lambda and the reduce functions.

```from functools import reduce

nums = input("Enter Two Numbers Seperated by Space = ")

numList = [float(n) for n in nums.split()]

result = reduce(lambda a, b: a + b, numList)
print(result)```
``````Enter Two Numbers Seperated by Space = 32.7 44.9
77.6``````