One of the most commonly asked question in Python interviews is "Have you worked with generators? How and what was the need to use it?" This post will try to explore what are they how and why they are used? Basically, Generator in simple terms can be thought as the function which returns series of results instead of one single result. Syntactic difference between function and generator is, Generator uses "yield" keyword instead of "return" keyword. Generators give us an iterator(Something which we can loop on for ex. list or dictionary) so they can be used with looping constructs such as 'for' and 'while' loops. Example: # Define a generator. def generator(number): while True: number += 1 yield number # Use the generator. x = generator(5) # Now calling the decorator print("Calling generator:") x.next() print("Calling generator:") ...
Majorly used Data Structures in python are as follows: List Set Tuple Dictionary String Numbers(int, float, decimal) One of the major things in python discussed is whether the data structure is mutable or immutable. What do this terms mean? In simple terms, Immutable means value cannot be changed after it is assigned to a variable. Whereas, In mutable data type value can be changed after assignment. Hmm, What ??? This is still not clear these are merely dictionary definitions I am blabbering. Amazon.in Widgets
Comments
Post a Comment