Python
  Home arrow Python arrow Page 7 - 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 - Within Range()
    ( Page 7 of 8 )

    While on the topic of the "for" loop, it's worth mentioning the range() function, a built-in Python function whose sole raison d'etre is to return a range of numbers, given a starting and ending point. This range is always returned as a list - and as you'll see, this can combine quite effectively with the "for" loop in certain situations.

    >>> range(30,40) [30, 31, 32, 33, 34, 35, 36, 37, 38, 39] >>>
    You can omit the first argument to have Python generate a range from 0 to the specified end point.

    >>> range(40) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39] >>>
    You can skip certain numbers in the range by adding an optional "step" argument (by default, this is 1).

    >>> range (25,500,25) [25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475] >>> range (100,1,-10) [100, 90, 80, 70, 60, 50, 40, 30, 20, 10] >>>
    What does this have to do with anything? Well, the range() function, in combination with the "for" loop, can come in handy when you need to perform a series of actions a specified number of times,

    >>> for x in range(1,12): ... print "It is now", x, "o'clock" ... It is now 1 o'clock It is now 2 o'clock It is now 3 o'clock It is now 4 o'clock It is now 5 o'clock It is now 6 o'clock It is now 7 o'clock It is now 8 o'clock It is now 9 o'clock It is now 10 o'clock It is now 11 o'clock >>>
    or to generate indices (corresponding to list elements) in a "for" loop.

    >>> flavours = ["Strawberry", "Blueberry", "Blackcurrant", "Pineapple", "Mango", "Grape", "Orange", "Banana"] >>> for temp in range(2,5): print flavours[temp] ... Blackcurrant Pineapple Mango >>>
    Here's a more interesting example.

    #!/usr/bin/python # get a number num = input("Gimme a number: ") # for loop for count in range(2,num): # if factor exists, num cannot be prime! if num % count == 0: print num, "is not a prime number." break else: # if we get this far, num is prime! print num, "is a prime number."
    This is a simple piece of code to test whether or not a number is prime. A "for" loop is used, in conjunction with the range() function, to divide the user-specified number (num) by all numbers within the range 2 to (num). If no factors are found, it implies that the number is a prime number.

    Here's what it looks like:

    Gimme a number: 23 23 is a prime number. Gimme a number: 45 45 is not a prime number. Gimme a number: 11 11 is a prime number. Gimme a number: 111 111 is not a prime number.
    As with the "while" loop, Python allows you to add an "else" clause to a "for" loop; it is executed only if the loop is completed without encountering a "break" statement even once.

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