SunQuest
 
       Python
  Home arrow Python arrow Page 7 - Python 101 (part 3): A Twist In The Ta...
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

Python 101 (part 3): A Twist In The Tail
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      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

    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 returna range of numbers, given a starting and ending point. This range is alwaysreturned as a list - and as you'll see, this can combine quite effectivelywith 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 tothe 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, incombination with the "for" loop, can come in handy when you need to performa 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 theuser-specified number (num) by all numbers within the range 2 to (num). Ifno 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 withoutencountering a "break" statement even once.

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


     

       

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