Consistently Infrequent

December 19, 2011

Python: Hello, World!

Filed under: Python — Tony Breyal @ 9:57 pm


Stanford is running a series of open online courses this January. One of these courses is about Text Mining (aka Natural Language Processing, or NLP for short). There is a pre-requisite in this course for being able to programme in either Java or Python.

I was going to spend my Christmas break re-learning C++ but as I really want to try this course out I’m instead going to try and learn Python by following this online Google class because it’s a language I often hear about from other R users.  Having done the first two modules of that google course I thought I should code a quick ‘hello world’ programme on my blog, for the sake of geekery if nothing else.


Write some python code which will print out “Hello, world!”.


Ubuntu Linux already comes with python pre-installed by the looks of it so I didn’t need to do anything special. I downloaded the Spyder IDE because it’s the closest thing to RStudio (which I now use when coding in R) that I could see and comes highly recommended based on the various web sites I visited. Anyway, here’s the code I entered into the script window of the Spyder IDE. To run it, I pressed F5 which prompted me to save the file and after which “Hello, world!” was printed to the integrated console :

def main():
  print 'Hello, world!'

if __name__ == '__main__':

Line 1 tells us that we have defined [def] a function called main() and it’s body starts after the colon [:].

Line 2 is indented to show that it belongs to main(). This is VERY important because unlike some other programming languages, python does not have curly braces “{” and “}” which tell us where a function starts and ends but instead uses the indentation to mark the boundaries (so this formatting is not optional). I’m not sold on this concept yet though I suppose it does save a bit on having to type in the curly braces explicitly because I would normally indent my code anyway.

Line 4 and 5 tells us that this file (lines 1-5) can be used as either a module for import into another python module or as a stand-alone programme. This seems to be required in every python file and so I guess I had better get used to it. When I run this file it is recognised as a standalone programme and starts off by calling the main() function which is used on line 5.



  1. […] Python: Hello, World! ( *{margin:0; padding:0;} #socialbuttonnav li{background:none;overflow:hidden;width:65px; height:80px; line-height:30px; margin-right:2px; float:left; text-align:center;} #fb { text-align:center;border:none; } […]

    Pingback by Python Programming Language Advantages - Mike Levin — March 5, 2012 @ 10:43 pm

    • One prefers not to make such an assumption.

      Comment by Harriet — July 16, 2012 @ 6:27 pm

  2. Thanks for another fantastic article. The place else may just anybody get that kind
    of info in such a perfect method of writing? I have a presentation subsequent week,
    and I’m at the look for such information.

    Comment by transfer avi to dvd — March 30, 2015 @ 12:43 am

  3. Hii..Your posting about the hello world program for thePython is really much more informative and useful to all …Thanks for sharing these types of informative..

    Comment by softwaretrainingweb — March 2, 2017 @ 9:59 am

  4. Hi there everyone, it’s my first visit at this web page,
    and post is really fruitful in favor of me, keep up posting such articles
    or reviews.

    Comment by good hosting service in kerala — July 15, 2018 @ 8:34 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

%d bloggers like this: