chzzz's Blog

The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.--Stephen William Hawking



# 1. Implement a generator called “squares” to yield the square of all numbers from (a)
# to (b). Test it with a “for” loop and print each of the yielded values
def squares(a,b):
    while a<=b:
        yield a*a

for i in squares(1,5):
#2. Create a generator to yield all the even numbers from 1 to (n)
def even(n):
    while a<n:
        yield a+1 
for i in even(6):
#3. Create another generator to yield all the odd numbers from 1 to (n)
def odd(n):
    while a<=n:
        yield a

for i in odd(10):
#4. Implement a generator that returns all numbers from (n) down to 0.
def odd(n):
    while n>=0:
        yield n
for i in odd(10):
#5. Create a generator to return the fibonnaci sequence starting from the first element
# up to (n). The first numbers of the sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
# 89, . . .
def fibonnaci(n):
    while i<n:
        yield value
for i in fibonnaci(12):
# 6. Implement a generator that returns all consecutive pairs of numbers from 0 to (n),
# such as (0, 1), (1, 2), (2, 3). . .
def consecutive_pairs(n):
    while a<n:
        yield (a,a+1)

for i in consecutive_pairs(10):



© 版权所有:沈阳市和平区赛博空间咨询部|辽ICP备18005891号
Powered by Z-BlogPHP &