Write a program that initializes an array with ten random integers and then prints four lines of output containing:
-every element at an even index
-every even element
-all elements in reverse order
-only the first and last element.
Name:
Anonymous2011-03-07 22:12
Sure.
import random
xs = [random.randint(1, 100) for i in xrange(10)]
print xs[::2]
print [x for x in xs if x % 2 == 0]
print xs[::-1]
print xs[0], xs[-1]
>>6
The semantics are slighly different. The first statement is executed once, printing a single array, while the second statement is executed n times, printing n scalar values.