Member-only story
Top 12 most important Python concepts
In this article I am going to explain several important python concepts and present their usage. They are very useful in the daily life of a python engineer and are very likely to be found in almost any python interviewer repertoire of question in one form or another.
Let’s begin:
Generators
The basic idea of a generator is that it allows you to create a function that has the same behavior as an iterator but without the boiler plate that comes with it.
If for an iterator we have to create a whole new class that implements the __next__ and __iter__ methods with the whole state management, a generator function can be declared almost like a normal function that instead of just using “return” it uses “yield”. The value you yield is the value we get from the next step of execution. An important aspect is that the call to the generator function returns an iterator, which must be then iterated over with the “next” function or using a for loop. The main advantage of a generator function is that it’s memory efficient, we don’t have to keep the values in an array to use them, we can get them in a “lazy” manner from the generator.
An example of a generator function that multiplies the given number by 2:
def multiply(n):
state = n
while True…