{"id":4163,"date":"2022-01-27T13:14:43","date_gmt":"2022-01-27T13:14:43","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4163"},"modified":"2024-11-26T15:34:25","modified_gmt":"2024-11-26T15:34:25","slug":"for-loops","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/loops-python\/for-loops","title":{"rendered":"For loops"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Simple For Loop<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfor i in range (1, 5):\t\t#Start value, Exit value (loop won't run with this value as it exits, so needs to be desired final value + 1)\n    print i:\n<\/code><\/pre>\n\n\n\n<p>Will output: 1 2 3 4<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Reverse Loop<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfor i in range (5, -1, -1):\t\t#Start value, Exit value (loop won't run with this value as it exits, so needs to be desired final value - 1), adjust value\n    print i:\n<\/code><\/pre>\n\n\n\n<p>Will output: 5 4 3 2 1 0<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Nested For Loops<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfor level in xrange(255, -1, -1):\n    for index in xrange(0, 24):\n        led_values&#91;index] = level\n    bus.write_i2c_block_data(PCA9626_ADDRESS, (0x80 | PCA9626_REG_PWM0), led_values)\n    time.sleep(0.01)\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">For loop &#8211; Each item in a collection<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for this_item in my_list:\n    #Next item\n    print(this_item)\n\n\n#You can do a loop in 1 line if you wish:\nfor this_item in my_list: print(this_item)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">For loop &#8211; Range<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for index in range(6):    #Loop 6 times, from value 0 to 5\n    #Next index\n    print(\"Loop # \" + str(index))<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">For loop by index<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for index in range(0, len(my_array)):\n    #Next index\n    print(\"Loop # \" + str(my_array&#91;index]))<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Loop Control<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Exit loop<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    break<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Exit and start next iteration<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    continue<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simple For Loop Will output: 1 2 3 4 Reverse Loop Will output: 5 4 3 2 1 0 Nested For Loops For loop &#8211; Each item in a collection For loop &#8211; Range For loop by index Loop Control Exit loop Exit and start next iteration<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[328],"tags":[],"class_list":["post-4163","post","type-post","status-publish","format-standard","hentry","category-loops-python"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4163"}],"version-history":[{"count":12,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4163\/revisions"}],"predecessor-version":[{"id":4764,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4163\/revisions\/4764"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}