Welcome to the Python: More to the Basics series Part — 1. In this series, I am going to talk about Python Advance concepts which are very important to understand. Without understanding these concepts, it’s very difficult to apply them in Real-world more importantly in the Data Science world.

Hence, let’s start with the first concept => Iterators, Generators, and Decorators in Python.

Iterators

Iterators are simply python objects which can be iterated upon and can be used to get element one by one from any collection. Iterators are everywhere in python for example for loop, generators, and comprehensions etc.

If you create a simple for loop in python then during execution that gets converted into iterators.

We can implement iterators using 2 special methods in python

  1. _ _iter _ _ or iter()
  2. _ _next _ _or next()

How to create an iterator

list = [1,2,3,4]
my_iter = iter(list)    ## THIS WILL CREATE my_iter AS AN ITERATOR OBJECT WHICH WE CAN ITERATE UPON

print(next(my_iter))
1

To get the next element from the iterator, use **next() **function again.

print(next(my_iter))
print(next(my_iter))
print(next(my_iter))

2
3
4

What happens if you execute next() again but there is no next element. You guessed it right, it will throw an exception.

next(my_iter)

Image for post

To handle the exception internally, python creates an iterator with a try-except block. Let’s understand how we can implement a for loop using iterators.

Implementation of for loop with Iterators

my_list = [1,2,3,4]
for element in my_list:
    pass
print("for loop completed")
for loop completed
#Implementaion using Iterators
my_list=[1,2,3,4]
iter_obj = iter(my_list)
while True:
    try:
        element = next(iter_obj)
        pass        
    except StopIteration:
        break

print("for loop implementation completed with iterators")
for loop implementation completed with iterators

In the above example, we saw how a for loop gets converted into iterators and how iterators work internally.

Generators

As we have seen in Iterators, there is a lot of overhead to create an Iterator — implement **iter() **and **next () **functions and then handle StopIternation exception. To overcome this problem, python has provided another powerful and useful solution i.e. Generators.

Generators are like any normal function in Python however there are 2 differences between a normal function and Generators.

  1. Generators use the yield keyword to return any value.
  2. There can be more than 1 yield keyword in a Generator, unlike normal Python Function.

Let’s understand the Generator using one example for reversing a String.

#Create a Generator
def my_generator(input_str):
    length_of_string = len(input_str)
    for i in range(length_of_string-1,-1,-1): ## This will take index as 5, 4, 3, 2, 1, 0 for the input string "Python"
        yield input_str[i]
print("Generator created")
Generator created
a = my_generator("PYTHON")
print(next(a))

print(next(a))
print(next(a))
print(next(a))
print(next(a))
print(next(a))
N
O
H
T
Y
P

As we have seen in this example, Generators also returns element one by one on demand.

Generators are useful when you need to keep track of index as well as the actual element from any collection.

Now Let’s see what is Generator Expression.

#python #data-science #web-development #machine-learning #developer

Advanced Python Tutorials – Iterators, Generators and Decorators
1.85 GEEK