Python on the Weekend cos GENERATORS
Python on the Weekend cos GENERATORS
I am sorry I just want to get this course done. I love it but I am not enjoying the stress of it - of it hanging over me. I still have this module and then 2-3 more modules and then two modules from the advanced course. But it is the intermediate ones that really need to be done. Ideally they need to be done by mid-January. But before would be better too. And if I can get the advanced ones done by before then too then that would be even better too - thank you.
Connecting Generators
"There is are some cases where it is useful to connect multiple generators into one." ... "This allows us to delegate the operations of one generator to a sub-generator." Wait, WHAT? "Connecting generators is similar to using the itertools chain() function to combine iterators into a single iterator." Oh okay yes this is so easy this is so easy I get it now omg.
"In order to connect generators, we use the yield from statement."
This all makes sense.
It went really well! Here is my code:
def science_students(x):
for i in range(1,x+1):
yield i
def non_science_students(x,y):
for i in range(x,y+1):
yield i
# Write your code below
def combined_students():
yield from science_students(5)
yield from non_science_students(10, 15)
yield from non_science_students(25, 30)
student_generator = combined_students()
for student in student_generator:
print(student)
Thank you. And now... generator pipelines.
Generator Pipelines
"Generator pipelines allow us to use multiple generators to perform a series of operations all within one expression." I looked over the example and this is so, so easy. It makes so so much sense thank you. I really love this - IT IS SO SO BEAUTIFUL - I really love this coding example - it is so so so beautiful thank you. This was an amazing lesson and it all made a lot of sense to me - thank you. Sorry I am too tired to type it out right now.
I just liked the code in the lesson so here we go please thanks:
def number_generator():
i = 0
while True:
yield i
i += 1
def even_number_generator(numbers):
for n in numbers:
if n % 2 == 0:
yield n
even_numbers = even_number_generator(number_generator())
for e in even_numbers:
print(e)
if e == 100:
break
And here is the code I wrote up myself please thanks for and during the lesson - thanks.
def course_generator():
yield ("Computer Science", 5)
yield ("Art", 10)
yield ("Business", 15)
def add_five_students(courses):
for course, num_students in courses:
yield(course, num_students + 5)
increased_courses = add_five_students(course_generator())
for course in increased_courses:
print(course)
QUIZ
And my score is: 100%, of course.
Admittedly I got the first question wrong on the first go but I refreshed the page... shhhhhhhhhhhhh.
Project
Only taking a quick look now... definitely not doing now, please... THANK YOU.
"Event Coordinator". Okay I am way too tired to do it so I'll stop. But it's good to have seen it. Thank you.
Comments
Post a Comment