{"id":1048,"date":"2017-12-11T12:23:56","date_gmt":"2017-12-11T17:23:56","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1048"},"modified":"2018-07-05T19:56:10","modified_gmt":"2018-07-06T00:56:10","slug":"python-and-ruby-a-comparison-of-the-language-basics","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/12\/11\/python-and-ruby-a-comparison-of-the-language-basics\/","title":{"rendered":"Python and Ruby: 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 Ruby 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%\">Ruby<\/th>\n<\/tr>\n<tr>\n<td><strong>Commenting code: Both use the pound # sign.<br \/>\n<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># This is a comment<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\"># This is a comment<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Multi-line comments:<br \/>\n<\/strong><\/td>\n<td>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\"\"\"\r\nThis is a comment.\r\nThis is a comment, too.\r\nThis is a comment, too.\r\n\"\"\"<\/pre>\n<\/td>\n<td>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">=begin\r\nThis is a comment.\r\nThis is a comment, too.\r\nThis is a comment, too.\r\n=end<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td><strong>Identifying variable type:<br \/>\n<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">type(variable)<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">variable.class<\/code>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td><strong>Creating a variable that contains an int (Fixnum in Ruby):<\/strong><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">age = 25<\/code><\/td>\n<td><code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">age = 25<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">negative_number = -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=\"ruby\">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=\"ruby\">state = 'North Dakota'<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">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. Ruby requires backslashes to escape characters.<\/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=\"ruby\">path = \"\\\\path\\\\to\\\\some\\\\files\"<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">path = '\\\\path\\\\to\\\\some\\\\files'<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\"># prints That's right\r\nsingle_quotes1 = 'That\\'s right'\r\ndbl_quotes1 = \"That's right\"\r\n\r\n# prints escape using \\\r\nsingle_quotes2 = 'escape using \\\\'\r\ndbl_quotes2 = \"escape using \\\\\"<\/pre>\n<\/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=\"ruby\">greeting = \"Hello\" + \" world\"<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">greeting = \"Hello\" &lt;&lt; \" world\"<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">greeting = \"Hello\"\r\ngreeting.concat(\" World\")<\/pre>\n<\/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=\"ruby\">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=\"ruby\">length = fruit_list.length<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">length = fruit_list.size<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Python dictionary vs. Ruby hash:<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=\"ruby\">fruit = {\"a\" =&gt; \"apple\", \"b\" =&gt; \"banana\", \"c\" =&gt; \"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=\"ruby\">fruit_keys = fruit.keys<\/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=\"ruby\">fruit_values = fruit.values<\/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=\"ruby\">fruit[:'o'] = 'orange'<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">fruit[:l] = 'lemon'<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">fruit['o'] = 'orange'<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">fruit.store(\"g\", \"grape\")<\/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=\"ruby\">fruit.delete(:'o')<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">fruit.delete('g')<\/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>Ruby was invented in the mid-1990s. According to Wikipedia:<\/p>\n<blockquote><p>Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro &#8220;Matz&#8221; Matsumoto in Japan.<\/p>\n<p>According to its creator, Ruby was influenced by Perl, Smalltalk, Eiffel, Ada, and Lisp. It supports multiple programming paradigms, including functional, object-oriented, and imperative. It also has a dynamic type system and automatic memory management.<\/p><\/blockquote>\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 Ruby programming languages. Programming element Python Ruby Commenting code: Both use the pound # sign. # This is a comment # This is a comment Multi-line comments: &#8220;&#8221;&#8221; This is a comment. This is a comment, too. This is &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/12\/11\/python-and-ruby-a-comparison-of-the-language-basics\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python and Ruby: 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":[75],"tags":[4,76],"class_list":["post-1048","post","type-post","status-publish","format-standard","hentry","category-ruby-on-rails","tag-python","tag-ruby"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1048","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=1048"}],"version-history":[{"count":33,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1048\/revisions"}],"predecessor-version":[{"id":2968,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1048\/revisions\/2968"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}