Adding Variables in Python

In this Python tutorial, you will learn about adding variables in Python. You will use the methods to add the variables containing different kinds of data.

I have explained what adding variables means.

Adding variables together is a common task that Python developers encounter. I have done this in my project wherever requirements arise; it is a very easy task, and you must know how to do it.

In this tutorial, I have shown how to add two or more variables containing data, such as integers, strings, or lists of values.

Adding Variables in Python

First, what does adding a variable in Python mean? It means performing an addition operation between two or more variables, similar to a mathematical addition operation like 4 + 5, which gives 9.

Look the two values 4 and 6 are added together using the ‘+’ operator, similarly, you can add two or more variables like this var1 + var2 + …+ varN.

However, adding two or more variables depends on the type of data in the variables. So, jump to the next section to learn how to use two variables in Python.

How to Add Two Variables in Python

To add two variables, you first need to define two variables and then add them together using the ‘+’ operator.

Declare two variables, data1 and data2, and assign values 50 and 100, respectively, as shown below.

data1 = 50
data2 = 100

Add two variables using the ‘+’ operator, as shown below.

sum_value = data1 + data2

Check the value after adding the two variables using the code below.

print(sum_value)
How to Add Two Variables in Python

Look at the output, it returns result of adding two variables as 150, the code data1 + data2 adds the values within two variables and stored it into the new variable called sum_value.

READ:  Python shutil copy file + Examples

If look closely, we have added the two numerical values of the variables, so this concept is not limited to only adding the numerical variables, also you can add the strings, for that look at the next section.

How to Add Two Variables in Python Containing String

In the above section, you added variables containing type integer or numerical values. I will show you how to add variables containing values of type characters or strings.

For example, you have two variables, first_name and last_name, containing values like ‘Rivera’ and ‘Smith’, respectively, as shown below.

first_name='Rivera'
last_name='Smith'

As shown in the code below, you can use the ‘+’ operator to add the two variables containing string values.

full_name = first_name + last_name

Now, print the full name using the code below.

print(full_name)
How to Add Two Variables in Python Containing String

From the output, you can see that the two variables, first_name and last_name, are added together using the ‘+’ operator, and as a result, it returns ‘RiveraSmith’.

Here, it adds the string together, but it is not readable or in the format we want, so let’s modify the code by adding the space between the first and last names. You need to add an empty string (‘ ‘), as shown in the code below.

full_name = first_name + ' ' + last_name
print(full_name)
How to Add Two Variables in Python Containing String With Formatting

Look now. After adding the first and last names, they are readable by adding space between them. So here, you can add literal strings like empty strings with variables using the ‘+’ operator.

Similarly, you can add any number of variables containing the string values.

How to Add Two Variables Together in a Python Containing List

You have added variables together containing numeric and string values, now you will see how to add variables containing list of values such integers.

READ:  Python Program to convert String to a List

For example, you have two lists, list1 and list2, with numerical values, as shown below.

list1 = [11, 23, 33]
list2 = [34, 65, 96]

To add these variables containing list values, you can use the same ‘+’ operator as shown below.

added_list = list1 + list2

Check the added list of values using the code below.

print(added_list)
How to Add Two Variables Together in python Containing List

The output shows that two variables containing the list of numeric values are added together using the ‘+’ operator.

Let’s understand how the variables containing a list are added. Behind the scenes, the variables’ lists of values are not added; they are concatenated.

The addition operation here adds the elements of one list to those of another, thus creating a new list containing the elements from both lists. This is done using the ‘+’ operator.

From the above two examples, I hope you understand how to add two or more variables with different data using the ‘+’ operator.

Conclusion

In this Python tutorial, you learned about adding variables in Python using the ‘+’ operator.

You learned about adding variable means, then created two variables containing integers data; you added those variables together using the ‘+’.

You also learned how to add variables containing string values and variables containing a list of values using the ‘+’ operator.

You may like to read: