Python Add Two Variables

In this tutorial, I will show you how to add two variables in Python. Adding variables is perhaps the most fundamental operation I perform in my daily coding routine.

Whether I am calculating financial data or merging user lists, understanding how Python handles addition is critical.

I have spent over a decade building software, and I have learned that “adding” means different things for different data types.

If you try to add a number to a string without converting it, Python will throw a TypeError. In this guide, I will cover how to add integers, floats, strings, lists, and even dictionaries using various Python keywords and methods.

Methods to Add Two Variables in Python

There are various methods to add two variables in Python. Let us check each method with examples.

I used an online Python editor to execute all the Python programs below. You can choose any editor that supports Python code, like Visual Studio Code.

1. Use the + Operator

The easiest way to add two variables in Python is by using the + operator. This operator is used to perform arithmetic addition.

Here is an example.

Consider a scenario where you want to calculate the total distance traveled by a car on two different days.

# Distance traveled on day 1
distance_day1 = 120  # in miles

# Distance traveled on day 2
distance_day2 = 150  # in miles

# Total distance traveled
total_distance = distance_day1 + distance_day2

print("Total distance traveled:", total_distance, "miles")

You can refer to the screenshot below to see the output.

how to add two variables in python

Here is another example.

Imagine you are running an online store, and you want to calculate the total sales for two different months.

# Sales for two months
sales_month1 = 15000 # in dollars
sales_month2 = 20000 # in dollars

# Calculate total sales
total_sales = sales_month1 + sales_month2

print("Total sales for two months:", total_sales, "dollars")

Let me show you another example, also.

Imagine you are managing your monthly budget and need to calculate the total expenses for two categories: groceries and utilities.

# Monthly expenses for groceries and utilities
expenses_groceries = 400 # in dollars
expenses_utilities = 150 # in dollars

# Calculate total monthly expenses
total_expenses = expenses_groceries + expenses_utilities

print("Total monthly expenses:", total_expenses, "dollars")

You can refer to the screenshot below to see the output.

how to add variables in python

2. Use Functions in Python

We can also create a function in Python that you can reuse to add two variables. By defining a function, we can reuse the code whenever needed.

Here is an example.

Let’s create a function to calculate the total salary of an employee based on their base salary and bonus.

def calculate_total_salary(base_salary, bonus):
    return base_salary + bonus

# Base salary and bonus
base_salary = 50000  # in dollars
bonus = 5000  # in dollars

# Calculate total salary
total_salary = calculate_total_salary(base_salary, bonus)

print("Total salary:", total_salary, "dollars")

You can refer to the screenshot below to see the output.

adding variables in python

3. Use the sum() Function in Python

The sum() function in Python is used to sum up the elements of an iterable (e.g., list, tuple). It can also be used to add two variables by passing them as elements of a list or tuple.

Here is an example.

Consider a scenario where you want to calculate the total revenue generated by two products.

# Revenue generated by product A
revenue_product_a = 20000  # in dollars

# Revenue generated by product B
revenue_product_b = 30000  # in dollars

# Calculate total revenue using sum() function
total_revenue = sum([revenue_product_a, revenue_product_b])

print("Total revenue:", total_revenue, "dollars")

In this example, we use the sum() function to add the revenues generated by two products. We pass the revenues as elements of a list to the sum() function, which returns the total revenue.

4. Use the operator.add Method in Python

The operator module in Python provides a set of functions. And one method is the .add() method. The operator.add method can be used to add two variables.

Let me show you an example.

Let’s use the operator.add method to calculate the total marks obtained by a student in two subjects.

import operator

# Marks obtained in subject 1
marks_subject1 = 85

# Marks obtained in subject 2
marks_subject2 = 90

# Calculate total marks using operator.add
total_marks = operator.add(marks_subject1, marks_subject2)

print("Total marks obtained:", total_marks)

In this example, we import the operator module and use the operator.add method to add the marks obtained in two subjects.

5. Use Lambda Functions

Lambda functions, also known as anonymous functions, can be used to add two variables in Python. They are defined using the lambda keyword.

Here is an example.

Consider a scenario where you want to calculate the total price of items in a shopping cart.

# Define a lambda function to add two prices
add_prices = lambda price1, price2: price1 + price2

# Prices of two items
price_item1 = 30  # in dollars
price_item2 = 45  # in dollars

# Calculate total price using lambda function
total_price = add_prices(price_item1, price_item2)

print("Total price of items:", total_price, "dollars")

You can refer to the screenshot below to see the output.

adding variables python

6. Use Recursive Functions

Recursive functions are functions that call themselves. Although not commonly used for simple addition, they can be used to add two variables in Python.

Let me show you an example.

Let’s use a Python recursive function to add two numbers.

def recursive_add(a, b):
    if b == 0:
        return a
    else:
        return recursive_add(a + 1, b - 1)

# Numbers to be added
num1 = 10
num2 = 5

# Calculate sum using recursive function
total = recursive_add(num1, num2)

print("Sum of numbers:", total)

In this example, the recursive_add function adds two numbers by incrementing a and decrementing b until b it becomes zero.

In this tutorial, I have explained how to add two variables in Python using various methods, like:

  1. Using the + Operator
  2. Using Functions
  3. Using the sum() Function
  4. Using the operator.add Method
  5. Using Lambda Functions
  6. Using Recursive Functions

The + operator is mostly used to add variables in Python. I hope you understand it now.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.