Python Forum
A program to define sine function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A program to define sine function
#4
mohanp06 Wrote:so whatever I didn't understand I have marked in bold.
from itertools import count, islice
This 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 is
a 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, 9
The 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.
Reply


Messages In This Thread
A program to define sine function - by mohanp06 - Aug-17-2020, 06:19 PM
RE: A program to define sine function - by mohanp06 - Aug-19-2020, 07:07 AM
RE: A program to define sine function - by Gribouillis - Aug-20-2020, 07:42 PM
RE: A program to define sine function - by mohanp06 - Aug-25-2020, 06:16 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020