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:
In the interactive interpreter, it looks like this:print "Hello world"
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
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.>>> print "%s %s" %(h,w)
Hello world
Python 3
In Python 3, the print statement is transformed into a function, so a basic hello world, is: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")
>>> 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:
- A Byte of Python, by Swaroop C H.
- The Python Tutorial, the official tutorial.
For programmers:
Dive into Python, by Mark Pilgrim
A Primer on Python for Life Science Researchers, by Sebastián Bassi









Comments
Write New Comment ▼
Write New Comment
Sorry! This knol's owner(s) have blocked you from editing, making suggestions, or commenting here.