Re: [Tutor] functions in Python

2006-04-17 Thread Paul D. Eden
This only happens because the python interactive command-line (what you 
get when you just type 'python' in a terminal window) prints return 
values automatically for you.

If you were executing

f(4)

in a program/script, the return value would be lost.

Paul

Payal Rathod wrote:
> On Mon, Apr 17, 2006 at 05:42:05PM +0100, Steve Nelson wrote:
> 
>>When you define a function, you are writing a block of code which you
>>can ask to perform a task.  The task may be simple, and not require
>>any additional information, or it may be more complex and need
>>information.
> 
> 
> What is the difference between,
> 
> 
def f(x):
> 
> ... return x
> ...
> 
f(4)
> 
> 4
> 
> 
def f(x):
> 
> ... print x
> ...
> 
f(4)
> 
> 4
> 
> Both give same results. So, why return statement is needed?
> With warm regards,
> -Payal
> ___
> 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] Changing lists in place

2006-04-17 Thread Paul D. Eden
Lists are mutable, you are right.

But the code you gave does not change the list.  It changes the variable 
element which is separate from the list myList.

If you want to change the list try something like this:

mylist = [ 'One', '   two', '   three   ' ]
print mylist
newlist = []
for element in mylist:
 element = element.strip()
 newlist.append(element)
 print "<>" + element + "<>"
print newlist

OR

mylist = [ 'One', '   two', '   three   ' ]
print mylist
mylist = [element.strip() for element in mylist]
for element in mylist:
 print "<>" + element + "<>"
print mylist

Paul

Paul D. Kraus wrote:
> I have a list that I want to change in a for loop. It changes in the 
> loop but the changes are not persistant outside of the loop.
> I thought lists were mutuable objects so I am a bit confused.
> 
> Sample Code
> #!/usr/bin/env python
> """ Testing lists """
> 
> mylist = [ 'One', '   two', '   three   ' ]
> print mylist
> for element in mylist:
> element = element.strip()
> print "<>" + element + "<>"
> print mylist
> 
> 
> Sample Output
> ['One', '   two', '   three   ']
> <>One<>
> <>two<>
> <>three<>
> ['One', '   two', '   three   ']
> 
> 
> 
> 
> ___
> 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] Changing lists in place

2006-04-17 Thread Paul D. Eden
Very true.

Paul

Kent Johnson wrote:
> Paul D. Eden wrote:
> 
>>Lists are mutable, you are right.
>>
>>But the code you gave does not change the list.  It changes the variable 
>>element which is separate from the list myList.
>>
>>If you want to change the list try something like this:
>>
>>mylist = [ 'One', '   two', '   three   ' ]
>>print mylist
>>newlist = []
>>for element in mylist:
>> element = element.strip()
>> newlist.append(element)
>> print "<>" + element + "<>"
>>print newlist
>>
>>OR
>>
>>mylist = [ 'One', '   two', '   three   ' ]
>>print mylist
>>mylist = [element.strip() for element in mylist]
>>for element in mylist:
>> print "<>" + element + "<>"
>>print mylist
> 
> 
> Neither of these changes the original list either. They both create new 
> lists with the desired contents. The second example binds the new list 
> to the old name, but it is still a new list. In many cases this is fine, 
> but the distinction is important. For example if you are writing a 
> function that modifies a list passed to it, these solutions won't work. 
> Bob's solution using enumerate() is the simplest way to modify a list in 
> place.
> 
> 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] wanted exercises in python

2006-04-18 Thread Paul D. Eden
If you have some programming experience already in another language you 
may want to try the examples in http://www.diveintopython.org/toc/index.html

Paul

Payal Rathod wrote:
> Hi,
> As  I mentioned I have been reading Python a lot in last 2 months but 
> lack of examples and coding is not getting me anywhere. Can someone give 
> me some exercises or I can try them myself (pythonchallenge.com please 
> excuse). I am reading Alan's tut now and covered Basis set and regex 
> from advanced set, so please give me exercises based on those topics 
> i.e. don't tell me to code a http client or a GUI based program in 
> Python :)
> 
> With warm regards,
> -Payal
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor