Question

In: Computer Science

Provided is a list of numbers. For each of the numbers in the list, determine whether...

Provided is a list of numbers. For each of the numbers in the list, determine whether they are even. If the number is even, add True to a new list called is_even. If the number is odd, then add False.

num_lst = [3, 20, -1, 9, 10]

**Please use Python**

Solutions

Expert Solution

Question:  

Provided is a list of numbers. For each of the numbers in the list, determine whether they are even. If the number is even, add True to a new list called is_even. If the number is odd, then add False.

num_lst = [3, 20, -1, 9, 10]

**Please use Python**

Answer:

Summary: We can implement this function in two approaches . First one is the normal list implementation. And the second one is using anonymous function (a function without a name is called anonymous function) or lambda function. Here we also use another function named map (syntax: map(function,sequence)). The "function" inside the map function performs a specified operation on all the elements of the sequence and the modified elements are returned which can be stored in another sequence.

1st approach:

print("how namy element do you want to enter? ") # taking number of elements of the list from the user
n=int(input())
lst=[] #declaring an empty list
for i in range(n):
   print("enter element: ") #taking inputs one by one from the user
   lst.append(int(input()))
print("your list is: ",lst) #displaying the list given by the user

#lst=[3,20,-1,9,10] #we can also initialize the list to calculate if we donot need to take inputs from user

is_even=[] #declaring an empty list to store here the final result
for i in lst:
   if(i%2==0):
       is_even.append("true")
   else:
       is_even.append("false")  
print("final list is " , is_even)  

2nd approach (using lambda function):

print("how namy element do you want to enter? ") # taking number of elements of the list from the user
n=int(input())
lst=[] #declaring an empty list
for i in range(n):
   print("enter element: ") #taking inputs one by one from the user
   lst.append(int(input()))
print("your list is: ",lst) #displaying the list given by the user

#lst=[3,20,-1,9,10] #we can also initialize the list to calculate if we donot need to take inputs from user

is_even=[] #declaring an empty list to store here the final result

is_even=list(map(lambda x: (x%2 ==0),lst))
print("final lis is " , is_even)  


Related Solutions

5. Determine whether or not the following functions from real numbers to real numbers are bijections....
5. Determine whether or not the following functions from real numbers to real numbers are bijections. If they are bijections, then find the inverse. If they are not bijections, then explain why not. (a) f(x) = [2x] (b) f(x) = −7x (c) f(x) = 7x 3 – 5 (d) f(x) = x 2 − 5
Review the list of common titration errors. Determine whether each error would cause the calculation for...
Review the list of common titration errors. Determine whether each error would cause the calculation for moles of analyte to be too high, too low, or have no effect. adding titrant past the color change of the analyte solution recording the molarity of titrant as 0.1 M rather than its actual value of 0.01 M spilling some analyte out of the flask during the titration starting the titration with air bubbles in the buret filling the buret above the 0.0...
in PYTHON Determine whether a number provided by the user is perfect or not. write a...
in PYTHON Determine whether a number provided by the user is perfect or not. write a program that: 1. Ask the user for a positive integer 2. Compute the list of integer divisors of the given number 3. Check whether the given number is perfect 4. Communicate the result to the user (something like ‘Your number…. Is a perfect number’ or ‘Your number ….. is not a perfect number’. 5. In any case, communicate to the user the list of...
72. The purposes of a(n) ______ should be to determine whether the project provided the customer...
72. The purposes of a(n) ______ should be to determine whether the project provided the customer with the expected benefits, assess the level of customer satisfaction, and obtain any feedback that would be helpful in future business relationships with this customer or with other customers. a. post-project evaluation meeting with the customer or sponsor b. project progress meeting with project stakeholders c. analysis session with the project team d. pre-RFP meeting with the customer
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
Please write code in Scala Write a function which check that numbers in provided list are...
Please write code in Scala Write a function which check that numbers in provided list are correctly sorted. Please use tail recursion
Write a for-loop in MATLAB that generates a list of numbers such that each number is...
Write a for-loop in MATLAB that generates a list of numbers such that each number is the sum of the previous three. Initialize your list of numbers at the values of 0, 0 and 1. In other words, "0" is the first element of the list, "0" is the second element of the list, and "1" is the third element of the list. What is the 20th value in the list?
Determine the electron configuration and whether each of the following is paramagnetic. Also, determine if the...
Determine the electron configuration and whether each of the following is paramagnetic. Also, determine if the complex undergoes a Jahn-Teller distortion. 1. [Fe(CN)6]4- 2. [Fe(CN)6]3- 3. [Co(H2O)6]3+ 4. [CoF6]3- 5. [Ni(CO)6]2+
Determine whether the relation R on the set of all real numbers is reflexive, symmetric, antisymmetric,...
Determine whether the relation R on the set of all real numbers is reflexive, symmetric, antisymmetric, and/or transitive, where (x,y) ∈ R if and only if: a) x = 1 OR y = 1 b) x = 1 I was curious about how those two compare. I have the solutions for part a) already.
For each of the statements in Section A, determine whether it is true or false, then...
For each of the statements in Section A, determine whether it is true or false, then explain in a few sentences why that is the answer. Note: no marks will be given for answers that do not include an explanation. 1. If money stops being a reliable store of value, then it ceases to be useful as a medium of exchange. 2. The gold standard is an improvement over actual gold coinage because it allows the government/central bank to control...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT