Arrays

G

Gordon C

Absolute newbie here. In spite of the Python Software Foundation tutorial's
( http://www.python.org/doc/current/tut/tut.html ) use of the array
declaration
array(type[,initializer]), the Python interpreter does NOT accept the word
array! It , presumably, needs to have an import <something> included. Could
some show me how to declare arrays with some basic examples?
Gord.
 
B

Bernard

J

John Machin

Bernard said:
Absolute newbie here. In spite of the Python Software Foundation tutorial's
(http://www.python.org/doc/current/tut/tut.html) use of the array
declaration
array(type[,initializer]), the Python interpreter does NOT accept the word
array! It , presumably, needs to have an import <something> included. Could
some show me how to declare arrays with some basic examples?
Gord.

hey Gordon,

here's a good reading for you: http://effbot.org/zone/python-list.htm

Hey Bernard, read Gordon's message carefully; he's asking about
arrays, not lists.

Hey Gordon, You seem a little lost; here's the tutorial reference:
http://docs.python.org/tut/node13.html#SECTION0013700000000000000000
which produces:
"""
The array module provides an array() object that is like a list that
stores only homogenous data and stores it more compactly. The
following example shows an array of numbers stored as two byte
unsigned binary numbers (typecode "H") rather than the usual 16 bytes
per entry for regular lists of python int objects:

>>> from array import array
>>> a = array('H', [4000, 10, 700, 22222])
>>> sum(a) 26932
>>> a[1:3]
array('H', [10, 700])
"""

The 2nd word (array) is a link (http://docs.python.org/lib/module-
array.html) to the docs for the array module.

Cheers,
John
 
J

John Machin

Chances are a list is exactly what the OP wants.

Chances are a list is what he *needs*. However he explicitly referred
to the array module array, not some generic array concept. Serial
processing of the tutorial should have clued him in on Python lists
way before he arrived at the mention of the array module.
 
G

Gordon C

OK, thanks to all. The key statement is "from array import array" which is
not exactly intuitive!
Gord

John Machin said:
Absolute newbie here. In spite of the Python Software Foundation
tutorial's
(http://www.python.org/doc/current/tut/tut.html) use of the array
declaration
array(type[,initializer]), the Python interpreter does NOT accept the
word
array! It , presumably, needs to have an import <something> included.
Could
some show me how to declare arrays with some basic examples?
Gord.

hey Gordon,

here's a good reading for you: http://effbot.org/zone/python-list.htm

Hey Bernard, read Gordon's message carefully; he's asking about
arrays, not lists.

Hey Gordon, You seem a little lost; here's the tutorial reference:
http://docs.python.org/tut/node13.html#SECTION0013700000000000000000
which produces:
"""
The array module provides an array() object that is like a list that
stores only homogenous data and stores it more compactly. The
following example shows an array of numbers stored as two byte
unsigned binary numbers (typecode "H") rather than the usual 16 bytes
per entry for regular lists of python int objects:

from array import array
a = array('H', [4000, 10, 700, 22222])
sum(a) 26932
a[1:3]
array('H', [10, 700])
"""

The 2nd word (array) is a link (http://docs.python.org/lib/module-
array.html) to the docs for the array module.

Cheers,
John
 
C

Chris Mellon

OK, thanks to all. The key statement is "from array import array" which is
not exactly intuitive!
Gord

It becomes intuitive when you learn Python, which is what you're
reading the tutorial for, and it's why the tutorial shows you exactly
what to type. If you're progressing through the tutorial step by step
instead of jumping ahead, you should already have been introduced to
the import statement.
 
S

Steven D'Aprano

OK, thanks to all. The key statement is "from array import array" which
is not exactly intuitive!


"The only intuitive interface is the nipple. After that, it's all
learned." -- Bruce Ediger on user interfaces.

Once you've been using Python for a while, using import becomes as
intuitive as a spoon. The only tricky part is knowing *which* module to
import. But, honestly, are you surprised to learn that the array type is
held in in the array module?
 
J

Jorge Godoy

Steven said:
"The only intuitive interface is the nipple. After that, it's all
learned." -- Bruce Ediger on user interfaces.

And after we learn its other "uses", not even the nipple is so easy... Who
haven't heard (or said, if you're a woman) "Don't bite it like that, it
hurts!"? :)

Anyway, back to nippling, I mean, programming... :)
 
G

Gordon C

OK Steve, But why do we say "from array import array" and NOT "from math
import math"? Why the difference in syntax?
Gord
 
D

davisn90210

Modules contain objects. When you want to import a specific set of
objects contained in a module into the local namespace, you use:
from <module> import <names to import>
For example:
from math import sqrt
from math import sin, cos

If you want to import everything from a module, use:
from <module> import *
For example:
from math import *

If you want to import a module as an entity itself, use:
import <module>
For example:
import math #Use, for example, math.sqrt to call the sqrt function

You can also use import ... as ... to "rename" an imported object:
import math as m #Now use m.sqrt instead of math.sqrt
from math import sqrt as square_root #sqrt is now bound to
square_root in the local module

In the array module, there is an object named array. You could access
using:
import array
array.array
or
from array import array
array
or
from array import array as foobar
foobar
or ...
The way you choose to actually do this is up to you. Most people just
decide what they like/makes the most sense.

In the math module, there is no object called math. That is why we do
not use Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name math

--Nathan Davis

OK Steve, But why do we say "from array import array" and NOT "from math
import math"? Why the difference in syntax?
Gord


"The only intuitive interface is the nipple. After that, it's all
learned." -- Bruce Ediger on user interfaces.
Once you've been using Python for a while, using import becomes as
intuitive as a spoon. The only tricky part is knowing *which* module to
import. But, honestly, are you surprised to learn that the array type is
held in in the array module?
 
C

cokofreedom

Modules contain objects. When you want to import a specific set of
objects contained in a module into the local namespace, you use:
from <module> import <names to import>
For example:
from math import sqrt
from math import sin, cos

If you want to import everything from a module, use:
from <module> import *
For example:
from math import *

If you want to import a module as an entity itself, use:
import <module>
For example:
import math #Use, for example, math.sqrt to call the sqrt function

You can also use import ... as ... to "rename" an imported object:
import math as m #Now use m.sqrt instead of math.sqrt
from math import sqrt as square_root #sqrt is now bound to
square_root in the local module

In the array module, there is an object named array. You could access
using:
import array
array.array
or
from array import array
array
or
from array import array as foobar
foobar
or ...
The way you choose to actually do this is up to you. Most people just
decide what they like/makes the most sense.

In the math module, there is no object called math. That is why we do
not use
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name math

--Nathan Davis

Let me just say, that is a perfect reply!
 
S

Steven D'Aprano

OK Steve, But why do we say "from array import array" and NOT "from
math import math"? Why the difference in syntax?

It isn't different syntax. The difference is that there is a function
"array" (technically, a type rather than a function) in the module
"array", but there is no function "math" in the array "math". There is
however a function sin, so you can do this:

from math import sin


Precisely the same syntax: "from <module-name> import <object-name>".
Only the names are different.
 
S

Steven D'Aprano

And after we learn its other "uses", not even the nipple is so easy...
Who haven't heard (or said, if you're a woman) "Don't bite it like that,
it hurts!"? :)

Or even "Please Sir, bite it like that! Thank you Sir!"


*ducks and hides*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top