Home arrow Python arrow Page 6 - Python 101 (part 3): A Twist In The Tail

Twist And Turn - Python

Find out more about adding flow control to your Python programswith the "for" and "while" loops, see how the range() function can be usedto generate number ranges, and learn all about list objects. And bring anold flame along for the ride.

TABLE OF CONTENTS:
  1. Python 101 (part 3): A Twist In The Tail
  2. Here Comes A Hero
  3. Making Friends And Influencing People
  4. We Don't Need Another Hero
  5. Looping The Loop
  6. Twist And Turn
  7. Within Range()
  8. Just Passin' Through
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
June 13, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
In most programming languages, a "for" loop is used to execute a set ofstatements a certain number of times. Unlike a "while" loop, whichcontinues to run for so long as the specified conditional expressionevaluates as true, a "for" loop comes with a specific limit on the numberof times it can iterate.

Python's "for" loop conforms to this basic requirement; however, as withmost things in Python, there's a twist in the tail. A Python "for" loop isdesigned only to iterate over built-in "sequence objects" like strings andlists, 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 itin 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 successiveiteration, one element of the list is assigned to the temporary variable"myhero" and then printed to the console via a print() call. Once all theelements of the list have been processed, the loop is automaticallyterminated.

You can use a "for" loop with any "sequence object" - this next exampledoes 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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: