Question

Write a Python function to count the number of even numbers from a list of numbers....

Write a Python function to count the number of even numbers from a list of numbers.

Write a Python function that takes a list and returns a new list with unique elements of the first list.
Sample List : [1,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]

Homework Answers

Answer #1

(Be careful with indentation of lines in python. If you still have any doubts regarding this question please comment I will definitely help)

1).

Explanation:

  • Call the even_count() function by passing the list x
  • initialize count to 0
  • run for loop from 0 to len(x)
  • Use the condition x[i]%2==0. If yes then increment count.
  • After coming out of for loop return count.
  • print the count

Code to find number of even numbers in a list:

def even_count(x):
count = 0
for i in range(len(x)):
if(x[i]%2==0):
count=count+1
return count

x = [2,3,9,6,5,10,8,12,14]
n = even_count(x)
print("number of even numbers in the list is: ",n)

O/P:

number of even numbers in the list is: 6

Code and O/P screenshot:

Here in the input list there are 6 even numbers. So it prints 6

2).

Code Explanation:

  • pass the list x to the unique_list() function
  • In the function initially declare an empty list named unique
  • Then run a for loop from 0 to length of list
  • Inside the for loop use the condition if(x[i] not in unique). then unique.append(x[i])
  • This condition adds elements to the list unique if the element is not present in the unique list which doesnot add the same element again
  • return the unique list

Code to return unique elements of the list:

def unique_list(x):
unique = []
for i in range(len(x)):
if(x[i] not in unique):
unique.append(x[i])
return unique

x = [1,2,3,3,3,3,4,5]
print(unique_list(x))

O/P:

[1, 2, 3, 4, 5]

Code and O/P screenshot:

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
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
In Python, write a function that takes in an arbitrary list of numeric triples (tuples of...
In Python, write a function that takes in an arbitrary list of numeric triples (tuples of size 3), and returns a new list containing the maximum elements from each
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.
Write a Python function count_bigger that takes two parameters, a nested list of objects and a...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a threshold number. It returns an integer specifying how many of the objects anywhere in the nested list are numbers that are larger than the threshold. (For our purposes, "numbers" are either integers or floats.) There may be objects in the list other than numbers, in which case you would simply ignore them. Here are a couple of examples of how the function should behave...
Write the function most_factors(numbers) that returns the integer from the list numbers that has the most...
Write the function most_factors(numbers) that returns the integer from the list numbers that has the most factors (divisors without remainder). For example: >>> most_factors([5,10,16,20,25]) 20 # because 20 has the most factors of any of these numbers # 6 factors, i.e., [1, 2, 4, 5, 10, 20] >>> most_factors([1, 2, 3, 4, 5]) 4 # because 4 has the most factors of any of these numbers # 3 factors, i.e., [1, 2, 4] Hints: For each element in numbers, call...
Write a program in python that prints the count of all prime numbers between A and...
Write a program in python that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = The 5 digit unique number you had picked at the beginning of the semester B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2,...
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 called unique words that takes a phrase as an input string and...
Write a python function called unique words that takes a phrase as an input string and returns a list of the unique words found in that phrase. The words should be listed in alphabetical order.