Computer Science
Answer
(i)
y for y in range(100) if y % 2 == 0 and if y % 5 == 0
The list comprehension should be enclosed in square brackets, and we should use a single if
statement with both conditions combined using the and
operator.
The corrected code is:
[y for y in range(100) if y % 2 == 0 and y % 5 == 0]
(ii)
(y for y in range(100) if y % 2 == 0 and if y % 5 == 0)
The list comprehension should be enclosed in square brackets, and we should use a single if
statement with both conditions combined using the and
operator.
The corrected code is:
[y for y in range(100) if y % 2 == 0 and y % 5 == 0]
(iii)
["good" if i < 3: else: "better" for i in range(6)]
The list comprehension does not include colon.
The corrected code is:
["good" if i < 3 else "better" for i in range(6)]
Related Questions
Find the error. Consider the following code and predict the error(s):
(y for y in range(100) if y % 2 == 0 and if y % 5 == 0)
Find the error in the following list comprehension :
["good" if i < 3: else: "better" for i in range(6)]
Write a program that uses a function called findinlist() to check for the position of the first occurrence of v in the list passed as parameter (lst) or -1 if not found. The header for the function is given below :
def find_in_list(lst, v): """ lst - a list v - a value that may or may not be in the list """
Implement the following function for a linear list, which find outs and returns the number of unique elements in the list
def unique(lst): """passed parameter lst is a list of integers (could be empty)."""
After implementing the above function, test it with following lists and show the output produced by above function along with the reason for that output.
(i) lst = []
(ii) lst = [1, 2, 3]
(iii) lst = [1, 2, 2]
(iv) lst = [1, 2, 2, 3, 3]