Python
  Home arrow Python arrow Page 6 - Python 101 (part 3): A Twist In The Tail
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? 
PYTHON

Python 101 (part 3): A Twist In The Tail
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2001-06-13


    Table of Contents:
  • Python 101 (part 3): A Twist In The Tail
  • Here Comes A Hero
  • Making Friends And Influencing People
  • We Don't Need Another Hero
  • Looping The Loop
  • Twist And Turn
  • Within Range()
  • Just Passin' Through

  • 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


    Python 101 (part 3): A Twist In The Tail - Twist And Turn
    ( Page 6 of 8 )

    In most programming languages, a "for" loop is used to execute a set of statements a certain number of times. Unlike a "while" loop, which continues to run for so long as the specified conditional expression evaluates as true, a "for" loop comes with a specific limit on the number of times it can iterate.

    Python's "for" loop conforms to this basic requirement; however, as with most things in Python, there's a twist in the tail. A Python "for" loop is designed only to iterate over built-in "sequence objects" like strings and lists, and is structured like this:

    for temp_var in sequence_obj: do this!
    Or, in English, "take each element of the sequence sequence_obj, place it in the variable temp_var, and execute the indented code block on temp_var".

    An example might help to make this clearer:

    >>> superheroes = ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America'] >>> for myhero in superheroes: ... print myhero, "rocks!" ... Incredible Hulk rocks! Green Lantern rocks! Human Torch rocks! Captain America rocks! >>>
    In this case, I've first initialized a list containing four elements. Next, I've used a "for" loop to iterate through the list; on each successive iteration, one element of the list is assigned to the temporary variable "myhero" and then printed to the console via a print() call. Once all the elements of the list have been processed, the loop is automatically terminated.

    You can use a "for" loop with any "sequence object" - this next example does something similar with a string.

    >>> str = "abracadabra" >>> for char in str: ... print char, "_", ... a _ b _ r _ a _ c _ a _ d _ a _ b _ r _ a _ >>>


     
     
    >>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
     

       

    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 1 Hosted by Hostway
    Stay green...Green IT