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

Alan Gauld alan.gauld at freenet.co.uk
Fri Nov 21 01:52:00 EST 2003


Tadey,

> So to "allocate" my questions reasonably, I am sending question
> about in-built help to Danny Yoo, the other guy from Python
Tutor
> (you probably know him) ...

You are better to semd all questions to the tutor list.
That way anyone on the list can answer so if Danny or I are busy
or
unavailable you get an answer quicker. Also other beginners can
learn from the answers to your questions. Finally, by getting
answers
from more than one person you get different points of view which
sometimes helps you see the answer quicker.


> 1.) What is the difference (in general) between [] and ()
brackets.

In general [] indicates a list and () a tuple.

a tuple is very like a list except you cannot change the contents
once created.
This allows us to use a tuple as the lookup key to a
dictionary(see below)

> For example in for loops with range function in examples bellow
there
> are the same results.

range() always returns a list.
However you can do

for n in (1,2,3)

or

for n in [1,2,3]

and there is no difference because for lopps over a *collection*
(or sequence) and lists, tuples, strings (and since v2,2
dictionaries)
are all sequences in Python.

>  I understand also that one creates an arrow of object in
string
> (or something like that), other creates the list, but could you
explain
> shortlly the (practical) difference ?!?

Sorry, not sure wjat you mean there. Can you reword it? Or give
an example?

> P.S., And there are also {} brackets - what is their part

{} indicate a dictionary. That is a collection of pairs of data.
One ite in the pair is the lookup key, the other the return
value.
In a real dictionary the lookup wouldbe a word and the value the
meaning.
But in programming the lookup can be any *immutable*(or
unchangeable)
value and the "definition" data can be anything at all.

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

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

And of course we use () to hold the arguments to functions and
to group mathematical terms in an expression. (5+6) * 3 etc...

> So I made some more examples of that kind of code and something
> seemed clear - that number 4 means, that loop is "jumping-over
4" from 1 to 10.

That's right, the last argument in range() is the size of step
between values.

> 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")

There are some subtle differences but in the main tuples can hold
anything
whereas strings can only hold characters. Othewise they are very
similar.

This might be where you get a better answer from another tutor
member.
(I'm off to catch a plane in 15 minutes!)

Take care,

Alan G.




More information about the Tutor mailing list