Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 explore different ways to add two numbers in Python using various operators and built-in functions.
Using Arithmetic Operator
To add two numerical values in Python, we can use the addition operator, 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:", result)
On executing the above program, we will get the following output ?
Addition of two numbers: 10
Direct Addition in Print Statement
Instead of storing the numbers and resultant value in variables, we can directly add two numbers within the 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 two numbers using the augmented assignment operator ?
# Assign values to the variables
a = 7
b = 3
# Add b to a using the assignment operator
a += b
print("Addition of two numbers:", a)
On executing the above program, we will get the following output ?
Addition of two numbers: 10
Using the sum() Function
In Python, sum() is a built-in function that can be used to add two numbers. This function takes an iterable containing numerical values and returns their sum.
Example
In this example, we pass a list of two numbers to the sum() function to perform the addition ?
# 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:", result)
On executing the above program, we will get the following output ?
Addition of two numbers: 10
Using User Input
You can also add two numbers taken from user input using the input() function ?
# Get input from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Add the numbers
result = num1 + num2
print("Sum:", result)
When you run this code and enter values, it will output the sum accordingly.
Comparison
| Method | Use Case | Advantage |
|---|---|---|
+ operator |
General addition | Simple and readable |
+= operator |
Adding to existing variable | Modifies variable in-place |
sum() function |
Adding multiple numbers | Works with iterables |
Conclusion
The + operator is the most common way to add two numbers in Python. Use += when you need to modify an existing variable, and sum() when working with multiple values in a list or other iterable.
