Write a list comprehension equivalent for the Python code shown below.
for i in range(1, 101):
if int(i*0.5)==i*0.5:
print(i)
a) [i for i in range(1, 100) if int(i*0.5)==(i*0.5)]
b) [i for i in range(1, 101) if int(i*0.5)==(i*0.5)]
c) [i for i in range(1, 101) if int(i*0.5)=(i*0.5)]
d) [i for i in range(1, 100) if int(i*0.5)=(i*0.5)]
Answer: b
Explanation: The code shown above prints the value ‘i’ only if it satisfies the condition: int(i*0.5) is equal to (i*0.5). Hence the required list comprehension is: [i for i in range(1, 101) if int(i*0.5)==(i*0.5)].
Related Posts
What will be the output of the following Python code?
l=[“good”, “oh!”, “excellent!”, “#450”]
[n for n in l if n.isalpha() or n.isdigit()]What will be the output of the following Python list comprehension?
[j for i in range(2,8) for j in range(i*2, 50, i)]What is the list comprehension equivalent for?
{x : x is a whole number less than 20, x is even} (including zero)Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].
What is the list comprehension equivalent for:
list(map(lambda x:x**-1, [1, 2, 3]))?Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.
What will be the output of the following Python code?
t=32.00
[round((x-32)*5/9) for x in t]
Join The Discussion