In this python tutorial, you will learn about how to add two variables in Python and also we will check:
- How to add two variables in python
- How to add multiple variables in python
- How to combine two variables in python
- How to add two string variables in python
- How to add two-variable using function
- How to add two numbers using class in python
- Append two variables in python
How to add two variables in Python
Here, we can see how to add two variables in python.
- In this example, I have used the print statement to enter a variable.
- I have taken an input() to store the input value given by the user.
- The “+” operator is used to add the variables.
- The print(“The sum of variables “, variable) is used to get the output.
Example:
print("Enter var1")
var1 = input()
print("Enter var2")
var2 = input()
variable = int(var1)+int(var2)
print("The sum of variables ", variable)
We can see the sum of variables is 7 as the output. You can refer to the below screenshot for the output.
This is how to add two variables in Python.
Check out, How to add two numbers in Python and How to create a string in Python.
How to add multiple variables in python
Here, we can see how to add multiple variables in python.
- In this example, I have taken four variables like a,b,c,d.
- I have used the “+” operator to add multiple variables.
- I have used print(a + b + c + d) to get the output.
Example:
a = 2
b = 3
c = 7
d = 5
print(a + b + c + d)
We can see the sum of all the variables as the output. You can refer to the below screenshot for the output.
This code, we can use how to add multiple variables in Python.
You may like, Python check if the variable is an integer.
How to add two string variables in Python
Now, we can see how to add two string variables in python.
- In this example, I have taken two strings as string1 and string2.
- string1 is assigned as a pen and string2 is assigned as a pencil.
- I have used the “+” operator to add the strings.
- I have used print(string) to get the output.
Example:
string1 = 'pen'
string2 = 'pencil'
string = string1 + string2
print(string)
The below screenshot shows the output.
This is the code, we can use to add two string variables in Python.
How to combine two variables in python
Here, we can see how to combine two variables in python.
- In this example, I have taken two variables as a and b.
- The variables a is assigned as a = “Welcome to” and the variable b is assigned as b = ” Pythonguides”.
- To combine the two variables, I have used the “+” operator.
- To get the output, I have used print(c).
Example:
a = "Welcome to"
b = " Pythonguides"
c = a + b
print(c)
We can see both the strings are combined as the output. You can refer to the below screenshot for the output.
This is how to combine two variables in python.
Check out, Python write variable to file and How to create a variable in python.
How to add two-variable using function
Now, we can see how to add two-variable using function in python.
- In this example, I have defined a function as def add and passed the parameter as (a,b).
- The function is returned with the expression. The values to be added are called in the function.
- I have used print(add(5,20)) to get the output.
Example:
def add(a,b):
return a + b
print(add(5,20))
The sum of the two variables is the output. You can refer to the below screenshot for the output.
This is the code, how to add two-variable using function in Python.
How to add two numbers using class in Python
Here, we can see how to add two numbers using class in python.
- In this example, I have created a class using a constructor to initialize the value of the class
- I have created a method to add the numbers.
- I have taken two numbers as the input and created an object for the class to pass the parameter.
- The self is the keyword used to pass the attributes and methods to the class.
- To get the output, I have used print(“Result: “,obj.add()).
Example:
class cal():
def __init__(self,a,b):
self.a=a
self.b=b
def add(self):
return self.a+self.b
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
obj=cal(a,b)
print("Result: ",obj.add())
We can see the sum of numbers as the output. You can refer to the below screenshot for the output.
This code we can use to add two numbers using class in Python.
Check out, How to Create Countdown Timer using Python Tkinter.
Add int and string variables
Here, we can see how to add int and string variables in Python.
- In this example, I have taken two variables as variable1 and variable2.
- The variable1 is assigned as ‘The number is ‘ and variable 2 is assigned as 5.
- To convert the int value to a string, I have used str(variable2).
- I have used print(variable1 + str(variable2)) to get the output.
Example:
variable1 = 'The number is '
variable2 = 5
print(variable1 + str(variable2))
You can refer to the below screenshot for the output.
This is how we can add int and string variable in Python.
Also, you may like,
- Python program to reverse a string with examples
- Python program to print prime numbers.
- Variable in Python
In this Python tutorial, we have learned about Add two variables in python. Also, we covered these below topics:
- How to add two variables in python
- How to add multiple variables in python
- How to combine two variables in python
- How to add two string variables in python
- How to add two-variable using function
- How to add two numbers using class in python
- Append two variables in python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.