{"id":2093,"date":"2015-03-24T01:52:13","date_gmt":"2015-03-24T00:52:13","guid":{"rendered":"https:\/\/www.modelical.com\/?p=1229"},"modified":"2022-03-21T12:13:07","modified_gmt":"2022-03-21T11:13:07","slug":"ghpython-103-conditional-statements","status":"publish","type":"post","link":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/","title":{"rendered":"Grasshopper Python 103"},"content":{"rendered":"<h2>Conditional Statements<\/h2>\n<p>Working with single values improves a lot when you can perform what is called conditional logic. This introduces a basic concept called program flow: parts of our code that get executed depending on some conditions. Conditional statements require operators, expressions that compare two (mostly numerical) properties and check for a logical state.<\/p>\n<h3>Basic Conditional Statement<\/h3>\n<pre class=\"lang:python decode:true\">if x &gt; y:  #Check if x is strictly larger than y\r\n    A = x   #If so, Assign x to A\r\nelse:\r\n    A = 0.0 #Assign 0.0 to A\r\n<\/pre>\n<p>Every conditional statement has a logical test and an action to perform if the test is true. The &#8220;else:&#8221; is optional and will be executed in case the test is false.<\/p>\n<p><strong>Important<\/strong>: Conditional statements and other flow control expression require we define blocks of code by carefully using <span style=\"text-decoration: underline;\">indents<\/span> in our lines. You can use whatever spaces you want but it is better to indent blocks using the TAB key. A minimum condition is that all lines belonging to the same block must be indented the same number of positions.<\/p>\n<p>This is correct:<\/p>\n<pre class=\"lang:c# decode:true\">if x &gt; y:\r\n    A = x\r\n    B = 0.0  \r\nelse:\r\n       A = 0.0 \r\n       B = 1.0\r\n<\/pre>\n<p>This is NOT correct:<\/p>\n<pre class=\"lang:c# decode:true\">if x &gt; y:\r\n    A = x\r\n  B = 0.0  \r\nelse:\r\n       A = 0.0 \r\n     B = 1.0<\/pre>\n<p>&nbsp;<\/p>\n<h3>\u00a0Operators<\/h3>\n<p>More info can be found in the corresponding <a title=\"Python Operators\" href=\"http:\/\/www.tutorialspoint.com\/python\/python_basic_operators.htm\">Python Operators<\/a><\/p>\n<p><strong>Comparison Operators:<\/strong><\/p>\n<p>Comparison operators are used to check two elements against each other. Most of the times the elements will be numbers (integers, doubles, floats,&#8230;) but also other types, specially when determining if two elements are the same.<\/p>\n<li><strong>A &lt; B<\/strong> &#8211; Will check if the A is strictly smaller than B.<\/li>\n<li><strong>A &lt;= B<\/strong> &#8211; Will check if A is smaller or equal than B.<\/li>\n<li><strong>A == B<\/strong> &#8211; Will check if A is equal to B.<\/li>\n<li><span style=\"line-height: 1.5em;\"><strong>A &gt; B<\/strong> &#8211; Will check if the A is strictly larger than B.<\/span><\/li>\n<li><span style=\"line-height: 1.5em;\"><strong>A &gt;= B<\/strong> &#8211; Will check if A is larger or equal than B.<\/span><\/li>\n<li><strong>A != B<\/strong> &#8211; Will check if A is not equal to \u00a0B.<\/li>\n<li><strong>A &lt;&gt;\u00a0B<\/strong> &#8211; Will check if A is not equal to \u00a0B.<\/li>\n<p><strong>Math\u00a0Operators:<\/strong><\/p>\n<li><strong>A = B<\/strong> &#8211; Will make A the same as B.<\/li>\n<li><strong>A + B\u00a0<\/strong>&#8211; Will add A to B provided they are of the same type, this method sums two numbers and concatenates two strings.<\/li>\n<li><strong>A &#8211; B\u00a0<\/strong>&#8211; Will subtract B from A\u00a0provided they are of compatible types<\/li>\n<li><strong>A \/ B\u00a0<\/strong>&#8211;\u00a0Will divide A over B and return a float (decimal) result<\/li>\n<li><strong>A \/\/ B\u00a0<\/strong>&#8211;\u00a0Will divide A over B and return\u00a0the floor integer result<\/li>\n<li><strong>A * B<\/strong> &#8211; Will multiply A times B<\/li>\n<li><strong>A % B<\/strong> &#8211; Will return the remainder of integer dividing A over B<\/li>\n<p><strong>Assignment Operators:<\/strong><\/p>\n<li><strong>A ^= B<\/strong> &#8211; Will raise A to the power of B and replace A with the result.<\/li>\n<li><strong>A *= B<\/strong> &#8211; Will multiply A times B and replace A with the result.<\/li>\n<li><strong>A \/= B<\/strong> &#8211; Will divide A over B and replace A with the result.<\/li>\n<li><strong>A %= B<\/strong> &#8211;\u00a0Will integer divide A over B and replace A with the result.<\/li>\n<li><strong>A += B<\/strong> &#8211;\u00a0Will add B to A and replace A with the result.<\/li>\n<li><span style=\"line-height: 1.5em;\"><strong>A -= B<\/strong> &#8211;\u00a0\u00a0Will subtract B from A and replace A with the result.\u00a0<\/span><\/li>\n<p><strong>Logical Operators:<\/strong><\/p>\n<p>Used to perform logical proofs between several comparison tests. Consider A and B as booleans.<\/p>\n<li><strong>A and\u00a0B<\/strong> &#8211; AND Operator. \u00a0True if both tests are true, checks both of them.<\/li>\n<li><strong>A or\u00a0B<\/strong> &#8211; OR Operator. True if either A or B are true.<\/li>\n<li><strong>not\u00a0A<\/strong>\u00a0&#8211;\u00a0NOT Operator. True if A is false, False if A is true.<\/li>\n<p><strong>Examples:<\/strong><\/p>\n<pre class=\"lang:python decode:true\">#Example 0. Copy the contents of the script to your component\r\n\r\nif x &gt; 1:\r\n    A = x\r\n\r\n#Example 1. Copy the contents of the script to your component\r\n\r\nif x &gt; z:\r\n    A = \"large\"\r\nelif x &lt;= z and x &gt; y:\r\n    A = \"medium\"\r\nelse:\r\n    A = \"small\";\r\n\r\n#Example 2. Copy the contents of the script to your component\r\n\r\nif x == y:\r\n    A = \"match!\"\r\nelif x &lt; y:\r\n    A = \"smaller\"\r\nelse: \r\n    A = \"bigger\"\r\n\r\n#Example 3. Copy the contents of the script to your component\r\n\r\nif x != 0.0:\r\n    if x &lt; y:\r\n        x += z # the same as x = x + z\r\n    else:\r\n        x -= z # the same as x = x - z\r\nA = x<\/pre>\n<h3>Challenge<\/h3>\n<p>1. Write a component that filters a lists of input integers and returns only those which are even numbers.<\/p>\n<p>2. Write a component that filters a list of input integers and returns only those which are a multiple of a Y integer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Conditional Statements Working with single values improves a lot when you can perform what is called conditional logic. This introduces a basic concept called program flow: parts of our code that get executed depending on some conditions. Conditional statements require operators, expressions that compare two (mostly numerical) properties and check for a logical state. Basic [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":27129,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[418,422],"class_list":["post-2093","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.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Grasshopper Python 103 - 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-103-conditional-statements\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Grasshopper Python 103 - Modelical\" \/>\n<meta property=\"og:description\" content=\"Conditional Statements Working with single values improves a lot when you can perform what is called conditional logic. This introduces a basic concept called program flow: parts of our code that get executed depending on some conditions. Conditional statements require operators, expressions that compare two (mostly numerical) properties and check for a logical state. Basic [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/\" \/>\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-03-24T00:52:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-21T11:13:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.modelical.com\/wp-content\/uploads\/Posts_35_GH_3.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=\"4 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-103-conditional-statements\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-103-conditional-statements\\\/\"},\"author\":{\"name\":\"Roberto Molinos\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\"},\"headline\":\"Grasshopper Python 103\",\"datePublished\":\"2015-03-24T00:52:13+00:00\",\"dateModified\":\"2022-03-21T11:13:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-103-conditional-statements\\\/\"},\"wordCount\":594,\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-103-conditional-statements\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/Posts_35_GH_3-400x250-1.jpg\",\"keywords\":[\"Tec Grasshopper\",\"Tec Python\"],\"articleSection\":[\"Guidelines\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-103-conditional-statements\\\/\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-103-conditional-statements\\\/\",\"name\":\"Grasshopper Python 103 - Modelical\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-103-conditional-statements\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-103-conditional-statements\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/Posts_35_GH_3-400x250-1.jpg\",\"datePublished\":\"2015-03-24T00:52:13+00:00\",\"dateModified\":\"2022-03-21T11:13:07+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-103-conditional-statements\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-103-conditional-statements\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/Posts_35_GH_3-400x250-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/Posts_35_GH_3-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 103 - 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-103-conditional-statements\/","og_locale":"en_US","og_type":"article","og_title":"Grasshopper Python 103 - Modelical","og_description":"Conditional Statements Working with single values improves a lot when you can perform what is called conditional logic. This introduces a basic concept called program flow: parts of our code that get executed depending on some conditions. Conditional statements require operators, expressions that compare two (mostly numerical) properties and check for a logical state. Basic [&hellip;]","og_url":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/","og_site_name":"Modelical","article_publisher":"https:\/\/www.facebook.com\/Modelical\/","article_published_time":"2015-03-24T00:52:13+00:00","article_modified_time":"2022-03-21T11:13:07+00:00","og_image":[{"width":600,"height":375,"url":"https:\/\/www.modelical.com\/wp-content\/uploads\/Posts_35_GH_3.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":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/#article","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/"},"author":{"name":"Roberto Molinos","@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5"},"headline":"Grasshopper Python 103","datePublished":"2015-03-24T00:52:13+00:00","dateModified":"2022-03-21T11:13:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/"},"wordCount":594,"image":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/Posts_35_GH_3-400x250-1.jpg","keywords":["Tec Grasshopper","Tec Python"],"articleSection":["Guidelines"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/","url":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/","name":"Grasshopper Python 103 - Modelical","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/#primaryimage"},"image":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/Posts_35_GH_3-400x250-1.jpg","datePublished":"2015-03-24T00:52:13+00:00","dateModified":"2022-03-21T11:13:07+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-103-conditional-statements\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.modelical.com\/en\/ghpython-103-conditional-statements\/#primaryimage","url":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/Posts_35_GH_3-400x250-1.jpg","contentUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/Posts_35_GH_3-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\/2093","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=2093"}],"version-history":[{"count":0,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts\/2093\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media\/27129"}],"wp:attachment":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media?parent=2093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/categories?post=2093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/tags?post=2093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}