+ 7

What is yield and generators in Python?

I read some articles about generation in Python and do lessons about it here. But I still don't understand. Can you explain as u understand this

3rd May 2019, 1:42 PM
Ann
Ann - avatar
10 odpowiedzi
+ 11
You may look this 1 also. If something is unclear then you can ask us https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2462/?ref=app
3rd May 2019, 1:47 PM
Seniru
Seniru - avatar
+ 11
here is a commented code showing an example where generators and yield are used: https://code.sololearn.com/cOGvT24D3Of9/#py
4th May 2019, 9:22 AM
Cépagrave
Cépagrave - avatar
+ 5
Ana generators can used for anything you like. Generators make it easier to code methods that create, modify and return list like structures. Also those yielded values are iterable and you can use those values in for loops.
3rd May 2019, 2:02 PM
Seniru
Seniru - avatar
+ 5
for i in range(1,10,3) can be seen as easy generator. even not in python sense. sometimes the rules to get the numbers are complicated and separared in a generators (fibonaccis maybe) also a loop is not always wanted. generators can help to avoid them.
4th May 2019, 7:25 AM
Oma Falk
Oma Falk - avatar
+ 4
Sεηιrυ ραsαη, okey, thank u)
3rd May 2019, 2:04 PM
Ann
Ann - avatar
+ 2
Hey Ann, yield statements are useful when you want to conserve memory. If you use a print statement or a for or while loop, there will be no pause (unless you ask for input after each result is displayed) between each result. Alternately you could place all results in some sort of data structure but this can slow down the computer as it uses more RAM. The yield statement will only display one result at a time.
4th May 2019, 5:45 AM
Tye Burrone
Tye Burrone - avatar
+ 1
How often generators will be used?
3rd May 2019, 1:56 PM
Ann
Ann - avatar
+ 1
They look highly useful. It is very common for computers to create lists and then to want to count (iterate) through them. Maybe you have a class full of students, or maybe a fleet of trucks, or maybe 300 orders from a web site, or a full day of temperature readings from a sensor that took a reading every minute.
3rd May 2019, 6:57 PM
James McLain
James McLain - avatar
0
who wants to solve problems
4th May 2019, 5:55 AM
Onesha Sappleton
Onesha Sappleton - avatar
0
تب
4th May 2019, 10:42 AM
Kadar Kadiro
Kadar Kadiro - avatar