-1

marks = [90, 25, 67, 45, 80] a = marks print(marks.count(a>=60))

I want to see how many students got marks over 60

DH.KIM
  • 11

4 Answers4

2

You could do this:

In [6]: sum(x >= 60 for x in marks)
Out[6]: 3
Akavall
  • 82,592
  • 51
  • 207
  • 251
1

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
mhawke
  • 84,695
  • 9
  • 117
  • 138
1

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.

Try this code snippet here

marks = [90, 25, 67, 45, 80]
print(len(filter(lambda x: x>=60,marks)))
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Reason behind downvote will be helpful for me. Who downvoted? – Sahil Gulati Sep 25 '17 at 04:23
  • Apart from this code printing `237`, you also need to import `functools.reduce` in Python 3. That should be a big hint that `reduce()` is no longer favoured. Combining `filter()` with a `lambda` and then `reduce()` is inefficient and less readable than the equivalent list comprehension or by using `sum()`. – mhawke Sep 25 '17 at 04:27
1

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)))
Stacktrace
  • 112
  • 4