Addition Operator in Python (+)

The Addition Operator (+) is one of the most fundamental tools in Python. While its primary job is to find the sum of numbers, Python allows this operator to perform different tasks depending on the data type it is working with.
1. Numeric Addition
When used with integers or floats, the operator performs standard mathematical addition.
# Adding Integers
print(10 + 5) # Output: 15
# Adding Floats
print(10.5 + 4.5) # Output: 15.0
# Adding Integer and Float (Result is always a float)
print(10 + 2.5) # Output: 12.5
2. Addition of Complex Numbers
Python natively supports complex numbers, where the imaginary part is denoted by j. When adding complex numbers, Python adds the real parts and the imaginary parts separately.
# Defining complex numbers
c1 = 2 + 3j
c2 = 1 + 2j
# Adding complex numbers
result = c1 + c2
print(result) # Output: (3+5j)
# Adding a real number to a complex number
print(10 + (2 + 4j)) # Output: (12+4j)
j for the imaginary part. Using i (as commonly used in math) will result in a NameError.
3. String Concatenation
When you use + between two strings, it "glues" them together. This process is called concatenation.
first_name = "Python"
last_name = "Language"
# Combining strings
full_name = first_name + " " + last_name
print(full_name) # Output: Python Language
4. List and Tuple Merging
In Python, you can use the addition operator to join two lists or two tuples into a single new collection.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
print(combined_list) # Output: [1, 2, 3, 4, 5, 6]
5. Common Errors to Avoid
# print("Age: " + 25) # This will raise a TypeError
Solution: Convert the number to a string using str(25).
💡 Interesting Facts
- Operator Overloading: The
+operator is a great example of "Polymorphism"—it takes different forms depending on whether it's adding numbers or joining strings. - Booleans are Numbers: In Python,
Trueis 1 andFalseis 0. So,True + Trueactually equals2! - Efficiency: While
+is easy for joining strings, if you are joining a long list of strings, using the.join()method is much faster in terms of performance.