The Wayback Machine - https://web.archive.org/web/20250317231007/https://www.geeksforgeeks.org/python-program-to-add-two-numbers/
Open In App

How to Add Two Numbers in Python

Last Updated : 07 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The task of adding two numbers in Python involves taking two input values and computing their sum using various techniques . For example, if a = 5 and b = 7 then after addition, the result will be 12.

Using the “+” Operator

+ operator is the simplest and most direct way to add two numbers . It performs standard arithmetic addition between two values and returns the result.

Python
a = 15
b = 12

# Adding two numbers
res = a + b
print(res)

Output
27

Using user input

This method allows users to input numbers dynamically instead of hardcoding them. The input() function is used to take input from the user, which is then converted to a numerical format before performing addition.

Python
# taking user input
a = input("First number: ")
b = input("Second number: ")

# converting input to float and adding
res = float(a) + float(b)

print(res)

Output

First number: 13.5
Second number: 1.54
15.04

Explanation: This code takes user input as strings, converts them to floats using float(), adds them and stores the sum in res.

Using Function

Functions help organize code into reusable blocks. By defining a function, we can call it multiple times with different values, making our program more structured and efficient.

Python
# function to add two numbers
def add(a, b):
    return a + b

# initializing numbers
a = 10
b = 5

# calling function
res = add(a,b)

print(res)

Output
15

Explanation: This code defines a function add(a, b) that returns the sum of a and b. It initializes a and b with values 10 and 5, calls the add() function with these values, stores the result in res.

Using Lambda Function

A lambda function is an anonymous, one-line function that can perform operations without defining a full function block. It is useful for short, simple calculations.

Python
res = lambda a, b: a + b
print(res(10, 5))

Output
15

Explanation: lambda res that takes two arguments and returns their sum. The lambda function is then called with 10 and 5, returning 15 .

Using operator.add

operator module provides a functionally equivalent method to the + operator. This is useful when working with functional programming or reducing redundancy in complex operations.

Python
import operator
print(operator.add(10, 5))

Output
15

Explanation: operator module, which provides functions for various arithmetic and logical operations. It then uses operator.add(10, 5), which performs addition like 10 + 5.

Using sum()

sum() function is commonly used to add multiple numbers in an iterable like lists or tuples. It provides a simple way to add elements without using loops or explicit operators.

Python
print(sum([10, 5]))

Output
15

Explanation: This code uses the sum() function, which takes a list of numbers [10, 5] and returns their sum.

You can also check other operations:

How to Add Two Numbers in Python – FAQs

Can we add integers and floating-point numbers together in Python? 

Yes, Python allows you to add integers and floating-point numbers together seamlessly. When you perform arithmetic operations between integers and floats, Python automatically handles the conversion and performs the addition.

# Example:
a = 5 # Integer
b = 2.5 # Floating-point number

result = a + b
print(result) # Output: 7.5

Can we use bitwise operators to add numbers in Python?

Bitwise operators (&, |, ^, <<, >>, ~) perform operations at the bit level and are typically used for manipulating binary data rather than adding numbers in the conventional sense. While some bitwise operations might seem like addition in specific cases (such as | for bitwise OR), they are not used for general arithmetic addition.

# Example:
a = 5 # 101 in binary
b = 3 # 011 in binary

result = a | b # Bitwise OR operation
print(result) # Output: 7 (which is 111 in binary)

How to sum two numbers in a class in Python?

To sum two numbers within a class in Python, you can define a method that performs the addition.

class Calculator:
def add(self, num1, num2):
return num1 + num2

# Example usage:
calc = Calculator()
result = calc.add(10, 20)
print(result) # Output: 30

Can we add 2 lists together in Python?

Yes, you can concatenate (combine) two lists together using the + operator in Python.

# Example:
list1 = [1, 2, 3]
list2 = [4, 5, 6]

combined_list = list1 + list2
print(combined_list) # Output: [1, 2, 3, 4, 5, 6]

How to add numbers from different data types in Python?

Python supports automatic type conversion during arithmetic operations. When adding numbers of different types (like integers, floats, or even other types), Python will convert them to a compatible type if necessary.

# Example:
int_num = 10
float_num = 2.5
complex_num = 1 + 3j

result = int_num + float_num
print(result) # Output: 12.5 (integer + float)

result = float_num + complex_num
print(result) # Output: (3.5+3j) (float + complex)


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg