How to add two numbers in Python

In this python tutorial, you will learn about Python program to add two numbers and also we will check:

  • Python program to add two numbers
  • How to add multiple numbers in python
  • How to add two integers in python
  • Python program to add two numbers by getting input from a user
  • Python program to add two numbers using functions
  • Python program to add two numbers using class
  • Python program to add two numbers using user defined function
  • How to add two float number by getting input from a user in python
  • Python program to add two numbers without using + operator
  • Python program to add two binary numbers
  • How to add two complex numbers in python
  • How to add int and float numbers in python
  • How to add two string numbers in python
  • How to add two decimal numbers in python
  • Python program to add two complex numbers using class
  • Python program to add two numbers using class and object
  • Python Program to add two numbers using a lambda function
  • Program to add two float numbers using the function in python

Python program to add two numbers

Now, we will see python program to add two numbers.

To add two numbers in python we will use the ” + ” operator to add two numbers and then the print is used to get the output.

Example:

f_Num = 2.0
s_Num = 8.0
Sum = f_Num + s_Num
print("The addition of {0} and {1} is {2}".format(f_Num, s_Num, Sum))

We can see the addition of two numbers is 10.0 in the output. You can refer to the below screenshot for the output.

Python program to add two numbers
Python program to add two numbers

This is how we can add two numbers in Python.

Check out, Python program to reverse a string with examples and How to Print Python Fibonacci series.

How to add multiple numbers in python

Here, we can see how to add multiple numbers in python.

  • In this example, we have taken three variables as f_Num, s_Num, and t_Num, and the ” + ” operator is used to add the multiple numbers in python.
  • Sum = f_Num + s_Num + t_Num is used to find the sum
  • The print is used to get the output.

Example:

f_Num = 2.0
s_Num = 8.0
t_Num = 5.0
Sum = f_Num + s_Num + t_Num
print("The addition of {0}, {1} and {2} is {3}".format(f_Num, s_Num, t_Num, Sum))

We can see the addition of multiple numbers is 15.0 in the output. You can refer to the below screenshot for the output.

How to add multiple numbers in python
How to add multiple numbers in python

This is how we can add multiple numbers in Python.

How to add two integers in python

Let see how to add two integers in python.

To add two integers in python, we will take two numbers of integer datatype and then we add the two numbers. To get the output, I have used print(sum).

Example:

num1 = 10
num2 = 25
sum = num1 + num2
print("The addition of two integers is: ",sum)

We can see the addition of two integers in python is 35 in the output. You can refer to the below screenshot for the output.

How to add two integers in python
How to add two integers in python

This is the Python program to add two integers in Python.

You may like, Python program to print prime numbers.

Python program to add two numbers by getting input from a user

Here, we will see python program to add two numbers by getting input from a user.

  • In this example, we will ask the user to enter two numbers and then sum = int(num1) + int(num2) is used to find the sum of the two numbers given by the user.
  • To get the output, I have used print(‘The addition of {0} and {1} is {2}’.format(num1, num2, sum)).

Example:

num1 = input('Enter the first number: ')
num2 = input('Enter the second number: ')
sum = int(num1) + int(num2)
print('The addition of {0} and {1} is {2}'.format(num1, num2, sum))

We can see the addition of two numbers is 80 in the output. You can refer to the below screenshot for the output.

Python program to add two numbers by getting input from a user
Python program to add two numbers by getting input from a user

This is the python code to add two numbers by getting input from a user in Python.

Python program to add two numbers using functions

Now, we will see python program to add two numbers using functions.

  • Firstly, we will define a function as def add_number(n1,n2).
  • sum = n1 + n2 is used for addition and return sum will return the value
  • We have declared the variable as num1 and num2
  • At last, print is used to call the function and we will get the output.

Example:

def add_number(n1,n2):
    sum = n1 + n2;
    return sum;
num1 = 30
num2 = 20
print("The sum of two number is",add_number(num1,num2))

We can see the sum of two numbers is 50 in the output. You can refer to the below screenshot for the output.

Python program to add two numbers using functions
Python program to add two numbers using functions

This is how to add two numbers using functions in Python.

Check out, How to read video frames in Python and Python program to print element in an array

Python program to add two numbers using class

Here, we will see python program to add two numbers using class.

  • In this program, we have the class as add and we have defined the function as def findsum(self, n1, n2)
  • sum = n1 + n2 is used for addition and return sum will return the value.
  • We have declared the variable as n1 and n2
  • Here, the object is created and the print is used to get the output.

Example:

class add:
    def findsum(self, n1, n2):
        sum = n1 + n2
        return sum
n1 = 100
n2 = 50
Obj = add()
sum =Obj.findsum(n1, n2)
print("The sum of two numbers is: ", sum)

We can see the sum of two numbers is 150 in the output. You can refer to the below screenshot for the output.

Python program to add two numbers using class
Python program to add two numbers using class

This is the Python program to add two numbers using class.

Python program to add two numbers using user defined function

Let’s see python program to add two numbers using user defined function.

  • User defined function is the function that we define to perform a certain specific task.
  • So, we have defined the function as add_num() which will add two numbers and returns the result.
  • sum = n1 + n2 is used for addition and return sum will return the value.
  • At last, print is used to call the function and we will get the output

Example:

def add_num(n1,n2):
   sum = n1 + n2
   return sum
num1 = 25
num2 = 35
print("The sum of two number is: ", add_num(num1, num2))

We can see the sum of two numbers is 60 in the output. You can refer to the below screenshot for the output.

Python program to add two numbers using user defined function
Python program to add two numbers using user defined function

This is how to add two numbers using user defined function in Python.

Also, check out, Python binary tree implementation and Python concatenate list with examples.

How to add two float number by getting input from a user in python

Now, we will see how to add two float number by getting input from a user in python

  • In this example, we will ask the user to enter two numbers and then sum = float(n1) + float(n2) is used to find the sum of the two float numbers given by the user.
  • At last, print is used to get the output.

Example:

n1 = input("Enter first number: ")
n2 = input("Enter second number: ")
sum = float(n1) + float(n2)
print("Sum:", sum)

We can see the addition of two float numbers is 21.0 in the output. You can refer to the below screenshot for the output.

How to add two float number by getting input from a user in python
How to add two float number by getting input from a user in python

This is the Python program to add two float number by getting input from a user in python.

Python program to add two numbers without using + operator

Here, we will see python program to add two numbers without using + operator

  • Firstly, we will define a function as def add(n1, n2)
  • While is used to iterate till there is no carry
  • Carry bit can be obtained by performing AND(&) of two bits
  • And the sum of two bits can be obtained by performing XOR(^)
  • n2 = carry << 1 here carry is shifted by one so that adding it to n1 gives the required sum
  • At last, call the function and the output will be displayed.

Example:

def add(n1, n2):
    while (n2 != 0):
        carry = n1 & n2
        n1 = n1 ^ n2
        n2 = carry << 1
    return n1
print(add(45, 25))

We can see the addition of two numbers is 70 in the output. You can refer to the below screenshot for the output.

Python program to add two numbers without using + operator
Python program to add two numbers without using + operator

The above code, we can use to add two numbers without using + operator in Python.

You may like, Python string formatting with examples

Python program to add two binary numbers

Now, we will see python program to add two binary numbers.

  • In this example, we are using two built-in functions int() and bin()
  • First, we have taken two variable n1 and n2
  • The int() function converts the given string into an integer number considering the provided base value.
  • We are converting the string which is a binary value into an integer number so we are passing the base value as 2. The binary number has base 2.
  • After that, the string is converted into an integer value, then we are adding them and the result is converted to a binary number using the bin() function.

Example:

n1 = '00010'
n2 = '10010'
sum = bin(int(n1,2) + int(n2,2))
print(sum)

We can see the addition of two binary numbers is 0b10100 in the output. You can refer to the below screenshot for the output.

Python program to add two binary numbers
Python program to add two binary numbers

This is how to add two binary numbers in Python.

How to add two complex numbers in python

Let see how to add two complex numbers in python.

  • In this example, we will take two complex number which is x = (10+2j) and y = (12+6j).
  • sum = x + y to find their sum and print(sum) is used to get the output.

Example:

x = (10+2j)
y = (12+6j)
sum = x + y
print(sum)

We can see the addition of two complex numbers is (22+8j) in the output. You can refer to the below screenshot for the output.

How to add two complex numbers in python
How to add two complex numbers in python

This is how to add two complex numbers in python.

Check out, Python concatenate tuples with examples.

How to add int and float numbers in python

Here, we will see how to add int and float numbers in python

  • Firstly, we will take a variable n1 with data type int and another variable n2 holding data of type float.
  • When we add two numbers with different datatypes, the lower type is promoted to the higher datatype and this happens implicitly. Here, int is promoted to float.
  • And then we will add n1 and n2 using the + operator
  • At last, print is used to get the output.

Example:

n1 = 10
n2 = 2.0
sum = n1 + n2
print('The addition of {0} and {1} is {2}'.format(n1, n2, sum))

We can see the addition of int and float numbers is 12.0 in the output. You can refer to the below screenshot for the output.

How to add int and float numbers in python
How to add int and float numbers in python

The above code, we can use to add int and float numbers in python.

How to add two string numbers in python

Let’s see how to add two string numbers in python.

  • Firstly, we will define a function as def Sum(first,second)
  • We have the variable as f_num = “200” and s_num = “100” which is string
  • To convert string to int we have the inbuilt method called int()
  • After that, it will return the sum of numbers
  • Now, call the method Sum and print the output.

Example:

def Sum(first,second):
  return int(first) + int(second)
f_num = "200"
s_num = "100"
print(Sum(f_num,s_num))

We can see the addition of two string numbers is 300 in the output. You can refer to the below screenshot for the output.

How to add two string numbers in python
How to add two string numbers in python

This code we can use in Python to add two string numbers.

Also, you may like, How to concatenate strings in python.

How to add two decimal numbers in python

Here, we will see how to add two decimal numbers in python.

  • In this example, we have taken two variables as Num1 and Num2
  • To find the sum of the two decimal numbers we will add Num1 and Num2 by using the + operator
  • At last, print(float(add)) to get the output.

Example:

Num1 = 3.3
Num2 = 2.5
add = Num1 + Num2
print(float(add))

We can see the addition of two decimal numbers is 5.8 in the output. You can refer to the below screenshot for the output.

How to add two decimal numbers in python
How to add two decimal numbers in python

This is how we can add two decimal numbers in python.

Python program to add two complex numbers using class

Now, we will see python program to add two complex numbers using class.

  • Firstly, we will declare a class “Complex” which will store the real and imaginary parts of complex numbers.
  • The class have three methods
  • The first initComplex() method helps in initializing them and it will ask for the real part and imaginary part of the complex number.
  • The second display() method will allow displaying them to the output screen.
  • And the third method is sum(), which will help to add two complex numbers passed in the argument.

Example:

class Complex ():
    def initComplex(self):
        self.real = int(input("Enter the Real Part: "))
        self.imag = int(input("Enter the Imaginary Part: "))            
    def display(self):
        print(self.real,"+",self.imag,"i", sep="")
    def sum(self, C1, C2):
        self.real = C1.real + C2.real
        self.imag = C1.imag + C2.imag
C1 = Complex()
C2 = Complex()
C3 = Complex()
print("Enter first Complex number")
C1.initComplex()
print("First Complex Number: ", end="")
C1.display()
print("Enter second Complex number")
C2.initComplex()
print("Second Complex Number: ", end="")
C2.display()
print("Sum of two complex numbers is ", end="")
C3.sum(C1,C2)
C3.display()

We can see the addition of two complex numbers using class is 20+10i in the output. You can refer to the below screenshot for the output.

Python program to add two complex numbers using class
Python program to add two complex numbers using class

This is how we can add two complex numbers using class in Python.

Also, check out, Python Concatenate Dictionary + Examples.

Python program to add two numbers using class and object

Here, we will see python program to add two numbers using class and object

  • In this program, we have the class as Add and we have defined the function as def sum(self, x, y)
  • z = x + y is used for addition and the return z will return the value.
  • We have declared the variable as x and y
  • Here, the object is created and the print is used to get the output.

Example:

class Add:
    def sum(self, x, y):
        z = x + y
        return z
x = int(input("Enter first number:"))
y = int(input("Enter second number:"))
obj = Add()
z = obj.sum(x, y)
print("Addition of two number is:", z)

We can see the addition of two numbers using class and object is 80 in the output. You can refer to the below screenshot for the output.

Python program to add two numbers using class and object
Python program to add two numbers using class and object

This is how to add two numbers using class and object in Python.

Python Program to add two numbers using a lambda function

Let’s see python program to add two numbers using a lambda function.

  • In this example, we will use the lambda function to add two numbers
  • Now, we will ask the user to input two numbers.
  • The two-argument is used to perform addition and we have to call the lambda function for the addition of two numbers entered by the user.

Example:

z = lambda n1, n2: n1 + n2
n1 = int(input("Enter first number:"))
n2 = int(input("Enter second number:"))
result = z(n1, n2)
print("The addition of two number is:", result)

We can see the addition of two numbers using a lambda function is 60 in the output. You can refer to the below screenshot for the output.

Python Program to add two numbers using a lambda function
Python Program to add two numbers using a lambda function

This is the Python code to add two numbers using a lambda function in Python.

You may like, Python concatenate arrays.

Program to add two float numbers using the function in python

Here, we will see program to add two float numbers using the function in python.

  • Firstly, we have defined a function for addition.
  • The return sum will return the value
  • Now, we will ask the user to input two numbers. So, we can use the float() function to convert from string to float.
  • At last, we will call the function and the output will be displayed.

Example:

def add(n1,n2):
    sum = n1+n2;
    return sum;
number1=float(input("Enter the first number: "))
number2=float(input("Enter the second number: "))
print("The addition of two float number is",add(number1,number2))

We can see the addition of two float numbers using the function is 33.1 in the output. You can refer to the below screenshot for the output.

Program to add two float numbers using the function in python
Program to add two float numbers using the function in python

This is how to add two float numbers using the function in Python.

You may like, Python program to find sum of n numbers with examples

In this Python tutorial, we have learned about the Python program to add two numbers. Also, we covered these below topics:

  • Python program to add two numbers
  • How to add multiple numbers in python
  • How to add two integers in python
  • Python program to add two numbers by getting input from a user
  • Python program to add two numbers using functions
  • Python program to add two numbers using class
  • Python program to add two numbers using user defined function
  • How to add two float number by getting input from a user in python
  • Python program to add two numbers without using + operator
  • Python program to add two binary numbers
  • How to add two complex numbers in python
  • How to add int and float numbers in python
  • How to add two string numbers in python
  • How to add two decimal numbers in python
  • Python program to add two complex numbers using class
  • Python program to add two numbers using class and object
  • Python Program to add two numbers using a lambda function
  • Program to add two float numbers using the function in python