SunQuest
 
       Python
  Home arrow Python arrow Page 3 - 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 
 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 - Control Structures


    (Page 3 of 4 )

    As with all other scripting languages, Python has its own set of control structures that allow you to determine a path thru which the execution of a script will proceed:

    if 1 == 2 :
         print 'One equals two'
    else :
         print 'One does not equal two'


    As you can see, no braces are used -- just indenting. This promotes readable code:

    if 1 < 2 :
         print 'One is lesser than two'
    elif 1 > 2 :
         print 'One is greater than two'


    Loops
    Loops allow you to iterate (repeat) a set of commands using a different set of values each time that command is executed. A simple loop looks like this in Python:

    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 left after dividing the number by two.

    This same example can also be rewritten as 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 do have to convert them to a single type if you want to operate on different kind of variables.

    To save the space that print adds between different parameters passed to it, you also have to add it to the string (when using string concatenation).

    Command-line Parameters
    If you want to pass parameters from the command line to your Python script, then you will need the sys module, which is imported on the first line of the example shown below. The module defines the argv list, which, as all of you C/C++ programmers will know, contains the arguments that are passed from the command-line to the script:

    import sys

    print sys.argv[0] # prints the filename of your script
    print sys.argv[1] # and the first parameter
    print sys.argv[1:] # or all parameters

    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 5 hosted by Hostway