{"id":98,"date":"2008-03-19T23:31:44","date_gmt":"2008-03-19T18:01:44","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/2008\/03\/python-hello-script\/"},"modified":"2008-03-19T23:31:44","modified_gmt":"2008-03-19T18:01:44","slug":"python-hello-script","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2008\/03\/python-hello-script\/","title":{"rendered":"Python Hello Script"},"content":{"rendered":"<p><a href='http:\/\/www.bin-co.com\/blog\/2008\/03\/python-hello-script\/python-logo\/' rel='attachment wp-att-97' title='Python Logo'><img src='http:\/\/www.bin-co.com\/blog\/wp-content\/uploads\/2008\/03\/python-logo.gif' alt='Python Logo' class=\"intro\" align=\"right\" \/><\/a><\/p>\n<p class=\"intro\">As promised in the last post(<a href=\"http:\/\/www.bin-co.com\/blog\/2008\/03\/hello-script-for-php\/\">Hello Script for PHP<\/a>), this is my Hello Script for <a href=\"http:\/\/www.python.org\/\">Python<\/a>. <strong class=\"highlight\">&#8216;<a href=\"http:\/\/www.bin-co.com\/blog\/2008\/03\/learning-a-new-programming-language-the-hello-world-method\/\">Hello Script<\/a>&#8216; is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet<\/strong> when working with that language.<\/p>\n<p>A word of caution here &#8211; the blocks in Python is created using whitespace &#8211; so the indentation is important. So when you see an indentation in the below code, think of it as one tab(instead of four spaces or something).<\/p>\n<p>I want to insert a disclaimer here &#8211; I am not that good with python. We <a href=\"http:\/\/www.bin-co.com\/blog\/2007\/10\/tk-verses-gtkand-python-verses-tcl\/\" title=\"\tTk Verses Gtk(And Python verses Tcl)\">never really clicked<\/a>. I have done very limited <a href=\"http:\/\/www.bin-co.com\/python\/programs\/frees\/\" title=\"Frees - PyGTK Tool to show HardDisk Space Usage\">work in Python<\/a>. So if you notice any problems with the below script, let me know and I&#8217;ll correct it.<\/p>\n<pre><code class=\"python\">\n#!\/usr\/bin\/python\n\nprint \"Hello World\\n\"\n\nname = \"Binny\"\nyear = 2008\nprint \"Hello, \" + name + \" - welcome to \" + str(year) + \"\\n\"\n\n# If,else conditions\nif (year &gt; 2008):\n\tprint \"Welcome to the future - yes, we have flying cars!\\n\"\nelif(year &lt; 2008):\n\tprint \"The past - please don't change anything. Don't step on any butterflies. And for the sake of all thats good and holy, stay away from your parents!\\n\"\nelse:\n\tprint \"Anything wrong with your time machine? You have not gone anywhere, kiddo.\\n\"\n\n# For loop\nfor i in range(0,3):\n\tprint str(i) + \") Hi there!\"\n\nprint \"\"\n\n#Numerical Array, While\nrules = ['Do no harm','Obey','Continue Living']\ni = 0\nwhile(i&lt;len(rules)):\n\tprint \"Rule \" + str(i+1) + \" : \" + rules[i]\n\ti = i + 1\nprint \"\"\n\n# Associated array, foreach\nassociated = {\n\t'hello'\t:\t'world',\n\t'foo'\t:\t'bar',\n\t'lorem'\t:\t'ipsum'\n}\nfor key in associated:\n\tprint key + \" : \" + associated[key]\nprint \"\"\n\nimport string\ncsv_values = string.split(\"hello,world,how,are,you\\n\", \",\")\nprint string.join(csv_values, \":\")\n\n\n# Function, argument, return, call\ndef hello(name):\n\treturn \"Hello \" + name + \"\\n\"\n\nhello_string = hello(\"Binny\")\nprint hello_string\n\n# One for the OOP fanboys - Class, members, object and stuff.\nclass Movie:\n\tname = ''\n\trating = 0\n\t\n\tdef __init__(self, name):\n\t\tself.name = name\n\t\tself.rateMovie()\n\n\tdef rateMovie(self):\n\t\tself.rating = (len(self.name) % 10) + 1 #IMDBs rating algorithm. True story!\n\t\n\tdef printMovieDetails(self):\n\t\tprint \"Movie : \",  self.name\n\t\tprint \"Rating : \", '*' * self.rating , \"(\", self.rating ,\")\\n\"\n\t\n\n#Create the object\nncfom = Movie(\"New Country for Old Men\") #It's a sequel!\nncfom.printMovieDetails()\n\n# File IO\n# File reading, easy method...\nfile_in  = open('Hello.py', 'r')\ncontents = file_in.read()\nprint \"Current file has \" + str(len(contents)) + \" chars\\n\"\nfile_in.close()\n# Writing to a file\nfile_out = open('\/tmp\/hello.txt', 'w')\nfile_out.write(\"Hello World\")\nfile_out.close()\n\n# Command Executing\nimport commands\nimport os\nprint \"Result of 'ls' command is \" + commands.getoutput('ls') #Execute the command 'ls' and print its output\nprint\n\n# Regular Expressions\nimport re\nhell_check = re.compile(\"^Hell\")\nstring = \"Hello World\"\nif hell_check.match(string): print \"Yup - its evil (Compiled)\"\nif re.match('^Hell', string): print \"Yup - its evil (Not Compiled)\"\nprint re.sub(r'l([^l])', r'\\1', string)\n\n<\/code><\/pre>\n<p>As I said last time, save this to a file and keep it around for future reference.<\/p>\n<p>Next hello script &#8211; Perl, Ruby or Tcl\/Tk? Which one do you want &#8211; leave it in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>As promised in the last post(Hello Script for PHP), this is my Hello Script for Python. &#8216;Hello Script&#8216; is a file that contains the most <a class=\"mh-excerpt-more\" href=\"https:\/\/www.bin-co.com\/blog\/2008\/03\/python-hello-script\/\" title=\"Python Hello Script\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,28],"tags":[80,106,137,225,245,289],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-scripts","category-tutorial","tag-code","tag-example","tag-hello","tag-python","tag-script","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/98","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}