{"id":10,"date":"2016-06-26T02:45:32","date_gmt":"2016-06-26T02:45:32","guid":{"rendered":"http:\/\/edspace.com\/blog\/?p=10"},"modified":"2016-06-26T02:45:32","modified_gmt":"2016-06-26T02:45:32","slug":"explain-this-to-me-as-if-i-were-a-small-child-python-interactive-interpreter","status":"publish","type":"post","link":"http:\/\/edspace.com\/blog\/2016\/06\/26\/explain-this-to-me-as-if-i-were-a-small-child-python-interactive-interpreter\/","title":{"rendered":"Explain This To Me As If I Were a Small Child &#8211; Python Interactive Interpreter"},"content":{"rendered":"<p>I used to work with a guy who would, when trying to understand the mess of a design I just made, ask me to (facetiously), &#8220;explain it to me as if I were a small child.&#8221; I never liked teach things that way because it always felt insulting to the target audience. However, I sometimes find myself in situations where people I&#8217;m teaching say the following:<\/p>\n<p>Him: &#8220;Wait, I didn&#8217;t know you could do that!&#8221;<\/p>\n<p>Me: &#8220;Oh, I just thought you knew.&#8221;<\/p>\n<p>Now I&#8217;m not insulting, I&#8217;m a terrible teacher.<\/p>\n<p>So, let&#8217;s start from the beginning, as if you were a small child.<\/p>\n<p>Python is an interpreted language. It means that the script that you write isn&#8217;t compiled directly into machine code. It get sent into an interpreter that then translates it into machine code on the fly. We&#8217;ll come back to this in a second.<\/p>\n<p>What happens when you want to jump in and try a language? Usually, you start with your favorite editor (hopefully not Notepad), write your generic &#8220;Hello, World!&#8221; program, load that file into the interpreter, and marvel at what your fingertips hath wrought.<\/p>\n<p>Let&#8217;s do one better. Let&#8217;s launch the Python interactive interpreter.<\/p>\n<pre>$ python3\r\nPython 3.5.1+ (default, Mar 30 2016, 22:46:26) \r\n[GCC 5.3.1 20160330] on linux\r\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\r\n&gt;&gt;&gt;<\/pre>\n<p>Woah, what&#8217;s this? It&#8217;s an interactive Python shell. You can try Python code out live. Go ahead. Try it.<\/p>\n<pre>&gt;&gt;&gt; print(\"Hello, world!\")\r\nHello, world!\r\n&gt;&gt;&gt;<\/pre>\n<p>There. Hello, world, without having to touch Notepad. (You should really consider a better text editor).<\/p>\n<p>What&#8217;s the point of this? You can, of course, learn about the language and try out new reserved keywords. It&#8217;s also good for trying out new libraries, seeing the exact format a function returns.<\/p>\n<p>When you&#8217;re working with a new library, it would be nice to know what classes are in a package, or what methods are in a class. Traditionally, you would call <em>dir()<\/em> to see what is available.<\/p>\n<pre>&gt;&gt;&gt; import json\r\n&gt;&gt;&gt; dir(json)\r\n['JSONDecodeError', 'JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']<\/pre>\n<p>It would be nice if we could get some tab completion in here. Enter <em>ipython<\/em>. You can get it via <em>apt, yum, <\/em> or <em>pip.<\/em><\/p>\n<pre>$ ipython3\r\nPython 3.5.1+ (default, Mar 30 2016, 22:46:26) \r\nType \"copyright\", \"credits\" or \"license\" for more information.\r\n\r\nIPython 2.4.1 -- An enhanced Interactive Python.\r\n? -&gt; Introduction and overview of IPython's features.\r\n%quickref -&gt; Quick reference.\r\nhelp -&gt; Python's own help system.\r\nobject? -&gt; Details about 'object', use 'object??' for extra details.\r\n\r\nIn [1]: import json\r\n\r\nIn [2]: json.\r\njson.JSONDecodeError json.dump json.loads\r\njson.JSONDecoder json.dumps json.scanner\r\njson.JSONEncoder json.encoder \r\njson.decoder json.load \r\n\r\nIn [2]: json.<\/pre>\n<p>Tab completion!<\/p>\n<p>Now get out there and start exploring.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I used to work with a guy who would, when trying to understand the mess of a design I just made, ask me to (facetiously), &#8220;explain it to me as if I were a small child.&#8221; I never liked teach things that way because it always felt insulting to the target audience. However, I sometimes &hellip; <a href=\"http:\/\/edspace.com\/blog\/2016\/06\/26\/explain-this-to-me-as-if-i-were-a-small-child-python-interactive-interpreter\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Explain This To Me As If I Were a Small Child &#8211; Python Interactive Interpreter<\/span><\/a><\/p>\n","protected":false},"author":1,"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}},"categories":[3,2],"tags":[4],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-explain-this-to-me","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paDvvN-a","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":1,"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":11,"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions\/11"}],"wp:attachment":[{"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edspace.com\/blog\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}