P8-Iterator
P8-Iterator
Miscellaneous
Generators, iterators and closures;
e.g. range()
range(5) generator is invoked six times, providing five
subsequent values from zero to four, and finally signaling
that the series is complete.
print(next(fruit)) # apple
print(next(fruit)) # banana
once
__next__() : return the next value, if there are no more
such a function should not be invoked explicitly. it isn't a function anymore; it's a generator object.
廖文宏 Python Programming 7
4.1.1.9 lambda function
A lambda function is a function without a name
(anonymous function).
syntax
lambda parameters : expression
returns the value of the expression
e.g.
sqr = lambda x : x*x
sqr(3)
e.g.
max = lambda a, b: a if a > b else b
maximum = max(10,20)
lambda function and map(), filter()
廖文宏 Python Programming 8
4.2.1 Processing Files
File Stream
Program does not communicate with the files directly,
mentioned above;
update mode: allows both writes and reads.
sys.stdout
standard output (normally associated with the screen)
sys.stderr
standard error output
.readlines()
read all the file contents, and returns a list of strings, one
to add it yourself
e.g. bytearray(10)
bytearray constructor fills the whole array with zeros
.replace() .weekday()