...

/

Generator expressions

Generator expressions

We'll cover the following...

A generator expression is like a generator function without the function.

Press + to interact
unique_characters = {'E', 'D', 'M', 'O', 'N', 'S', 'R', 'Y'}
gen = (ord(c) for c in unique_characters) #①
print (gen ) #②
#<generator object <genexpr> at 0x7ff0c8cf3a68>
print (next(gen) ) #③
#69
print (next(gen))
#83
print (tuple(ord(c) for c in unique_characters)) #④
#(69, 83, 89, 77, 68, 79, 78, 82)

① A generator expression is like an anonymous function that yields values. The expression itself looks like a ...