Python Addition Examples

By: On:

In this Python tutorial, we will discuss on Python addition with some examples and how to add two numbers in Python. We will also check:

  • Add two numbers in Python
  • Add two numbers in python using the function
  • Add two binary numbers in Python
  • How to add two complex numbers in Python
  • Add two numbers without using + operator

(Python Addition) Add two numbers in Python

To add two numbers in Python, we will use the ” + “ operator to add two numbers stored in a variable.


Example:

number1 = 23 
number2 = 17
sum = number1 + number2
print('The sum of two number is {2}'.format(number1, number2, sum))

After writing the above code (how do you add two numbers in Python), Ones you will print then the output will appear as “The sum of two number is 40”. Here, we have two numbers and it gets added in the sum variable.

You can refer to the below screenshot for adding two numbers in python.

Python Addition
How do you add two numbers in Python

This is how we can add two numbers in python.


Add two numbers in python using function

In python, we can add two numbers using the def function, and the parameter is passed inside the parenthesis in the function definition, we can call the function by its name.

Example:

def addition(x,y):
sum = x+y;
return sum;
number1=75
number2=25
print("The sum is: ", addition(number1, number2))

After writing the above code (add two numbers in Python using the function), Ones you will print then the output will appear as ” The sum is: 100 “. Here, def addition() is the function for adding, and the variable is declared.

You can refer to the below screenshot for adding two numbers in python using the function.

Add two numbers in python using the function
Add two numbers in python using the function

This is how we can add two numbers in python using the function.

Add two binary numbers in Python

In python, to add two binary numbers we have built-in function int() which will convert the given binary string to integers and bin() will convert the integer sum value to binary.

Example:

number1 = "01100"
number2 = "01000"
sum_number = int(number1, 2) + int(number2, 2)
sumofbinary = bin(sum_number)
print(sumofbinary)

After writing the above code (add two binary numbers in Python), Ones you will print “sumofbinary” then the output will appear as ” 0b10100 “. Here, it converts the binary into an integer and it will be added and again it will convert integer sum to binary.

You can refer to the below screenshot for adding two binary numbers in python.

Add two binary numbers in Python
Add two binary numbers in Python

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

How to add two complex numbers in Python

In python, complex number are combination of real and imaginary numbers to add two complex numbers, we will use the ” + “ operator to add the two complex number.

Example:

complex_num1 = 5+3j
complex_num2 = 6+7j
add_complex = complex_num1 + complex_num2
print('SUM = ', add_complex)

After writing the above code (how to add two complex numbers in Python ), Ones you will print “add_complex” then the output will appear as ” SUM= (11+10j) “. Here, it simply adds the two complex numbers.

You can refer to the below screenshot for adding two complex numbers in Python.

How to add two complex numbers in Python
How to add two complex numbers in Python

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

Add two numbers without using + operator

In python, to add two numbers without using + operator, we can use bitwise operator to add two numbers.

Example:

def addwithoutplus(x,y):
while y!=0:
s = x & y
x = x ^ y
y = s << 1
return x
print(addwithoutplus(23, 7))

After writing the above code (how to add two numbers without using + operator ), Ones you will print “addwithoutplus” then the output will appear as ” 30 “. Here, it simply adds the two numbers without using the ” + “ operator.

You can refer to the below screenshot for adding two numbers without using + operator

Add two numbers without using + operator
Add two numbers without using + operator

This is how we can add two numbers without using + operator.


You may like following tutorials:

In this tutorial, we learned how to add two numbers in Python.

  • How do you add two numbers in Python
  • Add two numbers in python using the function
  • Add two binary numbers in Python
  • How to add two complex numbers in Python
  • Add two numbers without using + operator

Leave a Comment