[Tutor] Sets in python

2007-01-21 Thread anil maran
http://www.linuxforums.org/programming/introduction_to_python__part_1.html
Sets

I think I don't really have to explain what a set is, as everyone should know 
them from mathematics. It's simply a pile of elements that do not have ordering 
and do not contain duplicates. 

A set has to be initialized with the elements of a list. Since you already know 
what a list is, we do this in one step. Just like with dictionaries, print can 
handle a set as it is.
Once we have a set, I show he first useful feature of sets: testing whether an 
element is in the set. 

inventory_carpenter=set(['helmet', 'gloves', 'hammer'])
print inventory_carpenter # outputs set(['helmet', 'hammer', 'gloves'])

print 'gloves' in inventory_carpenter # outputs 'True' 

Since sets are interesting only if we have more that one of them, let's 
introduce another one! Once we have that, we can immediately see what are the 
elements that both sets contain (intersection).

inventory_highscaler=set(['helmet', 'rope', 'harness', 'carabiner']) 

print inventory_carpenter & inventory_highscaler # outputs 'set(['helmet'])'

Similarly, we can have the union of sets ( using | ), difference ( using - ), 
or symmetric difference (using ^). 
For sets, you don't really need anything else, as you can do every meaningful 
operation using the ones above. For example, to add a new element, you can use 
union.

inventory_carpenter = inventory_carpenter | set(['nails']) 

Using the interpreter interactively

If you would like to try out the things you've learnt right now, you might 
appreciate that the interpreter can be used in an interactive way. In case you 
use it like that, you don't have to enter your commands to a file, then save 
and run it, just tell something to Python, and get the response immediately. 
All you have to do is to invoke the interpreter by typing 'python' to 
your shell.

[EMAIL PROTECTED]:~$ python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17)
[GCC 4.1.2 20060715 (prerelease) (Debian 4.1.1-9)] on linux2\n
Type "help", "copyright", "credits" or "license" for more information.
>>>

Once you are here, you can just type anything you like, it will get interpreted 
immediately. The good new is that you don't even have to use print if you 
want to see the value of a variable, just type the name of it.\n

>>> a\u003d4
>>> a
4
>>>

\n\n",0] ); D(["ce"]);  //-->
All you have to do is to invoke the interpreter by typing 'python' to your 
shell.

 
-
Now that's room service! Choose from over 150,000 hotels 
in 45,000 destinations on Yahoo! Travel to find your fit.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sets in python

2007-01-21 Thread Luke Paireepinart
anil maran wrote:
> http://www.linuxforums.org/programming/introduction_to_python__part_1.html 
> 
> [snip article excerpt]
why did you copy someone else's text verbatim and create a new thread?
Assuming you were trying to help someone on the list, why not reply to 
their e-mail like this:

Hey user,
it seems like you could benefit from reading an article i found on the web:



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

2007-01-21 Thread János Juhász
Dear Karl,


I use getch() when I start my script from the windows desktop and 
I am interested about its output.

import msvcrt
raw_input('Are you distressed ?\n')
print ('It will be better, I am sure :)')
msvcrt.getch() # append as last line

> Subject: Re: [Tutor] 'elp!!!1Totally Clueless Newbie In Distress
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=us-ascii

> * Karl Wittgenstein <[EMAIL PROTECTED]> [2007-01-20 13:10]:
> > Ok,got the script working almost fine now...The only problem is that 
the
> > program window closes before we can get a glimpse of the answer...I 
use SPE
> > under WinXP, and have seen this problem in every script i try...This 
is the
> > script,as redone by a Smart Caring Dude on this list:


Yours sincerely,
__
János Juhász

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Set changing order of items?

2007-01-21 Thread Adam Cripps
On 1/20/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Adam Cripps wrote:
> > Many thanks all - I've got there in the end, using a tuple inside a
> > set, ditching the question number and then rendering the sum for
> > output in my main gui module.
> >
> > However, this does raise another issue now (which I had thought would
> > happen, but was putting it off until I'd solved the original problem).
> >
> > Basically, the user can choose how many problems are set - i. They can
> > also choose the highest and lowest possible numbers - highest and
> > lowest. With these values, they can now create a scenario which will
> > lock the application - they can choose values with a narrow margin
> > (lowest = 5, highest = 6) and then choose to have 100 of these
> > problems (i=100). Of course, this will mean that my app will now go
> > through that loop trying to find a new combination that no longer is
> > possible.
>
> This is a pretty easy condition to test for - maybe check that the
> number of possible problems in the given range is at least double (or
> some multiple) of the number of problems desired. If not, print an error
> message and have them enter new values.
> >
> > Anyone have any guidance or tips here? How will I create a flag which
> > is raised once the loop becomes infinite?
>
> You could also restrict the loop to, say, twice the number of problems
> desired and give up if after that many loops you don't have the desired
> number.

Kent - completed the check through the former method - by checking the
possibly number of permutations wasn't exceeded.

Many thanks.
-- 
http://www.monkeez.org
PGP key: 0x7111B833
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

2007-01-21 Thread Luke Paireepinart
János Juhász wrote:
> Dear Karl,
>
>
> I use getch() when I start my script from the windows desktop and 
> I am interested about its output.
>
> import msvcrt
> raw_input('Are you distressed ?\n')
> print ('It will be better, I am sure :)')
> msvcrt.getch() # append as last line
And where is the interesting output?  What were you expecting, and what 
did you get?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] search a tuple of tuples

2007-01-21 Thread johnf
Hi,
I want to find a item within a large tuple that contains tuples.
mytuple = (('name',123,'value'),('first',345,'newvalue'))
so a 'for loop' works
istrue = False
for i in range(len(mytuple)):
  if 'first' in mytuple[i]:
  istrue = True
  break
if istrue:
  print " true"

Is possible to use a generator or list comp.  All I want to know is it True or 
False that mytuple contains 'first'.
-- 
John Fabiani
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Arabic text in Python

2007-01-21 Thread Emad Nawfal

Dear Folks,
I'm new to Python and programming in general. I'm interested in using Python
for text processing, but whenever I enter Arabic text, it says the text is
not supported.
How can I use Python for dealing with Arabic?
Thank you

--
If everyone is thinking alike, then somebody isn't thinking." George S.
Patton
---
Emad Soliman Nawfal
University of Arizona, Tucson

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

2007-01-21 Thread jim stockford

no need for apology on my side. there's
no agreed-upon and expressed policy.
i like the model that we accept each
other as we are.

On Jan 20, 2007, at 7:10 AM, Karl Wittgenstein wrote:

> Sorry for the swear words...
>
> 2007/1/20, Kent Johnson <[EMAIL PROTECTED]>: Karl Wittgenstein wrote:
>> > "input('Something') displays the prompt 'Something' and then waits 
>> for
>> > input up to a new line. When you enter the input it will execute the
>> > next statement which may be another input()."
>> > It should be so,man,I believe you.But believe me when I say that 
>> THIS
>> > DAMN INTERPRETER DOES NOT ACT ACCORDINGLY!!!Sorry for the emotive
>> > caps,it's just frustration biting my ancles...Maybe it's a SPE 
>> problem??
>> > Thank you.
>>
>> Can you describe exactly what you are doing? And please stop with the
>> swear words, they are not appropriate to this list.
>>
>> Kent
>>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] search a tuple of tuples

2007-01-21 Thread Kent Johnson
johnf wrote:
> Hi,
> I want to find a item within a large tuple that contains tuples.
> mytuple = (('name',123,'value'),('first',345,'newvalue'))
> so a 'for loop' works
> istrue = False
> for i in range(len(mytuple)):
>   if 'first' in mytuple[i]:

This is better written as iteration over mytuple directly, there is no 
need for the index:
for t in mytuple:
   if 'first' in t:
>   istrue = True
>   break
> if istrue:
>   print " true"
> 
> Is possible to use a generator or list comp.  All I want to know is it True 
> or 
> False that mytuple contains 'first'.

Use any() in Python 2.5:

In [1]: mytuple = (('name',123,'value'),('first',345,'newvalue'))

In [2]: help(any)
Help on built-in function any in module __builtin__:

any(...)
 any(iterable) -> bool

 Return True if bool(x) is True for any x in the iterable.

In [5]: any('first' in x for x in mytuple)
Out[5]: True

In [6]: mytuple = (('name',123,'value'),('last',345,'newvalue'))

In [7]: any('first' in x for x in mytuple)
Out[7]: False

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Arabic text in Python

2007-01-21 Thread Kent Johnson
Emad Nawfal wrote:
> Dear Folks,
> I'm new to Python and programming in general. I'm interested in using 
> Python for text processing, but whenever I enter Arabic text, it says 
> the text is not supported.
> How can I use Python for dealing with Arabic?

What platform? GUI or command line input?

wxPython seems to support Arabic, according to this page:
http://wiki.wxpython.org/index.cgi/ValidI18nCodes

For command-line use, my guess is that if you have a console that 
supports Arabic input then you should be able to write a Python app that 
supports Arabic input in that console. The input will be encoded 
according to the console character encoding.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

2007-01-21 Thread Danny Yoo


On Sun, 21 Jan 2007, jim stockford wrote:

>no need for apology on my side. there's
>no agreed-upon and expressed policy.
>i like the model that we accept each
> other as we are.

[meta; not really related to Python programming]

Hi Jim,

I did want to bring up that there are some community-held expectations on 
what to write on the list.  They're certainly not set in stone, but it's 
helpful to know about them.  Here's one description of an otherwise 
unspoken expectation:

http://www.catb.org/~esr/faqs/smart-questions.html#writewell


Generally, when we see someone being too frustrated by a problem to 
describe it properly to the rest of us, that's something that should be 
brought up to the poster, so that they can improve their writing on the 
Tutor list.

When we do so, we're not trying to coerse the poster to change who they 
are personally, because that would be domination.  But we are trying to 
say that if they are primarily focused on describing their frustration 
rather than the problem itself, they stand a good chance at succeeding. 
That is, they can risk frustrating the people who they are asking for 
help, and that's not good.


When Kent complained about the swearing, that was taken a little 
superficially.  Swearing isn't really the Big Problem.  He was really 
asking: "Can you start describing the problem more precisely so we can get 
to work helping you?"

We just want to make sure the goal structure of questions on Tutor doesn't 
lean toward: "I don't know what I'm doing, and the buggy system sucks, and 
I'm so angry that I want people to sympathize with my anger."

Rather: "I don't know what I'm doing, here's what I've done so far, and I 
hope I wrote enough information for people to help me."


Best of wishes!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] search a tuple of tuples

2007-01-21 Thread johnf
On Sunday 21 January 2007 10:30, you wrote:
> johnf wrote:
> > Hi,
> > I want to find a item within a large tuple that contains tuples.
> > mytuple = (('name',123,'value'),('first',345,'newvalue'))
> > so a 'for loop' works
> > istrue = False
> > for i in range(len(mytuple)):
> >   if 'first' in mytuple[i]:
>
> This is better written as iteration over mytuple directly, there is no
> need for the index:
> for t in mytuple:
>
>if 'first' in t:
> >   istrue = True
> >   break
> > if istrue:
> >   print " true"
> >
> > Is possible to use a generator or list comp.  All I want to know is it
> > True or False that mytuple contains 'first'.
>
> Use any() in Python 2.5:
>
> In [1]: mytuple = (('name',123,'value'),('first',345,'newvalue'))
>
> In [2]: help(any)
> Help on built-in function any in module __builtin__:
>
> any(...)
>  any(iterable) -> bool
>
>  Return True if bool(x) is True for any x in the iterable.
>
> In [5]: any('first' in x for x in mytuple)
> Out[5]: True
>
> In [6]: mytuple = (('name',123,'value'),('last',345,'newvalue'))
>
> In [7]: any('first' in x for x in mytuple)
> Out[7]: False
>
> Kent
Perfect thanks for the help!
-- 
John Fabiani
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] pyw bug ?

2007-01-21 Thread Dave S
Hi all,

My GUI app dumps some diagnostic output to the terminal, starting it from an 
XP terminal, all is well.

Starting it as app.pyw so no terminal is needed - and err - the app randomly 
locks because the diagnostic output is - backing up ?

Remove diagnostic output & app.pyw is 100% reliable.

... mmm ... windows  mutter mutter

Dave


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] direction and draw

2007-01-21 Thread linda.s
I have a segment (two ending points are A and B) which is 0 degree.
How to draw a segment with 10 degrees and 15 in length?
Thanks,
Linda
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor