Python
  Home arrow Python arrow Page 8 - 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 
Moblin 
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 - Just Passin' Through


    (Page 8 of 8 )

    Python's loops also come with a bunch of control statements, which can beused to modify their behaviour. I've listed the important ones below,together with examples:

    break:

    The "break" keyword is used to exit a loop when it encounters an unexpectedsituation. A good example of this is the dreaded "division by zero" error -when dividing one number by another one (which keeps decreasing), it isadvisable to check the divisor and use the "break" statement to exit theloop as soon as it becomes equal to zero.

    for x in range(10, -1, -1): # check for division by zero and exit if x == 0: break print 100 / x
    In this case, the "break" statement ensures that the loop is terminatedwhenever an attempt is made to divide by zero. If you'd like to see whathappens without the "break" statement, simply comment out the "if" test inthe code above.

    continue:

    The "continue" keyword is used to skip a particular iteration of the loopand move back to the top of the loop - it's demonstrated in the followingexample:

    for x in range(10): if x == 7: continue print x
    In this case, Python will print a string of numbers from 1 to 10 - however,when it hits 7, the "continue" statement will cause it to skip thatparticular iteration and go back to the top of the loop. So your string ofnumbers will not include 7 - try it and see for yourself.

    pass:

    The "pass" statement essentially means "do nothing". Since Python usesindentation rather than braces to distinguish blocks of code, it generatesa syntax error if it doesn't find an expected code block within a loop orconditional statement. The "pass" statement is used as a placeholder insuch situations.

    if var == "neo": call_neo_func() elif var == "trinity": # insert code later pass elif var == "agent": # insert code later pass
    In this case, the "pass" statement is used to avoid an error when theprogram is run (try omitting it and see what happens.)

    And that's about it for the moment. In this article, you found out a littlemore about adding flow control to your Python programs with the "for" and"while" loops, and you also learnt about the ancillary "break", "continue"and "pass" statements. You saw how the range() function can be used togenerate number ranges, which can then be used in combination with a "for"loop. And you now know a little more about Python's data structures, afterthat crash course in list objects.

    In the next article, we'll be continuing our tour of built-in Pythonobjects with a look at dictionaries and tuples, powerful and flexible datastructures which let you do weird and wonderful things with your code.We'll also re-visit numbers, strings and lists for a look at some more ofthe functions built into these objects. See you then!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

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