Advanced Python Tutorials – Iterators, Generators and Decorators

Advanced Python Tutorials – Iterators, Generators and Decorators

I am going to talk about Python Advance concepts which are very important to understand. In this post, we'll learn important Python concepts like Iterators, Generators, and Decorators in Python

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Hire Machine Learning Developers in India

We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.

Web Scraping using Python To Create a Dataset | Data Science | Machine Learning | Python

In this article I will show you how you can create your own dataset by Web Scraping using Python. Web Scraping means to extract a set of data from web. If you are a programmer, a Data Scientist, Engineer or anyone who works by manipulating the data, the skills of Web Scrapping will help you in your career. Suppose you are working on a project where no data is available, then how you are going to collect the data. In this situation Web Scraping skills will help you.

Web Scraping Using Python To Create A Dataset | Data Science | Machine Learning | Python

In this article I will show you how you can create your own dataset by Web Scraping using Python. Web Scraping means to extract a set of data from web

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.