{"id":640,"date":"2017-10-08T14:19:19","date_gmt":"2017-10-08T19:19:19","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=640"},"modified":"2018-07-05T19:50:18","modified_gmt":"2018-07-06T00:50:18","slug":"python-and-javascript-a-comparison-of-the-language-basics","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/10\/08\/python-and-javascript-a-comparison-of-the-language-basics\/","title":{"rendered":"Python and JavaScript: A comparison of the language basics"},"content":{"rendered":"<p>In this table I will compare some basic programming syntax and conventions between the Python and JavaScript programming languages.<\/p>\n<table width=\"60%\" id=\"lang-compare-table\">\n<tbody>\n<tr>\n<th width=\"30%\">Programming element<\/th>\n<th width=\"35%\">Python<\/th>\n<th width=\"35%\">JavaScript<\/th>\n<\/tr>\n<tr>\n<td><strong>Creating a variable that contains an int:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">age = 25<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var age = 25;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Creating a variable that contains a float:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">probability = 0.62<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var probability = 0.62;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Creating a variable that contains a string:<\/strong><br \/>\nPython supports single, double, and triple quotes.<\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">state = 'North Dakota'<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">state = \"North Dakota\"<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">state = \"\"\"North Dakota\"\"\"<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var state = \"North Dakota\";<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Denoting string literals:<\/strong><br \/>\nPython uses an &#8216;r&#8217; in front of the string.<\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">path = r'\\path\\to\\some\\files'<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">path = r\"\\path\\to\\some\\files\"<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">path = r\"\"\"\\path\\to\\some\\files\"\"\"<\/code><\/td>\n<td>No string literals. Special characters must be escaped.<\/td>\n<\/tr>\n<tr>\n<td><strong>String Concatenation:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">greeting = \"Hello\" + \" world\"<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">greeting = str1 + str2<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var greeting = \"Hello\" + \" world\";<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var greeting = str1 + str2;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Creating a list or array:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_list = ['apple', 'banana', 'cherry']<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var fruit_list = [\"apple\", \"banana\", \"cherry\"];<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Getting the length of a list or array:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">length = len(fruit_list)<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var length = fruit_list.length;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Python dictionary vs. JavaScript object:<br \/>\n<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit = {'a':'apple', 'b':'banana', 'c':'cherry'}<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var fruit = { \"a\": \"apple\", \"b\": \"banana\", \"c\": \"cherry\" };<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Getting keys from a dictionary or object:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_keys = fruit.keys()<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var fruit_keys = Object.keys(obj);<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var fruit_keys = [index for (index in fruit)];<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Getting values from a dictionary or object:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_values = fruit.values()<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var fruit_values = [fruit[index] for (index in fruit)];<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Adding key-value pairs to a dictionary or object:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit['o'] = 'orange'<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">fruit[\"o\"] = \"orange\";<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var o = \"o\";<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">fruit.o = \"orange\";<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Deleting key-value pairs from a dictionary or object:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">del fruit['c']<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">var key = \"c\";<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">delete fruit[key];<\/code><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">delete fruit[\"c\"];<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"javascript\">delete fruit.c;<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Python was invented in the late 1980s (1989) by Guido van Rossum. Ideas for Python were adopted from Modula-3 and Lisp.<\/p>\n<p>JavaScript first appeared in web browsers in 1995 and was written by Brendan Eich, who worked for Mosaic Netscape, while creating the Netscape browser code named &#8220;Mozilla&#8221;. JavaScript was influenced by programming languages such as Self and Scheme, and was originally named LiveScript. It is thought that it was rebranded &#8220;JavaScript&#8221; as a <a href=\"https:\/\/en.wikipedia.org\/wiki\/JavaScript#Beginnings_at_Netscape\">marketing ploy by Netscape<\/a> to give JavaScript the cachet of what was then the hot new programming language &#8220;Java&#8221;. However, JavaScript and Java are completely distinct programming languages with little in common.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this table I will compare some basic programming syntax and conventions between the Python and JavaScript programming languages. Programming element Python JavaScript Creating a variable that contains an int: age = 25 var age = 25; Creating a variable that contains a float: probability = 0.62 var probability = 0.62; Creating a variable that &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/10\/08\/python-and-javascript-a-comparison-of-the-language-basics\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python and JavaScript: A comparison of the language basics<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[45,4],"class_list":["post-640","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-javascript","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=640"}],"version-history":[{"count":23,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/640\/revisions"}],"predecessor-version":[{"id":2104,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/640\/revisions\/2104"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}