Anonymous user
Apply a callback to an array: Difference between revisions
→{{header|Python}}: print squares of integers in the range from 0 to 9
(→{{header|Python}}: fix formatting) |
(→{{header|Python}}: print squares of integers in the range from 0 to 9) |
||
Line 545:
numbers = [1, 3, 5, 7]
squares1 = [square(n) for n in numbers] # list comprehension
squares2a = map(square, numbers) # functional form
squares2b = map(lambda x: x*x, numbers) # functional form with `lambda`
squares3 = [n * n for n in numbers] # no need for a function,
# anonymous or otherwise
import itertools
isquares2 = itertools.imap(square, numbers) # iterator, lazy
</pre>
To print squares of integers in the range from 0 to 9, type:
print " ".join(str(n * n) for n in range(10))
Or:
print " ".join(map(str, map(square, range(10))))
Result:
0 1 4 9 16 25 36 49 64 81
=={{header|Raven}}==
|