Aug-20-2020, 07:42 PM
(This post was last modified: Aug-20-2020, 07:43 PM by Gribouillis.)
mohanp06 Wrote:so whatever I didn't understand I have marked in bold.
from itertools import count, isliceThis imports two functions from the itertools module of the standard library. This module contains various functions to help handle "iterables", which are the python name for general sequences. These sequences can have finitely many terms or not. For example
count(3, 2)
is the infinite sequence 3, 5, 7, 9, ...
.for n in count(3, 2): yield t ...This loops runs every integer n in the iterable
count(3, 2)
described above, and every time it generates a value by using the yield
keyword. This keyword is used to define "generator functions" that create finite or infinite sequences. Every function that contains the yield keyword is such a generator instead of a normal function. For example here isa function that generates the square of all numbers up to a limit
def squares(m): for n in range(m): yield n ** 2 for s in squares(4): print(s) # prints 0, 1, 4, 9The islice functions takes at most n terms of an iterable (a sequence)
islice(sine_terms(x), n) # <-- the n first terms of the iterable sine_terms(x)Finally the colloquial
if __name__ == '__main__': ...When python code is executed, the variable
__name__
contains the name of the current module. When this name is "__main__"
, it means that the current module is the main program. This test is used to declare code that we only want to run when this file is used as our main program and not as a library module.