
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add Two Numbers in Python
Adding two numbers in Python is one of the most basic tasks, where we need to combine their values to get a total.
In this article, we will see different ways to add two numbers in Python.
Using Arithmetic Operator
To add two numerical values in Python, we can use the addition operation, represented by the "+" symbol. It is one of the arithmetic operators in Python that performs the addition operation.
Example
Here is the basic example that adds the two numbers using the addition operator (+).
# Assign values to the variables a = 7 b = 3 # Add two numbers using the addition operator result = a+b # Display the results print("Addition of two numbers: ") print(result)
On executing the above program, we will get the following output -
Addition of two numbers: 10
Example
Following is another quick example that performs the addition operation on two numbers within the print() function.
Instead of storing the numbers and resultant value in the variables, this example directly adds the two numbers and prints the output in one statement using the Python print() function.
print("Addition of two numbers:", 2+6)
Here is the output of the above program -
Addition of two numbers: 8
Using the Assignment Operator
In Python += symbol is called the augmented assignment operator, which performs both the addition and assignment operation in one statement.
Example
The following example adds the two numbers using the Python assignment operator(i.e., Augmented Addition Operator).
# Assign values to the variables a = 7 b = 3 # Add two numbers using the assignment operator a += b print("Addition of two numbers: ") print(a)
On executing the above program, we will get the following output -
Addition of two numbers: 10
Using the Built-in Function
In Python, sum() is a built-in function that can be used to add two numbers. This function generally takes numerical iterables to sum the values.
Example
In this example, we will give the list of two numbers to the sum() function to perform the addition of two numbers.
# Assign values to the variables a = 7 b = 3 # Add two numbers using the sum() function result = sum([a, b]) print("Addition of two numbers: ") print(result)
On executing the above program, we will get the following output -
Addition of two numbers: 10