marks = [90, 25, 67, 45, 80]
a = marks
print(marks.count(a>=60))
I want to see how many students got marks over 60
How many scores over 60?
>>> marks = [90, 25, 67, 45, 80]
>>> len([score for score in marks if score > 60])
3
This uses a list comprehension to create a new list containing only those scores over 60, then the length of the new list tells you have many scores there are above 60.
Another common way is to use sum()
:
>>> sum(1 if score > 60 else 0 for score in marks)
3
You can also take advantage of the fact that booleans are ints:
>>> sum(score > 60 for score in marks)
3
Or, to better your understanding, using a for loop:
count = 0
for score in marks:
if score > 60:
count += 1
print count
I hope this one can be helpful too.
Here we are using filter
function for getting elements greater or equal to 60 and then using len
function to get no. of elements.
marks = [90, 25, 67, 45, 80]
print(len(filter(lambda x: x>=60,marks)))
You can use filter
to include only the marks you care about:
marks = [90, 25, 67, 45, 80]
print(len(filter(lambda x: x>=60, marks)))