Name: [002] Ian Ozsvald
Member: 101 months
Authored: 181 videos
Description: I am the co-founder of ShowMeDo (see, author of `The Screencasting Handbook <>`_ and the founder of the professional screencast production company `ProCasts <>`_: .. image:: ...

Python Beginners - Loops and Iteration [ID:296]

a series of video-tutorials by Ian Ozsvald

Iterating over a block of code is a very common operation when programming. Here I show you the 'for' and 'while' commands in Python, they let you iterate over a sequence of items and repeat blocks of code under a truth condition.

I'll show you how to use the 'for' loop on a list, set and dictionary whilst using iteration, range and enumerate. For the 'while' loop I'll show you an example of getting user-input (with 'raw_input') to build up a list of answers.

At the end of the series you'll have a firm knowledge for using 'for' and 'while' with Python.

Video Tutorials

1. Overview (Loops and Iteration)

In this 2 minute introduction I'll show you two code examples of the 'for' and 'while' loop. As mentioned, in this series we use 'range', 'len', 'enumerate' on 'list', 'set', 'dict' along with 'raw_input'. After watching this series you'll have a firm knowledge of how to iterate with two powerful Python commands.

2. For on a List

The 'for' command lets us iterate over a sequence of items, commonly we use it to iterate over the items in a list. This is exactly the use-case I show you here. We'll also look at using 'break' and 'continue', along with the less-usual 'else' statement (normally you only see 'else' on 'if' statements). Note that there is an AUDIO TYPO in this episode - I mention 'comment out' when I should be s [...]

3. For using a range

Older languages let you use a for loop simply to create a list of index values, you'd then use these to index into a container. In Python we can replicate this behaviour with 'len' and 'range'. This builds up to the 'enumerate' example in the next episode. Both techniques are useful as sometimes we want to have both the index of the item and the item itself (without having to type too much!). S [...]

4. For using enumerate

The 'enumerate' function builds on the last episode - we can get the index of an item and the item itself in a simple statement. 'enumerate' was added in Python 2.3. Links: built-in functions Looping Techniques

5. For on a set

Sets store a unique collection of values (unlike a list which can store repeated references to the same value). Sets are different to lists in that you can't index into them - I show you what happens when you try. You can however use the 'for' loop to iterate over the items in a set. See also Set Types -- set, frozenset.

6. For on a dict(ionary)

Dictionaries let us store values which we address using a key. We can use a 'for' loop to extract both the keys, values and key+value pairs, I show you how in this episode. Links: Dictionaries dict mapping

7. While loops

The 'while' loop is different to the 'for' loop. Rather than iterating over a sequnce of items, instead we repeatedly run a block of code under the control of a logical test. The 'while' look can be controlled using the 'break', 'continue' and 'else' statements just like the 'for' loop. Here we use a simple example of the 'while' loop to build a list of user-input (obtained using 'raw_input'). [...]

Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Ian

Learning Paths

This series lies on the following learning-paths. Learning-paths are a new initiative at Showmedo, to start structuring our content better. You can find out more here.



Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.