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

    - 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek