Simple Python Program to add Two numbers

A simple addition of two numbers in Python is one of the fundamental operations. This article shows how to write a Simple Python Program to add two numbers and addition of floating-point values with examples.

In real-time, you must know how to add numbers when you write a sum of a series, calculate averages, compute totals, develop a simple calculator, or make complex calculations. Being a widely-used programming language, Python provides various ways to add two numbers, and one such case was Arithmetic operators to perform this arithmetic addition. 

In the following sections, we explore a Python program to add two numbers using the arithmetic + operator, functions, for loop, while loop, and lambda expression.

Python Addition Program

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, 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))

Output.

Python Program to add Two numbers using user input

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: ")

In the 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, the 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 program of adding two numbers 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))
Python Program to Add Two Floating-Point Numbers

NOTE: The 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 their addition.

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

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

result = addNumbers(a, b)
print(result)
Python Program to add Two numbers using functions

Python program to add two numbers without using the operator

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

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

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))
Python Program to add Two numbers using lambda

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

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

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

Comments are closed.