Hello world in Python

A "Hello world" program is the customary way to show how to program in a new language. Although the Python's hello world is trivially simple, there is always something to learn from it


Installing Python

Depending on your operating system, you may need to download Python.  Most Linux distributions come with Python as does Mac OS X.  Windows does not ship with Python.  To test if Python is installed, open a command prompt and enter:
python
If Python is installed you will see the Python interactive interpreter, if not you will receive an error message.  To install Python go to http://www.python.org/download/ and download the appropriate file.  Follow the instructions to install Python.

Hello world in Python (prior version 3):


Writing a "Hello word" program in Python is trivial:
print "Hello world"
In the interactive interpreter, it looks like this:


This Knol also introduces the print command:

The print command

A print command in Python adds a line feed, so with two prints you will get two lines:

print "Hello"
print "world"

Hello
world

This can be avoided by using a comma (,) at the end:

print "Hello",
print "world"  

Hello world

Hello world using variables:

>>> h = "Hello"
>>> w = "world"
>>> print h+' '+w
Hello world

Note that the "+" operation concatenates three strings: Hello, a "space" character and "world".

Printing with placeholders


>>> print "%s %s" %(h,w)
Hello world
By using "%s" you don't have to concatenate strings, just use one string and put a placeholder (the %s) where the variable would go.


Python 3

In Python 3, the print statement is transformed into a function, so a basic hello world, is:

print("Hello world")
There are also some new parameters for the print function: "end" and "sep." The "end" parameter is used to determine what goes at the end of the statement. It defaults to " " (a newline character), exactly like in Python 2.x. The "sep" parameter determines the separator character between the other parameters. It defaults to a space, again like in 2.x. Some examples of utilizing these new parameters:
>>> print("hello","world")
hello world
>>> print("hello","world",sep="separator-text")
## instead of a space, it's "seperator-text" helloseparator-textworld
>>> print("hello","world",end="this is the ending.")
hello worldthis is the ending


Videos


Introduction to Python (from download to "Hello world")


Lean more


There are plenty of manuals and tutorials on the web to learn Python. Here are some recommended readings.

For beginners:



For programmers:


Dive into Python, by Mark Pilgrim
A Primer on Python for Life Science Researchers, by Sebastián Bassi




Comments

Sebastian Bassi
Sebastian Bassi
Bioinformatics
Bernal
Article rating:
Your rating:
Moderated collaboration
All signed in users can suggest edits to the knol, but these need approval from an author before being published
Version: 16
Versions
Last edited: May 21, 2009 4:42 PM.

Reviews

    Knol translations

    Categories

    Based on community consensus.

    Activity for this knol

    This week:

    21pageviews

    Totals:

    868pageviews