In Python:
Problem 5] Write a function that accepts a list as argument and returns a list that contains:
(1) The positive elements of the taken list (2) The negative
elements of the taken list (3) The odd elements of the taken
list
(4) The even elements of the taken list
Below is your code:
#function definition
def getLists(list):
#declaration of lists
postiveList = []
negativeList = []
evenList = []
oddList = []
#loop to fill the list with respective values
for x in range(0, len(list)):
if list[x] < 0:
negativeList.append(list[x])
else:
postiveList.append(list[x])
if list[x] % 2 == 0:
evenList.append(list[x])
else:
oddList.append(list[x])
#return all the lists
return postiveList, negativeList, oddList, evenList
#crating a sample list
listOfNums = [3,5,6,-2,-5,-6,34,76,-9,1,2,5,-7,8,9,-3,6,8,3]
#calling method and getting the returned lists
postiveList, negativeList, oddList, evenList = getLists(listOfNums)
#printing the output
print("Positive List:",postiveList)
print("Negative List:",negativeList)
print("Odd List:",oddList)
print("Even List:",evenList)
Indentation and output
Get Answers For Free
Most questions answered within 1 hours.