{"id":598,"date":"2024-07-25T08:58:29","date_gmt":"2024-07-25T03:58:29","guid":{"rendered":"https:\/\/augurytech.co.uk\/courses\/?p=598"},"modified":"2024-10-05T09:05:40","modified_gmt":"2024-10-05T04:05:40","slug":"python-for-loop","status":"publish","type":"post","link":"https:\/\/augurytech.co.uk\/courses\/python\/python-for-loop\/","title":{"rendered":"Python: For Loop"},"content":{"rendered":"\n<p><br>Python provides a <strong>for loop<\/strong> to iterate over a sequence (e.g. a list, tuple and dictionary). This is a convenient tool when you want to traverse all or a few<br>elements of a sequence without worrying about the number of elements in the sequence.<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--20);padding-bottom:var(--wp--preset--spacing--20);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, 3, 2, 6, 90, 34, 5]<\/strong>\n<strong>\nfor elem in arr:\n     print (elem)<\/strong><\/code><\/pre>\n\n\n\n<p>The output of this snippet will print each element of the list in a new line.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">For Loop: Break keyword<\/h3>\n\n\n\n<p>There are situations where we want to terminate our loop even if all the elements are not yet traversed, e.g. when finding a desired value and you saw it in the middle of the sequence. In such cases, you would want to terminate the loop. Python provides the break keyword for this scenario.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>arr = &#91;1, 3, 2, 6, 67, 23, 45]\nfor elem in arr:\n   if elem == 6:\n      break<\/strong>        <em># loop will be terminated as it encounters 6<\/em><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-e1bb55cb\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">For Loop: Continue keyword<\/h3>\n\n\n\n<p>Situations where we want to skip only the current iteration of the loop. Python provides a continue keyword for such a scenario.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>arr = &#91;1, 3, 2, 6, 67, 23, 45, 49]\nfor elem in arr:\n   if elem % 2 == 0:\n      continue<\/strong>        <em># Loop will skip from here to next iteration<\/em>\n   <strong>print (elem)<\/strong><\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python provides a for loop to iterate over a sequence (e.g. a list, tuple and dictionary). This is a convenient tool when you want to traverse all or a fewelements of a sequence without worrying about the number of elements in the sequence. The output of this snippet will print each element of the list [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-python","format":"standard","meta":{"_seopress_robots_primary_cat":"5","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-598","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python"},"_links":{"self":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/comments?post=598"}],"version-history":[{"count":14,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":620,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/598\/revisions\/620"}],"wp:attachment":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/media?parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/categories?post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/tags?post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}