Creating an Array: Difference between revisions

Content added Content deleted
No edit summary
Line 352: Line 352:
zeros = [0] * 10
zeros = [0] * 10
anything = [1, 'foo', 2.57, None, zeros]
anything = [1, 'foo', 2.57, None, zeros]
digits = range(10) # Built-in range(X) function generates lists up to but not including X
evens = range(0,10,2) # Optional arguments to range() can specify start, stop, and step
squares = [ x * x for x in range(100) ] # List comprehension expressions can create lists
digits.insert(0, digits.pop()) # List objects support various methods,
# .pop() removes and returns last item, .insert(index, item) does what you'd expect


Tuples are immutable arrays. Note hat tuples are defined by the "," - the parenthesis are optional:
Tuples are immutable arrays. Note hat tuples are defined by the "," - the parenthesis are optional: