Python has a variety of operators to perform various arithmetic operations like addition, subtraction, multiplication, division, modulus and exponentiation. Here we will discuss the addition operator in Python with some code snippets

The addition operator (+) in Python performs addition of 2 numbers. The numbers can be of the type integer or floating point. Let’s see some examples and understand the addition operator.

Addition of 2 Integer numbers

In below code the addition operator is used to perform addition of 2 integers, the sum too is a integer value.

a=10
b=20
c=a+b      #c will have the value 30 and type int
print("The sum of ", a, "and", b, "is", c)
print(type(c))

Addition of 2 Floating Point numbers

In below code the addition operator is used to perform addition of 2 floating point numbers, the sum too is a floating point value.

a=3.6
b=4.4
c=a+b      #c will have the value 8.0 and type float
print("The sum of ", a, "and", b, "is", c)
print(type(c))

Addition of an integer number and a floating point number

In below code the addition operator is used to perform addition of an integer number and a floating point number, the sum is a floating point value.

a=3
b=4.4
c=a+b      #c will have the value 7.4 and type float
print("The sum of ", a, "and", b, "is", c)
print(type(c))

Addition of 2 complex numbers

In below code the addition operator is used to perform addition of 2 complex numbers, the sum too is a complex number.

a=3+5j
b=4+6j
c=a+b      #c will have the value 7+11j and type complex
print("The sum of ", a, "and", b, "is", c)
print(type(c))

Addition of an integer number and a complex number

In below code the addition operator is used to perform addition of an integer number and a complex number, the sum is a complex number.

a=3
b=4+6j
c=a+b      #c will have the value 7+6j and type complex
print("The sum of ", a, "and", b, "is", c)
print(type(c))

Addition of a floating point number and a complex number

In below code the addition operator is used to perform addition of a floating point number and a complex number, the sum is a complex number.

a=3.5
b=4+6j
c=a+b      #c will have the value 7.5+6j and type complex
print("The sum of ", a, "and", b, "is", c)
print(type(c))

Addition of 2 string values

With string, the addition operator works as a concatenation operator. In below code the addition operator is used to perform concatenation of 2 string values, the result is a string.

a="Hello"
b="Python"
c=a+b      #c will have the value HelloPython and type str
print("The addition of ", a, "and", b, "is", c)
print(type(c))

Addition of 2 boolean values

With boolean variables, the addition operator works as in integer values, treating True as integer value 1 and False as integer value 0.

a=True      #treated as integer value 1
b=False     #treated as integer value 0
c=a+b       #c will have the value 1 and type int
print("The addition of ", a, "and", b, "is", c)
print(type(c))
d=a+a       #d will have the value 2 and type int
print(type(d))
e=b+b       #e will have the value 0 and type int
print(type(e))

Similar Operators in Python

Last modified: April 2, 2023

Author