Re: [Tutor] IF statements

2008-10-06 Thread Alan Gauld

WM [EMAIL PROTECTED] wrote

to IF.  The code below was not written by me.  It is a copy/paste 
job from the tutor.  I do not have any idea what is going wrong.



 x = int(raw_input(Please enter an integer: ))
Please enter an integer: 42
 if x  0:
...  x = 0
...  print 'Negative changed to zero'
... elif x == 0:
...  print 'Zero'
... elif x == 1:
...  print 'Single'
... else:
...  print 'More'
...
More
12
SyntaxError: invalid syntax


It looks as if you maybe cut more than you intended?
Also I'm not sure about the ... prompts. I don't have 2.6 yet
so maybe its an enhancement to IDLE but notmally IDLE
doesn't print ... prompts. Did you cut n paste them too?
If so that would confuse IDLE.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


Re: [Tutor] IF Statements

2008-10-06 Thread Rajeev Nair
 the script ending with .py/.pyw
 extension (command-line script/GUI script), although python doesn't
 complain if it is not in those extension (in Windows, the extension is
 associated with the interpreter). Calling a program from command line is
 done like this:

 python filename.py

That should get me going ... a book and manual by my side should
  suffice for
 the rest - - - except for one thing:

  2.  I have been unable to locate the gizmo in the literature to get
  ascii codes
  in python.  In the old days, it was a list of 256 (or so)
  characters that
  represented all keyboard symbols (A equalled 36; B equalled 37; et
  cetera).
 To assign a value, you used Let A$ = ASC (36) where A$ was a
  variable
  and 36 was the ASCII value for 'A'.  I believe the reverse of this
  process
  was PRINT VAL(A$) or something.  I want to play with a program
  that will
 assign a number to a word (using a simple algorhythm that will give
  a
  specific number to every word).  Other stuff is pretty easy to
  find with
  the book and on-line literature.  I will need to get an ascii code
  out of
   a string (whose content is not known to the programmer, as
  raw_input).
  Then to assign, I will need the actual list with assigned numbers.

 a = ord('A')
 b = chr(36)

 -- read on the help file: Built-in Functions

  You will be giving me probably the only boost I will need!  I will be
  available later on,
  if I want to take part in the ask/answer system here.



 --

 Message: 3
 Date: Mon, 6 Oct 2008 09:31:04 +0100
 From: Alan Gauld [EMAIL PROTECTED]
 Subject: Re: [Tutor] IF statements
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; format=flowed; charset=iso-8859-1;
reply-type=response

 WM [EMAIL PROTECTED] wrote

  to IF.  The code below was not written by me.  It is a copy/paste
  job from the tutor.  I do not have any idea what is going wrong.

   x = int(raw_input(Please enter an integer: ))
  Please enter an integer: 42
   if x  0:
  ...  x = 0
  ...  print 'Negative changed to zero'
  ... elif x == 0:
  ...  print 'Zero'
  ... elif x == 1:
  ...  print 'Single'
  ... else:
  ...  print 'More'
  ...
  More
  12
  SyntaxError: invalid syntax

 It looks as if you maybe cut more than you intended?
 Also I'm not sure about the ... prompts. I don't have 2.6 yet
 so maybe its an enhancement to IDLE but notmally IDLE
 doesn't print ... prompts. Did you cut n paste them too?
 If so that would confuse IDLE.

 HTH,

 --
 Alan Gauld
 Author of the Learn to Program web site
 http://www.freenetpages.co.uk/hp/alan.gauld




 --

 Message: 4
 Date: Mon, 6 Oct 2008 09:38:14 +0100
 From: Alan Gauld [EMAIL PROTECTED]
 Subject: Re: [Tutor] first call - newcomer
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; format=flowed; charset=iso-8859-1;
reply-type=original


 Anthony Smith [EMAIL PROTECTED] wrote

  This is my first post - I will be brief...

 Hi, welcome to tutor :-)

  1.  A brief (but complete) description regarding the use of script
  editor (I will be using command prompt in Windows), as:

 Brief and Complete don;t normally go together!
 If you are using IDLE then Danny yoo's intro is a good place to start.
 The IDLE section of the Python web site has a more detailed run
 through oof the faciilities.

 a.  details about loading and saving programs (not in that
 order) and little
  specs about pathnames or other requirements (I will
 probably store all
  my little goodies in one folder or space).

 As to paths:

 PATH should be set to the folder where the Python interpreters live
 PYTHONPATH should be set to include the folder where your code lives

  2.  I have been unable to locate the gizmo in the literature to get
  ascii codes
  in python.

 chr(n) is the function you need
 ord(c) is the one in the opposite direction

 but...

 was PRINT VAL(A$) or something.

 Python will generally figure out what you want to print without
 explicit conversions, certainly fewer than you will be used to
 from the early BASIC versions.

  assign a number to a word (using a simple algorhythm that will
  give a
  specific number to every word).  Other stuff is pretty easy to
  find with
  the book and on-line literature.  I will need to get an ascii
  code out of
  a string (whose content is not known to the programmer, as
  raw_input).
  Then to assign, I will need the actual list with assigned
  numbers.

 OK, You lost me here.
 Can you give an example of what the data would look like?

 HTH,

 --
 Alan Gauld
 Author of the Learn to Program web site
 http://www.freenetpages.co.uk/hp/alan.gauld




 --

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

Re: [Tutor] IF statements

2008-10-06 Thread Kent Johnson
On Sun, Oct 5, 2008 at 10:51 PM, WM [EMAIL PROTECTED] wrote:
 I used to do Basic and enjoyed it.  Someone said Python was a vastly better
 language than Visual Basic, which I considered playing with.  So I sought to
 give it a go but struck a sticking point very early.
 I am now going through the Python tutorial.  All went well until I came to
 IF.  The code below was not written by me.  It is a copy/paste job from the
 tutor.  I do not have any idea what is going wrong.

 IDLE 2.6 

 x = int(raw_input(Please enter an integer: ))
 Please enter an integer: 42
 if x  0:
 ...  x = 0
 ...  print 'Negative changed to zero'
 ... elif x == 0:
 ...  print 'Zero'
 ... elif x == 1:
 ...  print 'Single'
 ... else:
 ...  print 'More'
 ...
 More
 12
 SyntaxError: invalid syntax


Did you paste the  and ... or were they printed by the interpreter?
Examples often show the entire contents of a session in the
interpreter, including the  and ... prompts. When you enter the
example yourself you should not include them. Also Please enter an
integer:  is output from the program and 42 is user input to the
program, you should not paste either of them.

Where did the 12 come from? It looks like interpreter output but there
is nothing preceding it that would print a 12.

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


Re: [Tutor] IF Statements

2008-10-06 Thread Alan Gauld


Rajeev Nair [EMAIL PROTECTED] wrote
also i believe the first line can also be written as 'x = 
input('enter
...') instead of using x=int(raw_input('..') . use raw_input 
for

string and just input for integer.


No, use input() only in very special circumstances or when
experimenting for personal use only.

Use int(raw_input()) for all production code. It is much safer.

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


Re: [Tutor] IF Statements

2008-10-06 Thread Kent Johnson
On Mon, Oct 6, 2008 at 7:24 AM, Rajeev Nair [EMAIL PROTECTED] wrote:
 also i believe the first line can also be written as 'x = input('enter
 ...') instead of using x=int(raw_input('..') . use raw_input for
 string and just input for integer.

Yes, although that is not really recommended, this has been discussed
very recently.

Also, please don't include the entire digest in your reply!

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


[Tutor] IF statements-1

2008-10-06 Thread WM

TO THIS ORIGINAL POST I GOT SIX REPLIES.

WM wrote:
  I used to do Basic and enjoyed it.  Someone said Python was a vastly
  better language than Visual Basic, which I considered playing with.  So
  I sought to give it a go but struck a sticking point very early.
  I am now going through the Python tutorial.  All went well until I came
  to IF.  The code below was not written by me.  It is a copy/paste job
  from the tutor.  I do not have any idea what is going wrong.
 
  IDLE 2.6 
 
  x = int(raw_input(Please enter an integer: ))
  Please enter an integer: 42
  if x  0:
  ...  x = 0
  ...  print 'Negative changed to zero'
  ... elif x == 0:
  ...  print 'Zero'
  ... elif x == 1:
  ...  print 'Single'
  ... else:
  ...  print 'More'
  ...
  More
  12
  SyntaxError: invalid syntax
 

IS THIS THE WAY TO ANSWER?  OR SHOULD I DO INDIVIDUAL REPLIES?

JOHNSON 1
The copy above is exactly from the book, via COPY  PASTE, from IDLE26 
thru More.

I keyed in the 12 to generate the error message.

JOHNSON 2
All that was running was IDLE from the desktop and the tutor from the 
Python.Org site.
I will not do the down-load right now as I want the tutor to work with 
the program with no tinkering.


JOHNSON 3
I did not understand Nair's reply, so I didn't follow thru there.
What is the entire digest and how do I not include it?

LANE
I keyed in 12  Enter.  The rest is copy/paste.
I will key the text into Notepad then run it, as you suggest.

GAULD
Not so.  You can check on the tutor, the code stops at 'More'.
It's funny about the dots, they were in the tutor but not in IDLE, 
although the indents were the same.  Then, in the e-mil, there they 
were.  IDLE hides dots?  What do I know?


NAIR
Your post to me starts, also I believe...  It looks like the front end 
got truncated.

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


Re: [Tutor] IF statements-1

2008-10-06 Thread Kent Johnson
On Mon, Oct 6, 2008 at 9:48 PM, WM [EMAIL PROTECTED] wrote:
 TO THIS ORIGINAL POST I GOT SIX REPLIES.

 IS THIS THE WAY TO ANSWER?  OR SHOULD I DO INDIVIDUAL REPLIES?

Either way is OK. It helps to quote a bit of the post to which you are
replying, for context. And please don't use all caps, it is
interpreted as shouting. And you seem to be a bit off in your
attributions...

 JOHNSON 1
 The copy above is exactly from the book, via COPY  PASTE, from IDLE26 thru
 More.
 I keyed in the 12 to generate the error message.

You can't just paste the examples verbatim from the tutorial. The
examples are showing you both the text that you type and the output of
the interpreter. In particular the  and ... are output and should
not be typed or pasted in.


 JOHNSON 3
 I did not understand Nair's reply, so I didn't follow thru there.
 What is the entire digest and how do I not include it?

That was a note to Rajeev, who included a lot of extraneous text in his reply.

Did you start at the beginning of the tutorial or are you starting
with section 4?

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


[Tutor] IF statements

2008-10-05 Thread WM
I used to do Basic and enjoyed it.  Someone said Python was a vastly 
better language than Visual Basic, which I considered playing with.  So 
I sought to give it a go but struck a sticking point very early.
I am now going through the Python tutorial.  All went well until I came 
to IF.  The code below was not written by me.  It is a copy/paste job 
from the tutor.  I do not have any idea what is going wrong.


IDLE 2.6 



 x = int(raw_input(Please enter an integer: ))
Please enter an integer: 42
 if x  0:
...  x = 0
...  print 'Negative changed to zero'
... elif x == 0:
...  print 'Zero'
... elif x == 1:
...  print 'Single'
... else:
...  print 'More'
...
More
12
SyntaxError: invalid syntax


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


Re: [Tutor] assignment statements in python

2006-06-12 Thread Kent Johnson
Kermit Rose wrote:
   
 Message: 1
 Date: Sun, 11 Jun 2006 06:58:39 -0400
 From: Kent Johnson [EMAIL PROTECTED]
 Subject: Re: [Tutor] buggy bug in my program
 Cc: tutor@python.org
  
 Assignment in Python is not a copy, it is a name binding. Assignment
 creates a name for an object. If you assign the same object to two
 names, they both are bound to the same thing. If the object is mutable,
 like a list, changes to the object will be seen regardless of which name
 you use to refer to it.
  
 **
  
 I feel a little bit better now that I know that there is a reason for what
 my
 program did.
  
 However, I still don't have any idea how to copy values from one cell in 
 an array to the adjacent cell in the same array.

You need to copy the value stored in the list, which is itself a list.

 It must be possible, for otherwise, you could not sort an array.

Actually sorting doesn't require copying the values in the list, it just 
requires moving values to different locations of the list.

A list element is somewhat like a name - it is a reference to a value, 
not a container for a value.

If you say
a=[1,2,3]
b=a

then a and b refer to the same list. Likewise, if you say
x=[ [1,2,3], [4,5,6] ]
x[1] = x[0]

then x[1] and x[0] refer to the same list. If you want x[1] (or b) to 
refer to a new list, you have to copy the old list:
x[1] = x[0][:]

list[:] is the slice of the list that goes from the beginning to the end 
- a copy.

Kent

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


Re: [Tutor] assignment statements in python

2006-06-12 Thread Michael Sullivan
On Sun, 2006-06-11 at 22:14 -0400, Kermit Rose wrote:
   Message: 1
 Date: Sun, 11 Jun 2006 06:58:39 -0400
 From: Kent Johnson [EMAIL PROTECTED]
 Subject: Re: [Tutor] buggy bug in my program
 Cc: tutor@python.org
  
 Assignment in Python is not a copy, it is a name binding. Assignment
 creates a name for an object. If you assign the same object to two
 names, they both are bound to the same thing. If the object is mutable,
 like a list, changes to the object will be seen regardless of which name
 you use to refer to it.
  
 **

In that case, is it possible to copy a variable by value, instead of by
reference, in Python?

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


Re: [Tutor] assignment statements in python

2006-06-12 Thread Python
On Mon, 2006-06-12 at 09:37 -0400, Kermit Rose wrote:
   
 From: Python 
 Date: 06/11/06 22:59:38 
 To: Kermit Rose 
 Cc: Tutor Python 
 Subject: Re: [Tutor] assignment statements in python 
  
 
 The basic python objects: numbers, strings, and tuples are immutable and 
 can not be changed (mutated). 
 a = B = 3 
 B = 4 # binds B to a different object with value 4 
 # the object with value 3 is unchanged 
 print a 
 3 
  
 **
 If I write 
  
 a = 3
 a = 4
 a = 5
  
 Are the objects containing 3 and 4 erased when they no longer have a name?

Yes

  
 **
  
 
  
 Container objects such as lists and dictionaries can be changed in 
 place. 
  a = B = [1,2,3] 
  B.append(4) # changes (mutates) B 
  print a 
 [1, 2, 3, 4] 
  
 **
  
 Good.  Now I know a more efficient way to extend  an array.  
  
 I had been creating an entire new array, and equivalencing the old array to
 it.
  
 **
 
  
  B[2] = B[3] # positions 2 and 3 reference the same object 
  print B 
 [1, 2, 4, 4] 
  print a 
 [1, 2, 4, 4] 
  
 **
  
 I still don't know how to make it so that
  
 If  B = [ 1,2,4,5]
  
 B.append(value of B[4])

There is no B[4]

B[0] is 1
B[1] is 2
B[2] is 4
B[3] is 5

Perhaps you mean to search B looking for the value 4 and then append
that value?

index_of_4 = B.index(4) # returns index to location of first 4
B.append( B[index_of_4])# appends the 4 to the end of B

 copy the value of B[2] into B[3]
 import copy
 B = [ 1,2,4,5]
 B[3] = copy.copy(B[2])
 B
[1, 2, 4, 4]

Since 4 is immutable, there is no need to use the copy module, but it is
there for when you need to make copies of an object.

 copy  the value 3 into B[2].

B[2] = 3# no need for a copy since 3 is immutable

  
 
 Or,  equivalently,
  
 If B = [1,2,4,5]
  
 Insert the value 3 between
 B[1] and b[2],
  
 B = [ 1,2,4,5]
 B.insert(2,3)   # inserts 3 before B[2]
 B
[1, 2, 3, 4, 5]

 help(B.insert)

insert(...)
L.insert(index, object) -- insert object before index

(Use q to leave the help screen)
 so that B 
 becomes
 [1,2,3,4,5].
  Kermit  [EMAIL PROTECTED]  
  
  
  
  ___ 
  Tutor maillist - Tutor@python.org 
  http://mail.python.org/mailman/listinfo/tutor 
-- 
Lloyd Kvam
Venix Corp

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


[Tutor] assignment statements in python

2006-06-11 Thread Kermit Rose
  
Message: 1
Date: Sun, 11 Jun 2006 06:58:39 -0400
From: Kent Johnson [EMAIL PROTECTED]
Subject: Re: [Tutor] buggy bug in my program
Cc: tutor@python.org
 
Assignment in Python is not a copy, it is a name binding. Assignment
creates a name for an object. If you assign the same object to two
names, they both are bound to the same thing. If the object is mutable,
like a list, changes to the object will be seen regardless of which name
you use to refer to it.
 
**
 
I feel a little bit better now that I know that there is a reason for what
my
program did.
 
However, I still don't have any idea how to copy values from one cell in 
an array to the adjacent cell in the same array.
 
I looked  at the reference ,
 
http://www.effbot.org/zone/python-objects.htm
 
that you gave,
 
but did not gleam any hint from it how to copy values from one place in an
array to another place within the same array.
 
It must be possible, for otherwise, you could not sort an array.
 
 
It is quite remarkable that my not knowing that 
 
assignment is not a copy 
 
gave me no difficulties before now.
 
 
 
Kermit[EMAIL PROTECTED]  
 
 

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


Re: [Tutor] assignment statements in python

2006-06-11 Thread Python
On Sun, 2006-06-11 at 22:14 -0400, Kermit Rose wrote:
   Message: 1
 Date: Sun, 11 Jun 2006 06:58:39 -0400
 From: Kent Johnson [EMAIL PROTECTED]
 Subject: Re: [Tutor] buggy bug in my program
 Cc: tutor@python.org
  
 Assignment in Python is not a copy, it is a name binding. Assignment
 creates a name for an object. If you assign the same object to two
 names, they both are bound to the same thing. If the object is mutable,
 like a list, changes to the object will be seen regardless of which name
 you use to refer to it.
  
 **
  
 I feel a little bit better now that I know that there is a reason for what
 my
 program did.
  
 However, I still don't have any idea how to copy values from one cell in 
 an array to the adjacent cell in the same array.
  
 I looked  at the reference ,
  
 http://www.effbot.org/zone/python-objects.htm
  
 that you gave,
  
 but did not gleam any hint from it how to copy values from one place in an
 array to another place within the same array.
  
 It must be possible, for otherwise, you could not sort an array.
  
 
 It is quite remarkable that my not knowing that 
  
 assignment is not a copy 
  
 gave me no difficulties before now.
The basic python objects: numbers, strings, and tuples are immutable and
can not be changed (mutated).
a = b = 3
b = 4   # binds b to a different object with value 4
# the object with value 3 is unchanged
print a
3

Container objects such as lists and dictionaries can be changed in
place.
 a = b = [1,2,3]
 b.append(4) # changes (mutates) b
 print a
[1, 2, 3, 4]
 b[2] = b[3] # positions 2 and 3 reference the same object
 print b
[1, 2, 4, 4]
 print a
[1, 2, 4, 4]


  
 
 
 Kermit[EMAIL PROTECTED]  
  
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

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


[Tutor] import statements in functions?

2005-03-21 Thread Marcus Goldfish
Is there a convention to be considered for deciding if import
statements should be included in a function body?  For example, which
of these two module layouts would be preferable:

 # --- MyModule1.py -
 import foo1, foo2, foo3
 import foo_special

 # several coherent functions here

 def specialFunction():
  doSomethingSpecial()

or the embedded import version:

 # --- MyModule2.py -
 import foo1, foo2, foo3
 import foo_rare

 # several coherent functions here

 def specialFunction():
  import foo_special
  doSomethingSpecial()

Also, does the choice have any impact on performance/space/etc.?  And
will the import function get called each time (and possibly ignored)
in the second version?

The reason I consider the second form is that the module foo_special
is only used by the code in specialFunction(), and detracts (IMHO)
from understanding the rest of the code in the module.

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


Re: [Tutor] import statements in functions?

2005-03-21 Thread Sean Perry
Marcus Goldfish wrote:
Is there a convention to be considered for deciding if import
statements should be included in a function body?  For example, which
of these two module layouts would be preferable:
imports are cached. So once it is imported, it stays imported.

The reason I consider the second form is that the module foo_special
is only used by the code in specialFunction(), and detracts (IMHO)
from understanding the rest of the code in the module.
and this is a good reason why you would perform the import inside the 
function.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor