filmov
tv
Python Programming: Lesson 27 - More Techniques for Generators

Показать описание
PLEASE SUBSCRIBE!!!
a , b = b , a switches the values of a and b in Python, REGARDLESS of data type. Not a thing in Java!
A generator is a function that YIELDS values. This means that it progressively returns values that can be shown or used in the context of a for loop. However, it itself is not an iterable. It's not a list or tuple with index positions. If you try printing a generator, it just shows generatorobject. If you really want a concrete data structure out of a generator, convert it to a list or tuple.
def generateMe( x , y ):
for y in range(y):
yield x+2
myCounter = 0
for t in generateMe(100,5):
if myCounter == 0:
print( t )
myCounter = myCounter + 1
print( myCounter ) # 102, then 5 on another line
2:24 Switching the Values of Two Variables
6:04 NEW: Generators that Accept no Inputs
10:38 Another Example with Generators
17:21 Final Example with Generators
Thanks for watching, and PLEASE SUBSCRIBE!!!
a , b = b , a switches the values of a and b in Python, REGARDLESS of data type. Not a thing in Java!
A generator is a function that YIELDS values. This means that it progressively returns values that can be shown or used in the context of a for loop. However, it itself is not an iterable. It's not a list or tuple with index positions. If you try printing a generator, it just shows generatorobject. If you really want a concrete data structure out of a generator, convert it to a list or tuple.
def generateMe( x , y ):
for y in range(y):
yield x+2
myCounter = 0
for t in generateMe(100,5):
if myCounter == 0:
print( t )
myCounter = myCounter + 1
print( myCounter ) # 102, then 5 on another line
2:24 Switching the Values of Two Variables
6:04 NEW: Generators that Accept no Inputs
10:38 Another Example with Generators
17:21 Final Example with Generators
Thanks for watching, and PLEASE SUBSCRIBE!!!