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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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? 
Google.com  
PYTHON

Getting Started With Python
By: Martin Tsachev
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek