Python is a somewhat unpopular language that is often compared to Perl (from people who know it exists). So what exactly is Python - it is a scripting language, this means it is interpreted rather than compiled, so it saves time during debugging and early development.
Python is a high level language - this means that you can write big programs very fast and easier than with low level languages like C/C++.
You can download it for free from Python's website, it is Open Source so you can even modify it's source, at least if you know C. There is a source version and also precompiled binaries for both Linux and Windows.
If you want to install the Windows version you just run the graphic installer and select some options. If you compile it for Linux/Unix you'll have to do the usual three steps to complete it:
./configure make make install
The last one usually requires to be run as root, you may also want to compile tk-inter support in,
If you want to check something simple run the Python interpreter by typing
following a shortcut (Windows). The interpreter isn't very useful for large pieces of code so generally
you would want to use your favorite text editor to create the scripts and then execute them.
This depends on your OS:
#!/usr/bin/env pythonat the top of the script and do
chmod +x foobar.py. If you want to run the script as a CGI (from a webserver) you should better use
#!/usr/bin/python, depending on the system configuration.
cmd.exe, if the
.pyextension is registered from the Python interpreter you can go with typing the filename.
python foobar.pyif the python binary is in your path.
What editor do you use for writing the scripts doesn't really matter although one with syntax highlighting is preferred. I only know one editor for Windows/Linux: it is SciTE, and one which is Unix only - Kate or its predecessor KWrite (both bundled with KDE).
Only one thing before that in other languages statements are separated by braces and such - Python identifies statements by indentation only.
#!/usr/bin/env python # This is a comment # comments can span one line only print 'Hello world!' print "Hello world!"
You can delimit strings with single or double quotes - it doesn't make any difference to Python. If you want to write a string on more than one line you can escape the end of the line with a backslash:
foo = "I'm a very long string which spans multiple lines" print foo
or use HereDoc syntax with """ or ''':
foo = """I am a really long comment but it doesn't matter no backslashes are needed """
You can also access directly single characters or slices from a string
foo = "Hello" print foo # outputs "e" since indexing starts at 0 print foo[1:] # ello print foo[-1:] # o print foo[2:4] # ll
if 1 == 2 : print 'One equals two' else : print 'One does not equal two'
As you have noticed no braces are used, just indent, this promotes readable code.
if 1 < 2 : print 'One is lesser than two' elif 1 > 2 : print 'One is greater than two'
for i in range(0, 10) : if i % 2 == 0 : print i, 'is an even number' else : print i, 'is an odd number'
This example will loop through the numbers from 0 to 10 and test if the number is odd or even.
The modulus operator (
%) is used to calculate the remainder from dividing the
number by two.
The same example can be rewritten also with a while loop.
i = 0 while i <= 10 : if i % 2 == 0 : print str(i) + ' is an even number' else : print str(i) + ' is an odd number' i += 1 i = 'Hello world' print i
This example also shows that even though Python's variables are loosely typed (you can assign any type of data to a single variable), you have to convert them to a single type if you want to operate on different kind of variables.
To save the space that
import sys print sys.argv # prints the filename of your script print sys.argv # and the first parameter print sys.argv[1:] # or all parameters
If you want to pass parameters from the command line you will need the
which is imported on the first line of the example. The module defines the
which contains the arguments to the script.
Lists in Python are something similar to arrays in other languages yet Python has arrays also, which
are more functional than lists so if you want to use arrays you would have to import the