{"id":25750,"date":"2021-12-04T09:20:00","date_gmt":"2021-12-04T03:50:00","guid":{"rendered":"https:\/\/python-programs.com\/?p=25750"},"modified":"2021-12-04T09:20:00","modified_gmt":"2021-12-04T03:50:00","slug":"python-program-to-perform-addition","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-perform-addition\/","title":{"rendered":"Python Program to Perform Addition"},"content":{"rendered":"
Given two numbers and the task is to find the addition of two numbers.<\/p>\n
Also, given a list and the task is to find the addition of all the elements in a given list.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n 1) Addition using Functions<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Addition of list Elements using for loop<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Addition of list Elements using for loop<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 { …<\/p>\nGiven first number = 2\r\nGiven second number = 3<\/pre>\n
The addition of given two numbers { 2 + 3 } = 5<\/pre>\n
Given List = [4, 7, 3, 1, 5]<\/pre>\n
The sum of all the elements of a given list [4, 7, 3, 1, 5] = \r\n20<\/pre>\n
Program to Perform Addition in Python<\/h2>\n
\n
Method #1: Using + Operator (Static Input)<\/h3>\n
\n
# Create a function say Additionof_2numbers() which accepts the given two numbers\r\n# as the arguments and returns the addition of given two numbers.\r\n\r\n\r\ndef Additionof_2numbers(gvn_num1, gvn_num2):\r\n # Inside the function, add the given two numbers and store it in a variable.\r\n rsltsum = gvn_num1 + gvn_num2\r\n # Return the above result.\r\n return(rsltsum)\r\n\r\n# Give the first number as static input and store it in a variable.\r\ngvn_num1 = 2\r\n# Give the second number as static input and store it in another variable.\r\ngvn_num2 = 3\r\n# Pass the given two numbers as the arguments to the Additionof_2numbers() function \r\n# and store it in another variable.\r\nfnl_summ = Additionof_2numbers(gvn_num1, gvn_num2)\r\n# Print the addition of given two numbers\r\nprint(\"The addition of given two numbers {\",gvn_num1,\"+\",gvn_num2,\"} = \",fnl_summ)\r\n<\/pre>\n
The addition of given two numbers { 2 + 3 } = 5<\/pre>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [4, 7, 3, 1, 5]\r\n# Take a variable and initialize its value to 0.\r\nrslt_sum = 0\r\n# Loop in the given list using the for loop.\r\nfor elemnt in gvn_lst:\r\n # Add the element of the given list to the above declared rslt_sum variable\r\n # and store it in the same variable.\r\n rslt_sum = rslt_sum+elemnt\r\n# Print the sum of all the elements of a given list.\r\nprint(\"The sum of all the elements of a given list\", gvn_lst, \"= \")\r\nprint(rslt_sum)\r\n<\/pre>\n
The sum of all the elements of a given list [4, 7, 3, 1, 5] = \r\n20<\/pre>\n
Method #2: Using + Operator (User Input)<\/h3>\n
\n
# Create a function say Additionof_2numbers() which accepts the given two numbers\r\n# as the arguments and returns the addition of given two numbers.\r\n\r\n\r\ndef Additionof_2numbers(gvn_num1, gvn_num2):\r\n # Inside the function, add the given two numbers and store it in a variable.\r\n rsltsum = gvn_num1 + gvn_num2\r\n # Return the above result.\r\n return(rsltsum)\r\n\r\n# Give the first number as user input using the int(input()) fucntion and store it in a variable.\r\ngvn_num1 = int(input(\"Enter some random number = \"))\r\n# Give the second number as user input using the int(input()) fucntion and store it in another variable.\r\ngvn_num2 = int(input(\"Enter some random number = \"))\r\n# Pass the given two numbers as the arguments to the Additionof_2numbers() function \r\n# and store it in another variable.\r\nfnl_summ = Additionof_2numbers(gvn_num1, gvn_num2)\r\n# Print the addition of given two numbers\r\nprint(\"The addition of given two numbers {\",gvn_num1,\"+\",gvn_num2,\"} = \",fnl_summ)\r\n<\/pre>\n
Enter some random number = 55\r\nEnter some random number = 45\r\nThe addition of given two numbers { 55 + 45 } = 100<\/pre>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n\r\n# Take a variable and initialize its value to 0.\r\nrslt_sum = 0\r\n# Loop in the given list using the for loop.\r\nfor elemnt in gvn_lst:\r\n # Add the element of the given list to the above declared rslt_sum variable\r\n # and store it in the same variable.\r\n rslt_sum = rslt_sum+elemnt\r\n# Print the sum of all the elements of a given list.\r\nprint(\"The sum of all the elements of a given list\", gvn_lst, \"= \")\r\nprint(rslt_sum)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 6 7 3 1 2\r\nThe sum of all the elements of a given list [6, 7, 3, 1, 2] = \r\n19<\/pre>\n","protected":false},"excerpt":{"rendered":"