24 Respostas
+ 20
Kartik Taneja Take a look at this code -
def generate (str):
for s in str:
yield s
for ch in generate("Hello"):
print(ch)
Output 》
H
e
l
l
o
Notice how no variables are used. And also at each iteration one value is returned by the generator. This makes the code much easier. Hope it helps now!
+ 21
Kartik Taneja
Generator is a function which "yields" one value at a time. It is helpful when we need to iterate over objects. It basically returns one value at a time. This is is really useful in many cases. Also it doesn't have to store the complete value before returning it. So it takes up almost no memory.
Refer to this site for some in depth explanation -
https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/generator
+ 16
Kartik Taneja though ♨ Soumik ✅ did a good explanation here are some basic examples based on the same resource
https://code.sololearn.com/c3z1MS63LxDX/?ref=app
+ 14
BroFar
Thanks sir !
It Really helped !
+ 13
♨ Soumik ✅
Thanks for your help !!🤗
+ 9
Rahul Verma
Thanks !
+ 8
Añøńymöüß
Thanks di 🤗
+ 7
is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
+ 7
Thanks 🌟🌟Abhi 💫The💫 Boss ❤️❤️🚩 !
+ 5
Norgile BONOU as ♨ Soumik ✅ mentioned and if you look at my code above you will see I did 2 different types of reverses
+ 4
Generators are a type of iterable, like lists or tuples.
Unlike lists, they don't allow indexing with arbitrary indices, but they can still be iterated through with for loops.
They can be created using functions and the yield statement.
+ 3
generatorss create outputs. i like to use them to make lists
def generator_squares (n):
for x in range(n):
yield x **2
print(list(generator_squares(4)))
——> [0,1,4,9]
+ 3
A generator-function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
+ 2
How to reverve order in the list?
+ 2
Norgile BONOU
Suppose a list -
myList = [4, 3, 7, 9, 10]
Now, to reverse it, use list slicing -
myList[::-1]
What this does is goes from the starting element of the list to the last element, but in reverse order.
+ 2
Generator its construction which allow return something and also Generator cant raise memory error. Memory error - error which mean that memory of computer is filled
+ 2
Python generator works by maintaining its own local state, so that if the function called subsequent times then it can resume again exactly where it left. we can say that a generator used to like, a powerful iterator.
+ 2
hello,
Generators are iterators, but you can only iterate over them once. It's because they do not store all the values in memory, they generate the values on the fly. A python generator looks like a function but the yield statement makes the difference. The yield statement turns a function into the generator.
When you call a generator function, it will return a generator object but does not start execution immediately. The function execution begins on the very first call to next() method. The execution of the code stops when a yield statement has been reached. Once the yield statement is encountered, the function is paused and control is transfer to the caller with the yield value. As soon as "next" is called again on the generator object, the generator function will resume execution right after the yield statement in the code, where the last call exited.
But remember that a function execution always begins from the start of the function body.
Let's understand the concept using an example.
def abc():
number = 1
print("abc")
yield number
number = 2
print("xyz")
yield number
number = 3
print("def")
yield number
obj = abc() # Return a generator object. Function execution is not started yet
next(obj) # Function execution started.
# Output: abc
# 1
next(obj)
# Output: xyz
# 2
next(obj)
# Output: def
# 3
next(obj)
# Give error. As nothing to yield. Raise StopIteration exception
I hope this will help you
+ 2
Generators are used to create iterators, but with a different approach. Generators are simple functions that return an iterable set of items, one at a time, in a special way. When an iteration over a set of items starts using the for a statement, the generator is run.
Check Web - https://www.tecocraft.co.uk/JUMP_LINK__&&__python__&&__JUMP_LINK-development/
+ 2
in my simple thinking a generator is a function that returns an iterator object on which we can iterate (one value at a time)