Re: [Tutor] Re: 3 simple, pithy and short questions, releated to previous answers of yours ...

Magnus Lycka magnus at thinkware.se
Tue Nov 25 13:07:06 EST 2003


> a tuple is very like a list except you cannot change the contents
> once created.

E.g.

>>> l = [1,2,3]
>>> l[1]=5
>>> l
[1, 5, 3]
>>> t = (1,2,3)
>>> t[1]=5

Traceback (most recent call last):
  File "<pyshell#70>", line 1, in -toplevel-
    t[1]=5
TypeError: object doesn't support item assignment

> This allows us to use a tuple as the lookup key to a
> dictionary(see below)

A tuple is also somewhat faster and uses a little less memory.

But in practice, we typically use lists when we have a sequence
of similar objects, like a list of email addresses, or a list
of names of people. Tuples are often used for structures, for
instance if you want to keep a name, an email address and an
age for a person as a collection, you might do something like

someone = ('Doug Duckling', 'doug at example.com', 43)

As you might have guessed by now, it's fairly common with lists 
of tuples.

people = [
('Doug Duckling', 'doug at example.com', 43),
('Dan Duckling', 'da at example.com', 42),
('Fred Duckling', 'fred at example.com', 41)
]

With this construct, you can easily add people to the list,
or remove them from it, but a "people tuple" can't have itmes
appended to it etc. Of course, you can change Fred's age
like this...

# Fred's birthday
fred = people[2]
people[2] = (fred[0], fred[1], fred[2]+1)

..but then you have not motified the last tuple in the list. 
You have replaced it with a brand new tuple object with similar
content.

> >  I understand also that one creates an arrow of object in
> string

You mean array? The generic term used in Python is sequence.
Typically, arrays in computer languages imply a certain type
of implementation. The word array also has a mathematical meaning,
but only for numbers I guess... Sequence is considered a less 
technical, broader term that described the common qualities of
lists, tuples and strings etc.

A string is (like the tuple and list) also a sequence, but as
Alan said, it's restricted to containing characters (which are
strings with a length of one, which means that I just gave a
circular definition, sorry! ;)

Like the tuple, strings are immutable.

>>> s = "abc"
>>> s[1]
'b'
>>> s[1] = 'B'

Traceback (most recent call last):
  File "<pyshell#73>", line 1, in -toplevel-
    s[1] = 'B'
TypeError: object doesn't support item assignment

> So:
> [] => a list
> () => a tuple
> {} => a dictionary

To be picky, a tuple is really created by putting commas between
objects, like this:

>>> x = 1,2,3

Sometimes we need to use () to avoid ambiguities. For instance...

>>> t = 1,2,3*4
>>> t
(1, 2, 12)
>>> t = (1,2,3)*4
>>> t
(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)

or

>>> def f(x):
	print x

	
>>> f(1,2,3)

Traceback (most recent call last):
  File "<pyshell#85>", line 1, in -toplevel-
    f(1,2,3)
TypeError: f() takes exactly 1 argument (3 given)
>>> f((1,2,3))
(1, 2, 3)
>>> 

> To add to the confusion, we use [] to access the data for all
> three!
> 
> t = (1,2,3)
> L = [4,5,6]
> d = {7:8,9:0}
> 
> print t[0],L[0],d[7]  # prints 1 4 8

This works the same with strings. You can also use slices.

>>> l = range(65,80)
>>> t = tuple(l)
>>> s = "".join([chr(x) for x in l])
>>> print l
[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
>>> print t
(65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79)
>>> print s
ABCDEFGHIJKLMNO
>>> print l[3:10]
[68, 69, 70, 71, 72, 73, 74]
>>> print t[3:10]
(68, 69, 70, 71, 72, 73, 74)
>>> print s[3:10]

Or even...

DEFGHIJ
>>> print l[3:10:3]
[68, 71, 74]
>>> print t[3:10:3]
(68, 71, 74)
>>> print s[3:10:3]
DGJ
 
> That's right, the last argument in range() is the size of step
> between values.

Compare with the last slicing examples above. (I think this was
introduced in Python 2.3.)
 
> > 3.) I noticed, that examples below (after typing print a, and
> print b)
> > give identical results, so is it really so, that there is no
> difference,
> > creating string or tuple, if using brackets, or if not  ??
> > s = 1,2,3 ... and ... s = (1, 2, 3)
> > b = "ww", "aa" ... and ... b = ("ww", "aa")

As I wrote above, it's the commas that make the tuple.

You can find out the type of an object with the (suprise) type() function.

>>> type(l)
<type 'list'>
>>> type(t)
<type 'tuple'>
>>> type(s)
<type 'str'>
 

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list