Write a Python program to count the number 4 in a given list.

Write a Python program to count the number 4 in a given list.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">def</span> <span style="color: #0066BB; font-weight: bold">list_count_4</span>(nums):
  count <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>  
  <span style="color: #008800; font-weight: bold">for</span> num <span style="color: #000000; font-weight: bold">in</span> nums:
    <span style="color: #008800; font-weight: bold">if</span> num <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">4</span>:
      count <span style="color: #333333">=</span> count <span style="color: #333333">+</span> <span style="color: #0000DD; font-weight: bold">1</span>

  <span style="color: #008800; font-weight: bold">return</span> count

<span style="color: #008800; font-weight: bold">print</span>(list_count_4([<span style="color: #0000DD; font-weight: bold">1</span>, <span style="color: #0000DD; font-weight: bold">4</span>, <span style="color: #0000DD; font-weight: bold">6</span>, <span style="color: #0000DD; font-weight: bold">7</span>, <span style="color: #0000DD; font-weight: bold">4</span>]))
<span style="color: #008800; font-weight: bold">print</span>(list_count_4([<span style="color: #0000DD; font-weight: bold">1</span>, <span style="color: #0000DD; font-weight: bold">4</span>, <span style="color: #0000DD; font-weight: bold">6</span>, <span style="color: #0000DD; font-weight: bold">4</span>, <span style="color: #0000DD; font-weight: bold">7</span>, <span style="color: #0000DD; font-weight: bold">4</span>]))
</pre></td></tr></table></div>

Final output

Leave a Comment