{"id":268,"date":"2017-08-15T22:58:50","date_gmt":"2017-08-16T03:58:50","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=268"},"modified":"2018-07-05T19:56:48","modified_gmt":"2018-07-06T00:56:48","slug":"python-and-perl-a-comparison-of-the-language-basics","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/15\/python-and-perl-a-comparison-of-the-language-basics\/","title":{"rendered":"Python and Perl: 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 Perl 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%\">Perl<\/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=\"python\">%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=\"python\">%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. Perl supports only double 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=\"python\">%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. Single quotes always mean string literal in Perl. Double quotes in Perl allow for interpolations such as &#8216;\\n&#8217;, &#8216;\\t&#8217;, etc..<\/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><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">%path = '\\path\\to\\some\\files';<\/code><\/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><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">$greeting = \"Hello\" . \" world\";<\/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=\"python\">@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=\"python\">$length = @fruit_list;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Creating a dictionary or hash:<\/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=\"python\">%fruit = (\"a\", \"apple\", \"b\", \"banana\", \"c\", \"cherry\");<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">%fruit = (\"a\" =&gt; \"apple\", \"b\" =&gt; \"banana\", \"c\" =&gt; \"cherry\");<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Getting keys and values from a dictionary or hash:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_keys = fruit.keys()<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_values = fruit.values()<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">@fruit_keys = keys %fruit;<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">@fruit_values = values %fruit;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Adding and deleting key-value pairs to a dictionary or hash:<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit['o'] = 'orange'<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">del fruit['c']<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">$fruit{'o'} = \"orange\";<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">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. Perl was also invented in the late 1980s (1987) by Larry Wall as a general-purpose Unix scripting language to make report processing easier. Perl is an acronym that stands for <strong>P<\/strong>ractical <strong>E<\/strong>xtraction <strong>R<\/strong>eport <strong>L<\/strong>anguage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this table I will compare some basic programming syntax and conventions between the Python and Perl programming languages. Programming element Python Perl Creating a variable that contains an int: age = 25 %age = 25; Creating a variable that contains a float: probability = 0.62 %probability = 0.62; Creating a variable that contains a &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/15\/python-and-perl-a-comparison-of-the-language-basics\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python and Perl: 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":[96],"tags":[23,4],"class_list":["post-268","post","type-post","status-publish","format-standard","hentry","category-perl-language","tag-perl","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/268","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=268"}],"version-history":[{"count":61,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":2111,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/268\/revisions\/2111"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}