{"id":8677,"date":"2021-09-30T11:00:42","date_gmt":"2021-09-30T05:30:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=8677"},"modified":"2021-11-22T18:35:31","modified_gmt":"2021-11-22T13:05:31","slug":"python-program-to-print-sum-of-negative-numbers-positive-even-numbers-and-positive-odd-numbers-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-sum-of-negative-numbers-positive-even-numbers-and-positive-odd-numbers-in-a-list\/","title":{"rendered":"Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd numbers in a List"},"content":{"rendered":"
Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/p>\n List in Python :<\/strong><\/p>\n The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a Python List. Lists are used to store data items, with each item separated by a comma (,). A Python List can include data elements of any data type, including integers and Booleans.<\/p>\n One of the primary reasons that lists are so popular is that they are mutable. Any data item in a List can be replaced by any other data item if it is mutable. This distinguishes Lists from Tuples, which are likewise used to store data elements but are immutable.<\/p>\n Given a list the task is to print the sum of all positive even numbers ,odd numbers and negative numbers in the given list in python.<\/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 Below are the ways to print the sum of all positive even numbers ,odd numbers and negative numbers in the given list in python.<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/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 Related Programs<\/strong>:<\/p>\n Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease. List in Python : The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a Python List. …<\/p>\ngiven list =[23, 128, -4, -19, 233, 726, 198, 199, 203, -13]<\/pre>\n
The sum of all positive even numbers in thee given list [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = 1052\r\nThe sum of all positive odd numbers in thee given list [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = 658\r\nThe sum of all positive negative numbers in thee given list [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = -36<\/pre>\n
given list =[-4, 23, 12, -13, 234, 198, 55, -19, 87, 45]<\/pre>\n
The sum of all positive even numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45]\u00a0 = 444 \r\nThe sum of all positive odd numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45] =\u00a0 210 \r\nThe sum of all positive negative numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45] =\u00a0 -36<\/pre>\n
Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd numbers in a List<\/h2>\n
\n
Method #1:Using append() and Conditional Statements (User Input separated by newline)<\/h3>\n
\n
# scanning the total number of elements of the given list\r\ntotalCount = int(\r\n input(\"Enter the total number of elements of the given list = \"))\r\n# Taking a empty list\r\ngiven_list = []\r\n# Using for loop to loop totalCount times\r\nfor i in range(totalCount):\r\n eleme = int(input(\"Enter some random element(integer) = \"))\r\n given_list.append(eleme)\r\n# Taking three empty lists which stores positive\r\n# ven numbers ,positive odd numbers and negative numbers\r\nposEven = []\r\nposOdd = []\r\nnegNum = []\r\n# Traversing the list using for loop\r\nfor element in given_list:\r\n # checking if the number is greater than 0\r\n if(element > 0):\r\n # if the element is even then add this element to posEven using append() function\r\n if(element % 2 == 0):\r\n posEven.append(element)\r\n # if the element is even then add this element to posOdd using append() function\r\n else:\r\n posOdd.append(element)\r\n # else if the number is less than 0 then add to negNum list using append() function\r\n else:\r\n negNum.append(element)\r\n \r\n\r\n# Calculating sum\r\nposEvensum = sum(posEven)\r\nposOddsum = sum(posOdd)\r\nnegNumsum = sum(negNum)\r\n# printing the respectve sum's\r\nprint(\"The sum of all positive even numbers in thee given list = \",\r\n given_list, posEvensum)\r\nprint(\"The sum of all positive odd numbers in thee given list = \", given_list, posOddsum)\r\nprint(\"The sum of all positive negative numbers in thee given list = \",\r\n given_list, negNumsum)\r\n<\/pre>\n
Enter the total number of elements of the given list = 10\r\nEnter some random element(integer) = -4\r\nEnter some random element(integer) = 23\r\nEnter some random element(integer) = 12\r\nEnter some random element(integer) = -13\r\nEnter some random element(integer) = 234\r\nEnter some random element(integer) = 198\r\nEnter some random element(integer) = 55\r\nEnter some random element(integer) = -19\r\nEnter some random element(integer) = 87\r\nEnter some random element(integer) = 45\r\nThe sum of all positive even numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45]\u00a0 = 444\r\nThe sum of all positive odd numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45] =\u00a0 210\r\nThe sum of all positive negative numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45] =\u00a0 -36<\/pre>\n
Method #2:Using append() and Conditional Statements (Static Input separated by spaces)<\/h3>\n
\n
# given list\r\ngiven_list = [23, 128, -4, -19, 233, 726, 198, 199, 203, -13]\r\n# Taking three empty lists which stores positive\r\n# even numbers ,positive odd numbers and negative numbers\r\nposEven = []\r\nposOdd = []\r\nnegNum = []\r\n# Traversing the list using for loop\r\nfor element in given_list:\r\n # checking if the number is greater than 0\r\n if(element > 0):\r\n # if the element is even then add this element to posEven using append() function\r\n if(element % 2 == 0):\r\n posEven.append(element)\r\n # if the element is even then add this element to posOdd using append() function\r\n else:\r\n posOdd.append(element)\r\n # else if the number is less than 0 then add to negNum list using append() function\r\n else:\r\n negNum.append(element)\r\n\r\n\r\n# Calculating sum\r\nposEvensum = sum(posEven)\r\nposOddsum = sum(posOdd)\r\nnegNumsum = sum(negNum)\r\n# printing the respectve sum's\r\nprint(\"The sum of all positive even numbers in thee given list \",\r\n given_list, \"=\", posEvensum)\r\nprint(\"The sum of all positive odd numbers in thee given list \",\r\n given_list, \"=\", posOddsum)\r\nprint(\"The sum of all positive negative numbers in thee given list \",\r\n given_list, \"=\", negNumsum)\r\n<\/pre>\n
The sum of all positive even numbers in thee given list [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = 1052\r\nThe sum of all positive odd numbers in thee given list [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = 658\r\nThe sum of all positive negative numbers in thee given list [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = -36<\/pre>\n
\n
\n