You can download it for free from Python's web site. It's open source, so you can even modify its source code if you know C. There are source and precompiled binaries available for both Linux and Windows.
How Do I Install Python?
If you want to install the Windows version of Python, you just run the graphic installer and select some options. If you compile it for Linux/Unix then you'll have to do the usual three steps to complete it:
The last one usually needs to be run with root user privledges. You may also want to compile tk-inter support in, like this:
How Do I Run Python?
If you want to check something simple, run the Python interpreter by typing
...or following the Python shortcut in Windows. The interpreter isn't very useful for large pieces of code, so generally you would want to use your favourite text editor to create the scripts and then execute them.
How Do I Execute a Saved Script?
This depends on your which operating system you are using:
... at the top of the script and CHMOD it to have execute permissions, like this:
chmod +x foobar.py
If you want to run the script as a CGI (from a web server) then you should use #!/usr/local/bin/python or #!/usr/bin/python, depending on the system configuration.
To find out where Python is installed on your Linux web server, just type
... at the command line.
Run cmd.exe. If the .py extension is registered from the Python interpreter then you simply type the filename at the command prompt and Windows will associate it with Python and automatically execute it for you.
You can simply type
...at the command prompt if the python binary is in your path. If it isn't, then you will need to change into the Python directory and run it from there.
Which editor should you use for writing your Python scripts? It doesn't really matter, although one with syntax highlighting is preferred. Checkout HTML-Kit if you will be creating your scripts on Windows.
Enough Talk, Let's Start Coding!
Scripting languages such as PHP and Perl separate commands using semi-colons, brackets, etc. Python identifies a statement by indentation only. This is an important point, so make sure that you remember it!
Here's a basic Python script:
# 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 then you can escape the end of the line with a backslash:
foo = "I'm a very long string\
which spans multiple lines"
...or use HereDoc syntax with """ or ''':
foo = """I am a really long
comment but it doesn't
matter no backslashes are
You can also directly access 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
blog comments powered by Disqus