Python
  Home arrow Python arrow Page 2 - Getting Started With Python
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Getting Started With Python
By: Martin Tsachev
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2002-12-10

    Table of Contents:
  • Getting Started With Python
  • How Do I Get Python?
  • Control Structures
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Getting Started With Python - How Do I Get Python?


    (Page 2 of 4 )

    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


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway