Question

In Python: Problem 5] Write a function that accepts a list as argument and returns a...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Python Write function words() that takes one input argument—a file name—and returns the list of actual...
Python Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
Create a function that returns a boolean value if all-elements are not odd or even. Using...
Create a function that returns a boolean value if all-elements are not odd or even. Using Dr. Racket and the How to Design a Function (HtDF ) recipe, create a function that will return the following check-expect: (check-expect (all-elements? even? (list 1 2 3) false) (check-expect (all-elements? even? (list 2 4 6) true) (check-expect (all-elements? odd? (list 1 3 5) true) Can you find an element in the list where the predicate fails (return false)?
Create a function which returns the number of odd integers in a list (python)
Create a function which returns the number of odd integers in a list (python)
In Python: Sublist of list A is defined as a list whose elements are all from...
In Python: Sublist of list A is defined as a list whose elements are all from list A. For example, suppose list A = [0, 1, 2, 3, 4, 5, 6], its has many sublists and one of them is [0, 1, 3] because elements 0, 1 and 3 are all contained in list A. Define a function named returnComplement that accepts two integer lists as the parameter (one of the list is the sublist of the other). Suppose names...
Python Implement function allEven() that takes a list of integers and returns True if all integers...
Python Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10]) False
Hello! I am confused on the code for this question. Write a complete Python function called...
Hello! I am confused on the code for this question. Write a complete Python function called Frog, with two integer parameters M and N, where N has the default value of 5. The function computes and returns one of two results: if N is odd it returns the equation 2 * M + N, but if it is even it returns the equation 3 * M - N instead.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT