{"id":5130,"date":"2018-01-29T22:32:25","date_gmt":"2018-01-29T22:32:25","guid":{"rendered":"https:\/\/really.zonky.org\/?p=5130"},"modified":"2018-02-07T07:30:23","modified_gmt":"2018-02-07T07:30:23","slug":"python-not-as-simple-as-basic","status":"publish","type":"post","link":"https:\/\/really.zonky.org\/?p=5130","title":{"rendered":"Python: Not As Simple As BASIC?"},"content":{"rendered":"<p>I recently dived into the rabbit hole of educational computers and came across a site which made a big song and dance about how <a href=\"http:\/\/python.org\/\">Python<\/a> is a great deal more complicated than <a href=\"https:\/\/en.wikipedia.org\/wiki\/BASIC\">BASIC<\/a>. Well that is perhaps arguably correct, but the comparison they made was grossly unfair :-<\/p>\n<pre>#!\/usr\/bin\/env python\r\n#-*- coding: UTF-8 -*-\r\n\r\nfrom random import randint\r\nfrom time import sleep\r\nimport sys\r\n\r\nstring = \"Hello World!\"\r\nwhile true:\r\n  attr = str(randint(30,48))\r\n  out = \"\\x1b[%sm%s\\x1b[0m\" % (attr, string)\r\n  sys.stdout.write(out)\r\n  sys.stdout.flush()\r\n\r\n  sleep(1)<\/pre>\n<p>Now for the criticisms :-<\/p>\n<ol>\n<li>The first line (&#8220;#!\/usr\/bin\/env &#8230;&#8221;) is nothing to do with Python; and in fact a BASIC program should also include this if it wants to run in the same way as a Python program under Linux. The &#8220;#!&#8221; is in fact a directive to the Linux kernel to tell it what script to pass the rest of the file through.<\/li>\n<li>The second line (&#8220;# -*-&#8230;&#8221;) also has nothing to do with Python; it is a directive to an editor to tell it to use the UTF-8 character set. Why doesn&#8217;t the basic equivalent also include this?<\/li>\n<li>Now onto the Python itself &#8230; first of all there are a whole bunch of imports which are done in the verbose way just so that you can call\u00a0<em>sleep<\/em> rather than\u00a0<em>time.sleep<\/em>; I generally prefer the later (which would result in the\u00a0<em>import time<\/em> rather than\u00a0<em>from time import sleep<\/em>). But yes, in Python you have to import lots of stuff to get anything done, and it would be helpful for quick and dirty scripts if you could just\u00a0<em>import lots<\/em> to get a fair amount of ordinary stuff loaded.<\/li>\n<li>The rest of the code is &#8230; um &#8230; obviously designed to make Python look bad and glossing over the fact that Python runs in the Linux runtime environment whereas the BASIC equivalent does not &#8211; it has a BASIC runtime environment.<\/li>\n<\/ol>\n<p>That last point is worth going into more detail on &#8211; the BASIC code was written for a BASIC runtime environment, and\u00a0<em>one<\/em> method of sending output to the screen. Linux has\u00a0<em>many<\/em> ways of writing to the screen, and the chosen method above is perhaps historically the worst (it only works for devices that understand the escape sequences; there is a\u00a0<em>curses<\/em> library for doing this properly).<\/p>\n<p>So is Python unsuited to a quick and easy learning environment? A quick hackers language? As it is, perhaps not, but that is not quite what Python is designed to be. And with a suitable set of modules, Python\u00a0<em>could<\/em> be suitable :-<\/p>\n<pre>import lots\r\n\r\nwhite True:\r\n  screen.ink(random.choice(inkcolours))\r\n  screen.paper(random.choice(papercolours))\r\n  screen.print(\"Hello World!\")\r\n\r\n  time.sleep(1)\r\n<\/pre>\n<p>(That&#8217;s entirely hypothetical of course as there is no &#8220;screen&#8221; module)<\/p>\n<p>I&#8217;m not qualified to judge whether BASIC or Python are better languages for beginners &#8211; I&#8217;ve been programming for around 35 years, and the BASIC I remember was very primitive. But at least when you compare the two languages, make the comparison a fair one.<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3904\" src=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt1.png?resize=695%2C463&#038;ssl=1\" alt=\"\" width=\"695\" height=\"463\" srcset=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt1.png?w=792&amp;ssl=1 792w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt1.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently dived into the rabbit hole of educational computers and came across a site which made a big song and dance about how Python is a great deal more complicated than BASIC. Well that is perhaps arguably correct, but the comparison they made was grossly unfair :- #!\/usr\/bin\/env python #-*- coding: UTF-8 -*- from <a href='https:\/\/really.zonky.org\/?p=5130' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0"},"categories":[4],"tags":[315,407],"class_list":["post-5130","post","type-post","status-publish","format-standard","hentry","category-it","tag-basic","tag-python","category-4-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1f2KI-1kK","_links":{"self":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/5130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5130"}],"version-history":[{"count":5,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/5130\/revisions"}],"predecessor-version":[{"id":5135,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/5130\/revisions\/5135"}],"wp:attachment":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}