Creating an Array: Difference between revisions
Content added Content deleted
(list methods are not related to creating lists) |
|||
Line 348: | Line 348: | ||
List are mutable arrays. You can put anything into a list, including other lists. |
List are mutable arrays. You can put anything into a list, including other lists. |
||
empty = [] |
empty = [] |
||
numbers = [1, 2, 3, 4, 5] |
numbers = [1, 2, 3, 4, 5] |
||
Line 355: | Line 356: | ||
evens = range(0,10,2) # Optional arguments to range() can specify start, stop, and step |
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 |
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: |