{"id":38404,"date":"2016-10-26T18:58:44","date_gmt":"2016-10-26T23:58:44","guid":{"rendered":"http:\/\/tecjames.wordpress.com\/?p=640"},"modified":"2016-10-26T18:58:44","modified_gmt":"2016-10-26T23:58:44","slug":"project-12-use-of-loops-with-for","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/project-12-use-of-loops-with-for\/","title":{"rendered":"Project 12: Use of loops with \u2018for\u2019"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"https:\/\/tecjames.wordpress.com\/2016\/10\/26\/project-12-use-of-loops-with-for\/\"><a href=\"https:\/\/tecjames.wordpress.com\">TEC GDL 2016<\/a><\/a><\/em><br \/><\/p>\n<p>After having covered the if statement and the use of else and elif within those statements I feel like I&#8221;m <span style=\"color:#ff0000;\">getting closer to actually do smarter things<\/span> with my codes &#8211; not just tasks that I would easily be able to solve in my head.<\/p>\n<p>Before I start explaining the logic behind &#8216;for&#8217; loops &#8211; the topic of today&#8217;s blog post &#8211; I will once more explain <strong>how to create and use lists<\/strong>. This is necessary to bring back to mind because you need to store results of the loops you create somewhere.<\/p>\n<p><span style=\"color:#0000ff;\"><em><strong>Lists<\/strong><\/em><\/span> = Lists are containers of things that are organized from first to last that look like this:<\/p>\n<p>courses = [Programming, Negotiation, Entrepreneurship]<br \/>\nschooldays = [Monday, Tuesday,Thursday,Friday]<br \/>\n(no school on Wednesday&#8217;s for me ;))<\/p>\n<p>For further explanation have a look at <a href=\"https:\/\/tecjames.wordpress.com\/2016\/09\/13\/project-6-basic-types-their-use-part-ii\/\" >an earlier blog post (click here)<\/a>\u00a0about basic types and their use.<\/p>\n<h3><span style=\"text-decoration:underline;\"><em><strong><span style=\"color:#ff0000;text-decoration:underline;\">Now to &#8216;for&#8217; loops:<\/span><\/strong><\/em><\/span><\/h3>\n<p>For loops are traditionally used when you have a <span style=\"text-decoration:underline;\"><em>piece of code you want to repeat &#8216;n&#8217; number of times<\/em><\/span>. There alternatively is also the &#8216;while&#8217; loop that you are able to use within Python. These are rather considered when it comes to conditions to be met and I will be covering this in my NEXT blog post!<\/p>\n<p>For loops run for a fixed amount of variables (compared to while loops that can theoretically run forever). Any object whose data can be represented in list form (also called &#8216;objects with an iterable method&#8217;) can be used in a &#8216;for&#8217; loop. When for loops are used you are also able to create a list containing an amount of numbers (defined by the input) by using the &#8220;range&#8221; function.<\/p>\n<p>Have a look at the following flow diagram to understand further how &#8216;for&#8217; loops can be useful:<\/p>\n<p><img class=\"  wp-image-655 aligncenter\" src=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/python_for_loop.jpg?w=311&#038;h=281\" alt=\"python_for_loop.jpg\" width=\"311\" height=\"281\" srcset=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/python_for_loop.jpg?w=311&amp;h=281 311w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/python_for_loop.jpg?w=150&amp;h=136 150w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/python_for_loop.jpg?w=300&amp;h=271 300w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/python_for_loop.jpg 388w\" sizes=\"(max-width: 311px) 100vw, 311px\" \/><\/p>\n<p><span style=\"text-decoration:underline;\">Thus, a general syntax of a <div class=\"post-limited-image\"><img class=\"  wp-image-670 aligncenter\" src=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=416&#038;h=286\" alt=\"Bildschirmfoto 2016-10-26 um 18.56.42.png\" width=\"416\" height=\"286\" srcset=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=416&amp;h=286 416w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=832&amp;h=572 832w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=150&amp;h=103 150w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=300&amp;h=206 300w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=768&amp;h=528 768w\" sizes=\"(max-width: 416px) 100vw, 416px\" \/><\/div>\n<div class=\"post-limited-image\"><img class=\"  wp-image-668 aligncenter\" src=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=231&#038;h=244\" alt=\"Bildschirmfoto 2016-10-26 um 18.55.27.png\" width=\"231\" height=\"244\" srcset=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=231&amp;h=244 231w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=462&amp;h=488 462w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=142&amp;h=150 142w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=284&amp;h=300 284w\" sizes=\"(max-width: 231px) 100vw, 231px\" \/><\/div>\n<div class=\"post-limited-image\"><img class=\"aligncenter  wp-image-673\" src=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=507&#038;h=429\" alt=\"Bildschirmfoto 2016-10-26 um 18.42.55.png\" width=\"507\" height=\"429\" srcset=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=507&amp;h=429 507w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=1014&amp;h=858 1014w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=150&amp;h=127 150w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=300&amp;h=254 300w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=768&amp;h=650 768w\" sizes=\"(max-width: 507px) 100vw, 507px\" \/><\/div>\n<div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/tecjames.wordpress.com\/640\/\" \/><\/div>\n<div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=tecjames.wordpress.com&#038;blog=115237664&%23038;post=640&%23038;subd=tecjames&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/><\/div><!--more--> loop is:<\/span><\/p>\n<p><span style=\"color:#ff0000;\">for<\/span> \u00a0\u00a0<span style=\"color:#0000ff;\">variable<\/span>\u00a0 \u00a0<span style=\"color:#ff0000;\">in<\/span> \u00a0\u00a0<span style=\"color:#0000ff;\">sequence<span style=\"color:#ff0000;\">:<\/span><\/span><br \/>\n<span style=\"color:#808080;\">\u00a0 \u00a0 \u00a0 \u00a0<span style=\"color:#0000ff;\"> \u00a0statement(s)<br \/>\n<span style=\"color:#ff0000;\">else:<br \/>\n<\/span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0statement(s)<\/span><\/span><\/p>\n<p>Have a look at the following code to understand further how &#8216;for&#8217; loops are used:<\/p>\n<p><img class=\"  wp-image-670 aligncenter\" src=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=416&#038;h=286\" alt=\"Bildschirmfoto 2016-10-26 um 18.56.42.png\" width=\"416\" height=\"286\" srcset=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=416&amp;h=286 416w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=832&amp;h=572 832w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=150&amp;h=103 150w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=300&amp;h=206 300w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-56-42.png?w=768&amp;h=528 768w\" sizes=\"(max-width: 416px) 100vw, 416px\" \/><\/p>\n<p>What you should see after executing this code:<img class=\"  wp-image-668 aligncenter\" src=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=231&#038;h=244\" alt=\"Bildschirmfoto 2016-10-26 um 18.55.27.png\" width=\"231\" height=\"244\" srcset=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=231&amp;h=244 231w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=462&amp;h=488 462w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=142&amp;h=150 142w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-55-27.png?w=284&amp;h=300 284w\" sizes=\"(max-width: 231px) 100vw, 231px\" \/><\/p>\n<p>When using an <span style=\"color:#ff0000;\">&#8216;else&#8217; statement with a &#8216;for&#8217; loop<\/span>, the else statement is used once the loop has exhausted iterating the given list.<br \/>\nHave a look at the following to understand this:<\/p>\n<p><img class=\"aligncenter  wp-image-673\" src=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=507&#038;h=429\" alt=\"Bildschirmfoto 2016-10-26 um 18.42.55.png\" width=\"507\" height=\"429\" srcset=\"https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=507&amp;h=429 507w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=1014&amp;h=858 1014w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=150&amp;h=127 150w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=300&amp;h=254 300w, https:\/\/tecjames.files.wordpress.com\/2016\/10\/bildschirmfoto-2016-10-26-um-18-42-55.png?w=768&amp;h=650 768w\" sizes=\"(max-width: 507px) 100vw, 507px\" \/><\/p>\n<p>For additional uses of the &#8216;for&#8217; loop visit this website:<br \/>\n<a href=\"https:\/\/wiki.python.org\/moin\/ForLoop\">https:\/\/wiki.python.org\/moin\/ForLoop<br \/>\n_<\/a>_____________________________________<\/p>\n<p>That&#8217;s all there is to cover about &#8216;for&#8217; loops. Make sure to visit my blog later today for more information on &#8216;while&#8217; loops &amp; nested loops!!<\/p>\n<p>See ya later, aligator!<br \/>\n<a href='https:\/\/tecjames.wordpress.com\/mentions\/tecjames\/' class='mention'><span class='mentions-prefix'>@<\/span>tecjames<\/a><\/p>\n<p><span style=\"text-decoration:underline;\"><em><strong>Sources:<\/strong><\/em><\/span><\/p>\n<p><a href=\"https:\/\/learnpythonthehardway.org\/book\/ex32.html\" rel=\"nofollow\">https:\/\/learnpythonthehardway.org\/book\/ex32.html<\/a><br \/>\n<a href=\"https:\/\/wiki.python.org\/moin\/ForLoop\" rel=\"nofollow\">https:\/\/wiki.python.org\/moin\/ForLoop<\/a><br \/>\n<a href=\"https:\/\/www.tutorialspoint.com\/python\/python_for_loop.htm\" rel=\"nofollow\">https:\/\/www.tutorialspoint.com\/python\/python_for_loop.htm<\/a><br \/>\n<a href=\"http:\/\/www.python-course.eu\/python3_for_loop.php\" rel=\"nofollow\">http:\/\/www.python-course.eu\/python3_for_loop.php<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><br \/>  <a rel=\"nofollow\" href=\"http:\/\/feeds.wordpress.com\/1.0\/gocomments\/tecjames.wordpress.com\/640\/\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/tecjames.wordpress.com\/640\/\" \/><\/a> <img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=tecjames.wordpress.com&#038;blog=115237664&%23038;post=640&%23038;subd=tecjames&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at TEC GDL 2016 After having covered the if statement and the use of else and elif within those statements I feel like I&#8221;m getting closer to actually do smarter things with my codes &#8211; not just tasks that I would easily be able to solve in my head. Before I start explaining &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/project-12-use-of-loops-with-for\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Project 12: Use of loops with \u2018for\u2019<\/span><\/a><\/p>\n","protected":false},"author":35,"featured_media":655,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[28],"class_list":["post-38404","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","category-syndicated","tag-uncategorized"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=38404"}],"version-history":[{"count":13,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38404\/revisions"}],"predecessor-version":[{"id":42777,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38404\/revisions\/42777"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=38404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=38404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=38404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}