{"id":2098,"date":"2015-04-20T13:35:31","date_gmt":"2015-04-20T11:35:31","guid":{"rendered":"https:\/\/www.modelical.com\/?p=1363"},"modified":"2022-03-21T12:07:58","modified_gmt":"2022-03-21T11:07:58","slug":"ghpython-105-loops-i","status":"publish","type":"post","link":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/","title":{"rendered":"Grasshopper Python 105"},"content":{"rendered":"<h2>Loops (I)<\/h2>\n<p>Loops are a fundamental weapon when dealing with program flow. You&#8217;ll want your component to perform a certain action many times over a data-set or to keep on doing something until a condition is met. That&#8217;s exactly what loops are for. Let&#8217;s see them in action.<\/p>\n<h3>Two Types Of Loops<\/h3>\n<p><strong>The Fore Each Loop\u00a0<\/strong>is useful when you want to exactly the same operation to each element within a collection. For instance printing something to the console or just adding all the numbers in a collection. We have to supply an interator value, of the a suitable type for the collection, that will be evaluated at each step of the loop.<\/p>\n<p>This example prints reads a string as a list of letters and prints them independently<\/p>\n<pre class=\"lang:python decode:true \">myName = \"Roberto Molinos\"\r\nfor letter in myName:\r\n    print letter<\/pre>\n<p><img decoding=\"async\" width=\"1720\" height=\"599\" class=\"alignnone size-large wp-image-4079\" src=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_01.png\" alt=\"105_Loops_01\" srcset=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_01.png 1720w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_01-480x167.png 480w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_01-768x267.png 768w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_01-1280x446.png 1280w\" sizes=\"(max-width: 1720px) 100vw, 1720px\" \/><\/p>\n<p>This example takes a list of numbers and returns the number of elements and the sum of all of them:<\/p>\n<pre class=\"lang:python decode:true\">buffer = 0\r\nfor num in x:\r\n    buffer += int(num)\r\n\r\nprint \"There are %d items in the list and their sum is %s\" %(len(x),buffer)<\/pre>\n<p><img decoding=\"async\" width=\"1720\" height=\"599\" class=\"alignnone size-large wp-image-4080\" src=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_02.png\" alt=\"105_Loops_02\" srcset=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_02.png 1720w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_02-480x167.png 480w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_02-768x267.png 768w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_02-1280x446.png 1280w\" sizes=\"(max-width: 1720px) 100vw, 1720px\" \/><\/p>\n<p>This example takes a list of strings and creates a sentence with all the supplied values:<\/p>\n<pre class=\"lang:python decode:true\">buffer = \"My favourite ice cream flavours are:\"\r\nfor flavour in x:\r\n    buffer += str(flavour) + \", \"\r\n    \r\n\r\nprint buffer<\/pre>\n<p><img decoding=\"async\" width=\"1720\" height=\"599\" class=\"alignnone size-large wp-image-4081\" src=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_03.png\" alt=\"105_Loops_03\" srcset=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_03.png 1720w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_03-480x167.png 480w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_03-768x267.png 768w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/105_Loops_03-1280x446.png 1280w\" sizes=\"(max-width: 1720px) 100vw, 1720px\" \/><\/p>\n<p><strong>The For\u00a0loop<\/strong> is useful when we want to keep control of the iteration process, being able modify actions depending on iterator state. Look at this example that will output only elements with even index from a list:<\/p>\n<pre class=\"lang:python decode:true \"># This script takes a list of input numbers and returns a sublist with the even numbers\r\n# Declare a list to hold even number\r\nevenNumList = []\r\n\r\n# make i change between 0 and the length of the x list\r\nfor i in range(0,len(x)):\r\n\r\n# check if the index is even\r\n    if i % 2 == 0:\r\n\r\n# if so, append the item to the list\r\n        evenNumList.append(x[i])\r\n\r\n# Return the list\r\na = evenNumList<\/pre>\n<p>The key here is the declaration of the loop:<\/p>\n<li><strong>for i in;<\/strong>\u00a0establishes i as our iterator, the number that will change to give us access to each item within the list.<\/li>\n<li><strong>range(0, len(x))\u00a0<\/strong>\u00a0establishes that i will change and grow\u00a0from 0 to the maximun size of the list we are evaluating<\/li>\n<p>Besides, the inner IF statement is checking when the iterator is even, look at it carefully.<\/p>\n<p>Another example, this component will create a Fibonacci series:<\/p>\n<pre class=\"lang:python decode:true\"># This script creates a the first x numbers of a fibonacci series\r\n# Declare a list and start it with the first two numbers\r\nfiboList = [1,1]\r\n\r\n# Check that x is at least 2\r\nif x &gt; 1:\r\n    # make i change between 0 and x\r\n    for i in range(2,x):\r\n    \r\n    # retrieve the two previous values, add them an assign to a temporary variable\r\n        buffer = fiboList[i - 2] + fiboList[i - 1]\r\n    \r\n    # append the value to the list\r\n        fiboList.append(buffer)\r\n\r\n# Return the list\r\na = fiboList<\/pre>\n<p>Challenge<\/p>\n<p>1. Write a Python\u00a0component that reads a list of numbers and outputs the largest of them together with its index.<\/p>\n<p>2. Write a Python\u00a0component that reads a list of numbers and outputs the smallest of them together with its index.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Loops (I) Loops are a fundamental weapon when dealing with program flow. You&#8217;ll want your component to perform a certain action many times over a data-set or to keep on doing something until a condition is met. That&#8217;s exactly what loops are for. Let&#8217;s see them in action. Two Types Of Loops The Fore Each [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":27118,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[418,422],"class_list":["post-2098","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guidelines","tag-technology-grasshopper","tag-technology-python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Grasshopper Python 105 - Modelical<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Grasshopper Python 105 - Modelical\" \/>\n<meta property=\"og:description\" content=\"Loops (I) Loops are a fundamental weapon when dealing with program flow. You&#8217;ll want your component to perform a certain action many times over a data-set or to keep on doing something until a condition is met. That&#8217;s exactly what loops are for. Let&#8217;s see them in action. Two Types Of Loops The Fore Each [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/\" \/>\n<meta property=\"og:site_name\" content=\"Modelical\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Modelical\/\" \/>\n<meta property=\"article:published_time\" content=\"2015-04-20T11:35:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-21T11:07:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.modelical.com\/wp-content\/uploads\/Posts_31_GH_1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"600\" \/>\n\t<meta property=\"og:image:height\" content=\"375\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Roberto Molinos\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@modelical\" \/>\n<meta name=\"twitter:site\" content=\"@modelical\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Roberto Molinos\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/\"},\"author\":{\"name\":\"Roberto Molinos\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\"},\"headline\":\"Grasshopper Python 105\",\"datePublished\":\"2015-04-20T11:35:31+00:00\",\"dateModified\":\"2022-03-21T11:07:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/\"},\"wordCount\":326,\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/04\\\/Posts_31_GH_1-400x250-1.jpg\",\"keywords\":[\"Tec Grasshopper\",\"Tec Python\"],\"articleSection\":[\"Guidelines\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/\",\"name\":\"Grasshopper Python 105 - Modelical\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/04\\\/Posts_31_GH_1-400x250-1.jpg\",\"datePublished\":\"2015-04-20T11:35:31+00:00\",\"dateModified\":\"2022-03-21T11:07:58+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-105-loops-i\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/04\\\/Posts_31_GH_1-400x250-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/04\\\/Posts_31_GH_1-400x250-1.jpg\",\"width\":400,\"height\":250},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/\",\"name\":\"Modelical\",\"description\":\"We build information\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\",\"name\":\"Roberto Molinos\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro\",\"caption\":\"Roberto Molinos\"},\"sameAs\":[\"https:\\\/\\\/www.modelical.com\"],\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/author\\\/roberto\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Grasshopper Python 105 - Modelical","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/","og_locale":"en_US","og_type":"article","og_title":"Grasshopper Python 105 - Modelical","og_description":"Loops (I) Loops are a fundamental weapon when dealing with program flow. You&#8217;ll want your component to perform a certain action many times over a data-set or to keep on doing something until a condition is met. That&#8217;s exactly what loops are for. Let&#8217;s see them in action. Two Types Of Loops The Fore Each [&hellip;]","og_url":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/","og_site_name":"Modelical","article_publisher":"https:\/\/www.facebook.com\/Modelical\/","article_published_time":"2015-04-20T11:35:31+00:00","article_modified_time":"2022-03-21T11:07:58+00:00","og_image":[{"width":600,"height":375,"url":"https:\/\/www.modelical.com\/wp-content\/uploads\/Posts_31_GH_1.jpg","type":"image\/jpeg"}],"author":"Roberto Molinos","twitter_card":"summary_large_image","twitter_creator":"@modelical","twitter_site":"@modelical","twitter_misc":{"Written by":"Roberto Molinos","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/#article","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/"},"author":{"name":"Roberto Molinos","@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5"},"headline":"Grasshopper Python 105","datePublished":"2015-04-20T11:35:31+00:00","dateModified":"2022-03-21T11:07:58+00:00","mainEntityOfPage":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/"},"wordCount":326,"image":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/Posts_31_GH_1-400x250-1.jpg","keywords":["Tec Grasshopper","Tec Python"],"articleSection":["Guidelines"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/","url":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/","name":"Grasshopper Python 105 - Modelical","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/#primaryimage"},"image":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/Posts_31_GH_1-400x250-1.jpg","datePublished":"2015-04-20T11:35:31+00:00","dateModified":"2022-03-21T11:07:58+00:00","author":{"@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.modelical.com\/en\/ghpython-105-loops-i\/#primaryimage","url":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/Posts_31_GH_1-400x250-1.jpg","contentUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/Posts_31_GH_1-400x250-1.jpg","width":400,"height":250},{"@type":"WebSite","@id":"https:\/\/www.modelical.com\/en\/#website","url":"https:\/\/www.modelical.com\/en\/","name":"Modelical","description":"We build information","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.modelical.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5","name":"Roberto Molinos","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro","url":"https:\/\/secure.gravatar.com\/avatar\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro","caption":"Roberto Molinos"},"sameAs":["https:\/\/www.modelical.com"],"url":"https:\/\/www.modelical.com\/en\/author\/roberto\/"}]}},"_links":{"self":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts\/2098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/comments?post=2098"}],"version-history":[{"count":0,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts\/2098\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media\/27118"}],"wp:attachment":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media?parent=2098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/categories?post=2098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/tags?post=2098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}