top of page
Search

Generator Function - Yield Python

Writer's picture: Abhinaw TripathiAbhinaw Tripathi

So Yield is a keyword.first let's take an example. student = [] def read_file(): try: f=open("students.txt","r") for student in read_students(f): students.append(student) f.close() except Exception: print("Could not read file") def read_students(f): for line in f: yield line read_file() print(students) So what I have done is created a single function read_students and what it does it iterated over the file and yield the single line from that file. 


7 views0 comments

Recent Posts

See All

Lambda Functions in Python

So Lambda Functions are just an anonymous function.they are very simple just saves your space and time.they are useful in case of higher ...

Opening,Reading and Writing Python

Let's save our data in a file and read from that file again when we re-open our app. So let's take an example: Let's take an example: ...

Nested Functions and Closures

Let's take an example: def get_students(): students=["Abhinaw","Aman"] def get_students_titlecase(): students_titlecase = [] for...

© 2016 by Abhinav Tripathi

  • Facebook Clean Grey
  • Twitter Clean Grey
bottom of page