Python Iterables Iterators Homework
Python Iterables Iterators Homework
- An **Iterable** is any Python object you can loop over with a for loop (like lists, tuples, strings).
- An **Iterator** is an object that remembers the current position while looping and gives the next
---
```python
numbers = [1, 2, 3]
for n in numbers:
print(n)
```
---
```python
numbers = [10, 20, 30]
it = iter(numbers)
print(next(it)) # 10
print(next(it)) # 20
print(next(it)) # 30
```
---
```python
def countdown(n):
while n > 0:
yield n
n -= 1
cd = countdown(3)
print(next(cd)) # 3
print(next(cd)) # 2
print(next(cd)) # 1
```
---
### Homework Questions:
4. Write code to create an iterator from this list: `[5, 10, 15]` and print all its items using `next()`.
5. What is a generator? Write a small generator function that yields the first 4 even numbers.
7. What error is raised when there are no more items left in an iterator?
---