{"id":17210,"date":"2021-09-30T11:00:55","date_gmt":"2021-09-30T05:30:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=17210"},"modified":"2021-11-22T18:35:30","modified_gmt":"2021-11-22T13:05:30","slug":"python-program-to-add-number-to-each-element-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-add-number-to-each-element-in-a-list\/","title":{"rendered":"Python Program to Add Number to each Element in a List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check a Binary Number is Divisible by a number N.<\/a> 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 Below are the ways to add a given input number to each element in a list.<\/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 Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Check a Binary Number is Divisible by a number N. Given a list and the task is to add a given input number to each element in a list. Examples: Example1: Input: Given list = [2, 4, 6, 8, 10] Given number = 5 Output: …<\/p>\n
\nGiven a list and the task is to add a given input number to each element in a list.<\/p>\nGiven list = [2, 4, 6, 8, 10]\r\nGiven number = 5<\/pre>\n
The given list after addition of a number to each element in a list = [7, 9, 11, 13, 15]<\/pre>\n
Given list = [1, 4, 6, 2, 9]\r\nGiven number = 10<\/pre>\n
The given list after addition of a number to each element in a list = [11, 14, 16, 12, 19]<\/pre>\n
Program to Add Number to each Element in a List<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the List as static input and store it in a variable.\r\ngven_lst = [2,4,6,8,10]\r\n# Give the number as static input and store it in another variable.\r\nnumbr = 5\r\n# Calculate the length of the given list using len() function and store it\r\n# in another variable.\r\nlen_lst = len(gven_lst)\r\n# Loop in the above-given list using the for loop.\r\nfor i in range(len_lst):\r\n # Inside the loop, Increment the iterator value of the list by the given number\r\n # and store it in the same variable\r\n gven_lst[i] = gven_lst[i]+numbr\r\n# Print the given list after the addition of a given number to each element in a list .\r\nprint(\"The given list after addition of a number to each element in a list = \", gven_lst)\r\n<\/pre>\n
The given list after addition of a number to each element in a list = [7, 9, 11, 13, 15]<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the List as user input using list(),map(),input(),and split() functions and \r\n#store it in a variable.\r\ngven_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the number as user input using the int(input()) function and \r\n#store it in another variable.\r\nnumbr = int(input('Enter some random number = '))\r\n# Calculate the length of the given list using len() function and store it\r\n# in another variable.\r\nlen_lst = len(gven_lst)\r\n# Loop in the above-given list using the for loop.\r\nfor i in range(len_lst):\r\n # Inside the loop, Increment the iterator value of the list by the given number\r\n # and store it in the same variable\r\n gven_lst[i] = gven_lst[i]+numbr\r\n# Print the given list after the addition of a given number to each element in a list .\r\nprint(\"The given list after addition of a number to each element in a list = \", gven_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 1 4 6 2 9\r\nEnter some random number = 10\r\nThe given list after addition of a number to each element in a list = [11, 14, 16, 12, 19]<\/pre>\n
\n