Home arrow Python arrow Page 2 - Getting Started With Python

How Do I Get Python?&toc - Python

Yes, Python is a type of snake, but it's also a little-known scripting language that you can use to create entire web sites. In this article Martin introduces us to the wonderful world of Python...

TABLE OF CONTENTS:
  1. Getting Started With Python
  2. How Do I Get Python?
  3. Control Structures
  4. Conclusion
By: Martin Tsachev
Rating: starstarstarstarstar / 17
December 10, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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:

./configure
make
make install


The last one usually needs to be run with root user privledges. You may also want to compile tk-inter support in, like this:

./configure --with-tk

How Do I Run Python?
If you want to check something simple, run the Python interpreter by typing

python

...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:

Linux/Unix
Put

#!/usr/bin/env python

... 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

where python

... at the command line.

Windows NT/2000
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.

Windows 9x/ME
You can simply type

python foobar.py

...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:

#!/usr/bin/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 then 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 directly access single characters or slices from a string:

foo = "Hello"
print foo[1] # outputs "e" since indexing starts at 0
print foo[1:] # ello
print foo[-1:] # o
print foo[2:4] # ll


 
 
>>> More Python Articles          >>> More By Martin Tsachev
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: