What is an Iteration
Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration.
Example 1
Explanation
- Creates a list called 'num' containing integers 1, 2, and 3
- Uses a for loop to iterate through each element in the list
- Calls the print() function to display each individual number on a separate line
- The loop processes one item at a time, printing 1, then 2, then 3
- Expected output shows each number vertically printed to console
1# Example2 3num = [1,2,3]4 5for i in num:6 print(i)Output
112233What is Iterator
An Iterator is an object that allows the programmer to traverse through a sequence of data without having to store the entire data in the memory
Example 2
Explanation
- Creates a list comprehension that generates integers from 1 to 9 and stores them in variable L
- Iterates through each element in L and prints twice its value (2, 4, 6, 8, 10, 12, 14, 16, 18)
- Uses sys.getsizeof() to measure memory usage of the list L in units of 64-byte blocks
- Creates a range object x containing integers from 1 to 99999 without storing all values in memory
- Prints memory usage of the range object x, which will be much smaller than the list due to lazy evaluation

