{"id":442,"date":"2021-03-17T08:47:28","date_gmt":"2021-03-17T08:47:28","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=430"},"modified":"2021-06-24T09:25:06","modified_gmt":"2021-06-24T08:25:06","slug":"countingoccurences","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/nat4\/countingoccurences\/","title":{"rendered":"Counting Occurences"},"content":{"rendered":"<p>Counting occurrences is used to count how often a value appears in a list.<\/p>\n<h1>General Algorithm<\/h1>\n<pre>set total to zero\nfor each item in list\n    if item is a mathch then\n        add 1 to the counter\n    end if\nnext item\n<\/pre>\n<h1>Counting values as they are entered (Nat4)<\/h1>\n<pre class=\"brush: python; highlight: [2,5,6]; title: ; notranslate\" title=\"\">#\r\nadults = 0\r\nfor person in range(10):\r\n    age = int(input(&quot;Enter age&quot;))\r\n    if age &gt;= 18:\r\n        adults = adults + 1\r\n#<\/pre>\n<h1>Counting values is an in an array (Nat5)<\/h1>\n<pre class=\"brush: python; highlight: [4,6,7]; title: ; notranslate\" title=\"\">#\r\nages = &#x5B;18, 12, 15, 21, 19, 23, 21, 35, 25, 14]\r\n#\r\nadults = 0\r\nfor person in range(10):\r\n    if ages&#x5B;person] &gt;= 18:\r\n        adults = adults + 1\r\n#<\/pre>\n<h1>Adding numbers in an array of records (Higher)<\/h1>\n<h2>Traversing array by index:<\/h2>\n<pre class=\"brush: python; highlight: [2,4,5]; title: ; notranslate\" title=\"\">#\r\nadults = 0\r\nfor person in range(10):\r\n    if members&#x5B;person].age &gt;= 18:\r\n        adults = adults + 1\r\n#<\/pre>\n<h2>Traversing array by values:<\/h2>\n<pre class=\"brush: python; highlight: [2,4,5]; title: ; notranslate\" title=\"\">#\r\nadults = 0\r\nfor person in members:\r\n    if person.age &gt;= 18:\r\n        adults = adults + 1\r\n#<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Counting occurrences is used to count how often a value appears in a list. General Algorithm set total to zero for each item in list if item is a mathch then add 1 to the counter end if next item Counting values as they are entered (Nat4) # adults = 0 for person in range(10): age = int(input(&quot;Enter age&quot;)) if age &gt;= 18: adults = adults + 1 # Counting<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/nat4\/countingoccurences\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":297,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-442","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":8,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/442\/revisions"}],"predecessor-version":[{"id":591,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/442\/revisions\/591"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/297"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}