Return to Level1Techs.com

The Noobs of Python: Ep.3.1 - Lets Iterate with I t e r a t i o n s ! [Updated]

Q: This post has gone through several incarnations, Why the move to Iterations now?
A: Iterations felt like a better topic to start the Ep: 3.x series and considering that Iteration is one of the fundamental operations a computer is useful for, like data processing! When scanning datasets that don’t fit in memory, we need a way to get the items lazily, that is, one at a time and on demand. Traditionally, iteration is achieved by a for loop. The primary aim of the for statement is to traverse a list like:

>>> 
>>> for i in ['a', 'b', 'c', 'd', 'e']:
	print(i)

a
b
c
d
e
>>>

In this example, the loop variable i is successively assigned to one element of the list. Python 3 uses generators in many places. Even the range() built-in now returns a generator-like object instead of full-blown lists like before. If you must build a list from range , you have to be explicit :

>>> 
>>> list(range(100))
[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, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 
56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 
74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 
92, 93, 94, 95, 96, 97, 98, 99]
>>>

Every collection in Python is iterable, and iterators are used internally to support:
• for loops;
• collection types construction and extension;
• looping over text files line by line;
• list, dict and set comprehensions;
• tuple unpacking;
• unpacking actual parameters with * in function calls.

Q: Can you show me an example of iterations?
A: Let's get to it !

>>> for x in [1, 3, 7, 8]: print(x ** 4, end=' ')

1 81 2401 4096 
>>>  
>>> 
>>> for x in (1, 4, 72, 15): print(x ** 7, end=' ')

1 16384 10030613004288 170859375 
>>> 
>>> 
>>>
>>> for x in 'Level1Techs': print(x * 4, end=' ')

LLLL eeee vvvv eeee llll 1111 TTTT eeee cccc hhhh ssss 
>>>

The for loop turns out to be even more generic than this, it works on any iterable object. In fact, this is true of all iteration tools that scan objects from left to right in Python, including for loops, list comprehensions which we will cover in the next Ep: 3.2 - Lets Comprehend Comprehensions.

This Post is the intro piece to the Iterations Ep: , We will discuss File Iterators, Manual Iteration with iter() and next() methods, using yield from statement to combine generators itertools() and other built-in type Iterables in future post below.

Code_On_CodeWarriors

2 Likes

Ok, i think i got it.

>>> for i in ["a", "b", "c", "d"]: print(i.upper(), end=" is a string ")
...
A is a string B is a string C is a string D is a string >>>

I think i got. I need to go through file manipulation episode and i will be ready for the followup. :smiley:

1 Like

Here is my question. How do you print an error if you go through an iteration without any matches.

for i in ["a", "b", "c"]:

if i == "e":

print ("E")

I don't want to use an else statement because it will do the else statement for every loop. I ONLY want a message once the loop as completed.

for i in ["a", "b", "c"]:

if i == "e":

print ("E")

Is this part of a def() function?

No. But it can be I guess.

I'll touch on this topic in a later post on itertools()