# Python Program to Perform Addition

Given two numbers and the task is to find the addition of two numbers.

Also, given a list and the task is to find the addition of all the elements in a given list.

Examples:

Example1:

Input:

Given first number = 2
Given second number = 3

Output:

The addition of given two numbers { 2 + 3 } =  5

Example2:

Input:

Given List = [4, 7, 3, 1, 5]

Output:

The sum of all the elements of a given list [4, 7, 3, 1, 5] =
20

## Program to Perform Addition in Python

### Method #1: Using + Operator (Static Input)

Approach:

• Create a function say Additionof_2numbers() which accepts the given two numbers as the arguments and returns the addition of given two numbers.
• Inside the function, add the given two numbers and store it in a variable.
• Return the above result.
• Give the first number as static input and store it in a variable.
• Give the second number as static input and store it in another variable.
• Pass the given two numbers as the arguments to the Additionof_2numbers() function and store it in another variable.
• Print the addition of given two numbers
• The Exit of the program.

Below is the implementation:

# Create a function say Additionof_2numbers() which accepts the given two numbers
# as the arguments and returns the addition of given two numbers.

# Inside the function, add the given two numbers and store it in a variable.
rsltsum = gvn_num1 + gvn_num2
# Return the above result.
return(rsltsum)

# Give the first number as static input and store it in a variable.
gvn_num1 = 2
# Give the second number as static input and store it in another variable.
gvn_num2 = 3
# Pass the given two numbers as the arguments to the Additionof_2numbers() function
# and store it in another variable.
# Print the addition of given two numbers
print("The addition of given two numbers {",gvn_num1,"+",gvn_num2,"} = ",fnl_summ)


Output:

The addition of given two numbers { 2 + 3 } =  5

2)Addition of list Elements using for loop

Approach:

• Give the list as static input and store it in a variable.
• Take a variable and initialize its value to 0.
• Loop in the given list using the for loop.
• Add the element of the given list to the above declared rslt_sum variable and store it in the same variable.
• Print the sum of all the elements of a given list.
• Â The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [4, 7, 3, 1, 5]
# Take a variable and initialize its value to 0.
rslt_sum = 0
# Loop in the given list using the for loop.
for elemnt in gvn_lst:
# Add the element of the given list to the above declared rslt_sum variable
# and store it in the same variable.
rslt_sum = rslt_sum+elemnt
# Print the sum of all the elements of a given list.
print("The sum of all the elements of a given list", gvn_lst, "= ")
print(rslt_sum)


Output:

The sum of all the elements of a given list [4, 7, 3, 1, 5] =
20

### Method #2: Using + Operator (User Input)

Approach:

• Create a function say Additionof_2numbers() which accepts the given two numbers as the arguments and returns the addition of the given two numbers.
• Inside the function, add the given two numbers and store it in a variable.
• Return the above result.
• Give the first number as user input using the int(input()) function and store it in a variable.
• Give the second number as user input using the int(input()) function and store it in another variable.
• Pass the given two numbers as the arguments to the Additionof_2numbers() function and store it in another variable.
• Print the addition of two numbers.
• The Exit of the program.

Below is the implementation:

# Create a function say Additionof_2numbers() which accepts the given two numbers
# as the arguments and returns the addition of given two numbers.

# Inside the function, add the given two numbers and store it in a variable.
rsltsum = gvn_num1 + gvn_num2
# Return the above result.
return(rsltsum)

# Give the first number as user input using the int(input()) fucntion and store it in a variable.
gvn_num1 = int(input("Enter some random number = "))
# Give the second number as user input using the int(input()) fucntion and store it in another variable.
gvn_num2 = int(input("Enter some random number = "))
# Pass the given two numbers as the arguments to the Additionof_2numbers() function
# and store it in another variable.
# Print the addition of given two numbers
print("The addition of given two numbers {",gvn_num1,"+",gvn_num2,"} = ",fnl_summ)


Output:

Enter some random number = 55
Enter some random number = 45
The addition of given two numbers { 55 + 45 } = 100

2)Addition of list Elements using for loop

Approach:

• Give the list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Take a variable and initialize its value to 0.
• Loop in the given list using the for loop.
• Add the element of the given list to the above declared rslt_sum variable and store it in the same variable.
• Print the sum of all the elements of a given list.
• Â The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))

# Take a variable and initialize its value to 0.
rslt_sum = 0
# Loop in the given list using the for loop.
for elemnt in gvn_lst:
# Add the element of the given list to the above declared rslt_sum variable
# and store it in the same variable.
rslt_sum = rslt_sum+elemnt
# Print the sum of all the elements of a given list.
print("The sum of all the elements of a given list", gvn_lst, "= ")
print(rslt_sum)


Output:

Enter some random List Elements separated by spaces = 6 7 3 1 2
The sum of all the elements of a given list [6, 7, 3, 1, 2] =
19