Anonymous user
Creating an Array: Difference between revisions
→{{header|Python}}
(m) |
|||
Line 567:
List are mutable arrays. You can put anything into a list, including other lists.
<python>
empty = []
numbers = [1, 2, 3, 4, 5]
Line 575 ⟶ 576:
evens = [x for x in range(10) if not x % 2] # same using list comprehension
words = 'perl style'.split()
</python>
Tuples are immutable arrays. Note
<python>
empty = ()
numbers = (1, 2, 3, 4, 5)
numbers2 = 1,2,3,4,5 # same as previous
zeros = (0,) * 10
anything = (1, 'foo', 2.57, None, zeros)
</python>
Both lists and tuples can be created from other iterateables:
<
>>> list('abc')
['a', 'b', 'c']
Line 595 ⟶ 600:
>>> list(open('file'))
['1\n', '2\n', '3\n']
</
Note: In Python 2.6 the ''collections.namedtuple'' factory was added to the standard libraries. This can be used to create classes of lightweight objects (c.f. the Flyweight design pattern) which are tuples that can additionally support named fields.
=={{header|Raven}}==
|