{"id":2092,"date":"2015-03-24T01:20:44","date_gmt":"2015-03-24T00:20:44","guid":{"rendered":"https:\/\/www.modelical.com\/?p=1224"},"modified":"2022-03-21T12:14:09","modified_gmt":"2022-03-21T11:14:09","slug":"ghpython-102-variables-and-assignment","status":"publish","type":"post","link":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/","title":{"rendered":"Grasshopper Python 102"},"content":{"rendered":"<h2>Variables and Assignment<\/h2>\n<p>After the introduction in our latest post now is time to get our feet wet with a fundamental concept: variables.<\/p>\n<h3>What Is A Variable?<\/h3>\n<p>Variables are portions of memory that represent a certain entity and that have a name. We use variables to hold information in order to read, write, modify or process it. Variables would be equivalent to parameters in GH and they behave in a very similar way.<\/p>\n<p>Variables in Python\u00a0can belong to two families: value-type and reference-type. Value-type variables are used to represent basic information such as integer numbers, decimal numbers, strings of characters and boolean states. Reference-type variables are way more flexible and can be used to hold any type of data. In GH we will use \u00a0value-type variables to basically store numbers and booleas and reference-type for surfaces, curves and points.<\/p>\n<h3>Declare and Assign Variables:<\/h3>\n<p>Declaring variables in Python is easey, just write the name of your variable and assign something to it. Python will figure out whether that variable will hold numbers, surfaces or carrots. You can change the assignment type anytime you want, although that might have unexpected results.<\/p>\n<p>Here are some examples of <strong>variable declaration and assignment:<\/strong><\/p>\n<pre class=\"lang:c# decode:true\"># Declare an variable and name it myVariable, assign a number\r\nmyVariable = 10\r\nmyVariable = 20.0\r\nmyVariable = \"20\"\r\nmyVariable = true\r\n\r\n# Although is good practice to indicate the type in the identifier\r\n# Declare a boolean\r\nmyBoolean = true\r\nmyOtherBoolean = false\r\nmyThirdBoolean = myBolean\r\n\r\n# Declare some doubles\r\nmyDouble = 12.33;\r\nmyOtherDouble = 1.0;\r\n\r\n# Strings come within quotes\r\nmyString = \"Hello World!\";\r\n\r\n# By the way, lines starting with # are not processed and are called comments<\/pre>\n<p>You should keep this in mind when naming variables:<\/p>\n<li>Python\u00a0is Case-Sensitive. This means you have to be careful when naming and calling your variables. <em>myString<\/em> will not be the same as <em>Mystring.<\/em><\/li>\n<li>A Variable identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).<\/li>\n<li>It is a good practice to use Camel Case:\u00a0The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example: myName, myNameAndSurname.<\/li>\n<li>Some words are reserved for other purposes and their use as variable identifiers is prohibited: \u00a0<em>and, exec, not,\u00a0assert, finally, or,\u00a0break, for, pass,\u00a0class, from, print,\u00a0continue, global, raise,\u00a0def, if, return,\u00a0<\/em><em>del, import, try,\u00a0elif, in, while,\u00a0else, is, with,\u00a0except, lambda, yield.<\/em><\/li>\n<h3>Outputting Values In Your Component<\/h3>\n<p>Let&#8217;s see a complete example of variable declaration, assignment and output.<\/p>\n<ol>\n<li>Create a new Custom Python\u00a0Component by double-clicking on the canvas and look for Python&#8230;<\/li>\n<li>Remove every input parameter by clicking the (-) sign after zooming in the component.<\/li>\n<li>Double-click on the center of the component.<\/li>\n<li>This should be your code:\n<pre class=\"lang:python decode:true\">myAge = 31\r\nmyName = \"Roberto\"\r\nmySurname = \"Molinos\"\r\nmySentence = \"Hello!, my name is \" + myName + \" \" + mySurname + \", and my age is \" + str(myAge)\r\nA = mySentence\r\n\r\n#This is a comment to show you how to use the out informer\r\nprint(\"You're too old!\")\r\n\r\n<\/pre>\n<p>Keep in mind that you should only edit the region inside the curly brackets, as the rest is controlled by GH itself.<\/li>\n<\/ol>\n<p>In the example above whe are doing the following:<\/p>\n<ol>\n<li>Line 1 declares a variable, by assigning 31 it will be treated as an integer.<\/li>\n<li>Line 2\u00a0and 3\u00a0declare two variables\u00a0to store my name and surname \u00a0as strings (&#8220;) and those are assigned.<\/li>\n<li>Line 4\u00a0declares a variable\u00a0and directly assigns the concatenation of several other strings. Those not being a variable are created by typing within quotes and the parts are joined by using the (+) operator. \u00a0myAge is converted to a string using the str() function.<\/li>\n<li>Finally, in line 5, the output value A gets a reference to mySentence.<\/li>\n<li>Line 6 is blank, which is OK.<\/li>\n<li>Line 7 is a comment as it starts with #.<\/li>\n<li>print() is a function to render a string through the out connector, and is very helpful for reflecting intermediate steps in our code.<\/li>\n<\/ol>\n<p><img decoding=\"async\" width=\"800\" height=\"350\" class=\"alignnone size-full wp-image-4058\" alt=\"102_01\" src=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/102_01.png\" srcset=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/102_01.png 800w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/102_01-480x210.png 480w, https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/102_01-768x336.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variables and Assignment After the introduction in our latest post now is time to get our feet wet with a fundamental concept: variables. What Is A Variable? Variables are portions of memory that represent a certain entity and that have a name. We use variables to hold information in order to read, write, modify or [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":27131,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[418,422],"class_list":["post-2092","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 102 - 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-102-variables-and-assignment\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Grasshopper Python 102 - Modelical\" \/>\n<meta property=\"og:description\" content=\"Variables and Assignment After the introduction in our latest post now is time to get our feet wet with a fundamental concept: variables. What Is A Variable? Variables are portions of memory that represent a certain entity and that have a name. We use variables to hold information in order to read, write, modify or [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/\" \/>\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:20:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-21T11:14:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.modelical.com\/wp-content\/uploads\/Posts_36_GH_4.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-102-variables-and-assignment\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-102-variables-and-assignment\\\/\"},\"author\":{\"name\":\"Roberto Molinos\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\"},\"headline\":\"Grasshopper Python 102\",\"datePublished\":\"2015-03-24T00:20:44+00:00\",\"dateModified\":\"2022-03-21T11:14:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-102-variables-and-assignment\\\/\"},\"wordCount\":549,\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-102-variables-and-assignment\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/Posts_36_GH_4-400x250-1.jpg\",\"keywords\":[\"Tec Grasshopper\",\"Tec Python\"],\"articleSection\":[\"Guidelines\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-102-variables-and-assignment\\\/\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-102-variables-and-assignment\\\/\",\"name\":\"Grasshopper Python 102 - Modelical\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-102-variables-and-assignment\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-102-variables-and-assignment\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/Posts_36_GH_4-400x250-1.jpg\",\"datePublished\":\"2015-03-24T00:20:44+00:00\",\"dateModified\":\"2022-03-21T11:14:09+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-102-variables-and-assignment\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/ghpython-102-variables-and-assignment\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/Posts_36_GH_4-400x250-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/Posts_36_GH_4-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 102 - 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-102-variables-and-assignment\/","og_locale":"en_US","og_type":"article","og_title":"Grasshopper Python 102 - Modelical","og_description":"Variables and Assignment After the introduction in our latest post now is time to get our feet wet with a fundamental concept: variables. What Is A Variable? Variables are portions of memory that represent a certain entity and that have a name. We use variables to hold information in order to read, write, modify or [&hellip;]","og_url":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/","og_site_name":"Modelical","article_publisher":"https:\/\/www.facebook.com\/Modelical\/","article_published_time":"2015-03-24T00:20:44+00:00","article_modified_time":"2022-03-21T11:14:09+00:00","og_image":[{"width":600,"height":375,"url":"https:\/\/www.modelical.com\/wp-content\/uploads\/Posts_36_GH_4.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-102-variables-and-assignment\/#article","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/"},"author":{"name":"Roberto Molinos","@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5"},"headline":"Grasshopper Python 102","datePublished":"2015-03-24T00:20:44+00:00","dateModified":"2022-03-21T11:14:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/"},"wordCount":549,"image":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/Posts_36_GH_4-400x250-1.jpg","keywords":["Tec Grasshopper","Tec Python"],"articleSection":["Guidelines"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/","url":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/","name":"Grasshopper Python 102 - Modelical","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/#primaryimage"},"image":{"@id":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/Posts_36_GH_4-400x250-1.jpg","datePublished":"2015-03-24T00:20:44+00:00","dateModified":"2022-03-21T11:14:09+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-102-variables-and-assignment\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.modelical.com\/en\/ghpython-102-variables-and-assignment\/#primaryimage","url":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/Posts_36_GH_4-400x250-1.jpg","contentUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/03\/Posts_36_GH_4-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\/2092","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=2092"}],"version-history":[{"count":0,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts\/2092\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media\/27131"}],"wp:attachment":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media?parent=2092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/categories?post=2092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/tags?post=2092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}