tags:

views:

999

answers:

6

I'm really new to python and I'm wondering how to declare an array in this language? I can't find any reference to arrays in the docs.

+7  A: 
variable = []

Now variable refers to an empty list (array).

Of course this is an assignment, not a declaration. There's no way to say in python "this variable should never refer to anything other than an array", since python is dynamically typed.

sepp2k
+1  A: 

my_array = [1, 'rebecca', 'allard', 15]

as a sample.

canadiancreed
+4  A: 

You don't declare anything in Python. You just use it. I recommend you start out with something like http://diveintopython.org.

bayer
+2  A: 

I would normally just do a = [1,2,3] which is actually a list but for arrays look at this formal definition

non sequitor
+3  A: 

You don't actually declare things, but this is how you create an array in Python:

from array import array
intarray = array('i')

For more info see the array module: http://docs.python.org/library/array.html

Now possible you don't want an array, but a list, but others have answered that already. :)

Lennart Regebro
This is sort of funny, but not really a good answer for a question tagged "beginner". Just to make it clear: In Python you usually use a data type called a `list`. Python has a special-purpose data type called an `array` which is more like a C array and is little used.
steveha
No, but everyone else already used a list. I thought it would be a good answer to point out that there are arrays too.
Lennart Regebro
+1  A: 

Following on from Lennart, there's also numpy which implements homogeneous multi-dimensional arrays.

camh