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 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
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))
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.
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.
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.
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.
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.
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.
We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.
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.
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
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.