{"id":16563,"date":"2021-09-30T11:00:23","date_gmt":"2021-09-30T05:30:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=16563"},"modified":"2021-11-22T18:35:32","modified_gmt":"2021-11-22T13:05:32","slug":"python-program-divide-all-elements-of-a-list-by-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-divide-all-elements-of-a-list-by-a-number\/","title":{"rendered":"Python Program Divide all Elements of a List by a Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Get Sum of all the Factors of a Number<\/a><\/p>\n Given a list, and the task is to divide all the elements of a given List by the given Number in Python<\/p>\n As we know, elements such as int, float, string, and so on can be stored in a List. As we all know, dividing a string by a number is impossible. To divide elements of a list, all elements must be int or float.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Want to excel in java coding? Practice with these Java Programs examples with output<\/a> and write any kind of easy or difficult programs in the java language<\/p>\n Below are the ways to divide all Elements of a List by a Number.<\/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 Get Sum of all the Factors of a Number Given a list, and the task is to divide all the elements of a given List by the given Number in Python As we know, elements such as int, float, string, and so on can be …<\/p>\nGiven List = [3, 2.5, 42, 24, 15, 32]\r\nGiven Number = 2<\/pre>\n
The above given list after division all elements of a List by the given Number= [1.5, 1.25, 21.0, 12.0, 7.5, 16.0]<\/pre>\n
Given List =\u00a0 [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]\r\nGiven Number = 5<\/pre>\n
The above given list after division all elements of a List by the given Number= [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]<\/pre>\n
Program Divide all Elements of a List by a Number<\/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 = [3, 2.5, 42, 24, 15, 32]\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb = 2\r\n# Take a new empty list and store it in a variable.\r\nLst_aftr_div = []\r\n# Iterate in the above given list using For Loop.\r\nfor itr in gven_Lst:\r\n # Divide the iterator\u00a0 by\u00a0 the above given number and append the result to the\r\n # above declared new list.\r\n Lst_aftr_div.append(itr\/gvn_numb)\r\n# print the above given list after division all elements of a List by the given Number.\r\nprint(\"The above given list after division all elements of a List by the given Number=\", Lst_aftr_div)\r\n<\/pre>\n
The above given list after division all elements of a List by the given Number= [1.5, 1.25, 21.0, 12.0, 7.5, 16.0]<\/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.\r\ngven_Lst = list(map(float, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the number as user input\u00a0using int(input()) and store it in another variable.\r\ngvn_numb = int(input(\"Enter some random number = \" ))\r\n#Take a new empty list and store it in a variable.\r\nLst_aftr_div = []\r\n# Iterate in the above given list using For Loop.\r\nfor itr in gven_Lst:\r\n # Divide the iterator\u00a0by the above given number and append the result to the\r\n # above declared new list.\r\n Lst_aftr_div.append(itr\/gvn_numb)\r\n# print the above given list after division all elements of a List by the given Number.\r\nprint(\"The above given list after division all elements of a List by the given Number=\", Lst_aftr_div)\r\n\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 4 5 8 9.6 15 63 32 84\r\nEnter some random number = 4\r\nThe above given list after division all elements of a List by the given Number= [1.0, 1.25, 2.0, 2.4, 3.75, 15.75, 8.0, 21.0]<\/pre>\n
\n