Thanks to visit codestin.com
Credit goes to flexiple.com

Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Python Program To Add Two Numbers

Python Program To Add Two Numbers

Author image

Harsh Pandey

Software Developer

Published on Fri Mar 08 2024

Writing a Python program to add two numbers is a fundamental exercise demonstrating the simplicity and elegance of Python programming for beginners. This blog post will guide you through creating a straightforward Python script that performs this basic mathematical operation.

Add Two Numbers With “+” Operator

The "+" operator is used to add two numbers in Python. This operator allows for directly adding numeric values, providing a simple yet effective way to perform arithmetic operations. In Python, adding two numbers involves defining variables to store these numbers, then using the "+" operator to calculate their sum.

For instance, if you want to add the numbers 5 and 7, you would first assign these numbers to two variables. Then, you apply the "+" operator to these variables to obtain the sum. Finally, you can print the result to display the outcome of the addition.

Here's a concise example of how this is done in Python.

# Define variables with numeric values
number1 = 5
number2 = 7

# Add the numbers using the "+" operator
sum = number1 + number2

# Display the result
print("The sum is:", sum)

Output.

The sum is: 12

This code snippet demonstrates the fundamental concept of adding two numbers in Python. It highlights the simplicity of the language and how straightforward it is to perform arithmetic operations using Python's built-in operators.

Add Two Numbers With User Input

To add two numbers with user input in a Python program, you will use the input() function to collect numbers from the user and perform the addition operation. This process involves prompting the user to enter two numbers, converting these inputs from strings to numeric data types (such as integers or floating-point numbers), and then calculating the sum.

# Prompt the user to enter the first number
num1 = float(input("Enter first number: "))

# Prompt the user to enter the second number
num2 = float(input("Enter second number: "))

# Add the two numbers
sum = num1 + num2

# Display the sum
print("The sum of", num1, "and", num2, "is", sum)

Output.

Enter first number: 5.5
Enter second number: 4.5
The sum of 5.5 and 4.5 is 10.0

Add Two Numbers In Python Using Function

To add two numbers in Python using a function, we define a simple function that takes two parameters and returns their sum. This method encapsulates the addition operation within a reusable code block, enhancing modularity and readability. Functions in Python are defined using the def keyword, followed by the function name and parentheses containing the parameters.

# Define the function to add two numbers
def add_numbers(num1, num2):
    return num1 + num2

# Call the function with two numbers
result = add_numbers(5, 3)

# Print the result
print("The sum is:", result)

Output.

The sum is: 8

Add Two Numbers Using operator.add() Method

The operator can also add two numbers in a Python program.add() method. This method, part of Python's operator module, provides a way to perform arithmetic addition that is semantically similar to using the + operator. Still, it is used in a function-call format. This approach is particularly useful in scenarios requiring functional programming techniques or when you need to pass the addition operation as an argument to higher-order functions.

import operator

# Define the numbers to be added
number1 = 5
number2 = 3

# Use operator.add() to add the two numbers
sum = operator.add(number1, number2)

# Print the result
print("The sum is:", sum)

Output.

The sum is: 8

Add Two Numbers Using Lambda Function

Adding two numbers using a lambda function in Python showcases the power and simplicity of Python's functional programming features. A lambda function is an anonymous function defined with the keyword lambda instead of the standard def used for regular functions. This approach is ideal for simple operations expressed in a single line.

# Define a lambda function to add two numbers
add_two_numbers =

 lambda x, y: x + y

# Example usage
result = add_two_numbers(5, 3)

# Print the result
print(result)

Output.

8

Add Two Numbers Using Recursive Function

One can implement a simple yet effective approach to add two numbers using a recursive function in Python. Recursive functions are a fundamental concept in programming, allowing a function to call itself to solve a problem. This method can be particularly useful for operations that can be broken down into smaller, similar tasks.

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

# Example usage
result = add_recursive(5, 3)
print(f"The sum is: {result}")

Output.

The sum is: 8

This program defines a function add_recursive that takes two arguments, a and b. It checks if b is zero; if so, it returns a as the result. Otherwise, it calls itself with a + 1 and b - 1 until b becomes zero. The final result is the sum of the two numbers, displayed using a print statement. This method illustrates the power of recursion for simplifying the addition of two numbers into repetitive increments and decrements until a base condition is met.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.