Re: [Tutor] Return T/F vs print T/F

2012-02-04 Thread bob gailer

On 2/4/2012 11:53 AM, Steven D'Aprano wrote:
[snip]


In the interactive interpreter, as a convenience, the result of each 
line is automatically printed for you:


Actually the interpreter prints the result of each /statement /that IS 
an /expression/.

>>> 2
2
>>> a = 3
>>> a
3
IOW if the line is blank, start with # or is a statement the interpreter 
does not print anything (unless it is a print statement). For a messy 
example:


>>> if 1:3;a=4;a
...
3
4

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Return T/F vs print T/F

2012-02-04 Thread Brett Ritter
On Sat, Feb 4, 2012 at 11:42 AM, Sivaram Neelakantan
 wrote:
> On Sat, Feb 04 2012,bob gailer wrote:
>
>> On 2/4/2012 9:38 AM, Sivaram Neelakantan wrote:
>>> While trying out code, I have trouble following the difference between
>>>
>>> return True vs print True and the same with False.  If I use return
>>> for the True/False statements, nothing gets printed.  Why?
>>
>> Why did you expect something to be printed?
>
> err...return sends something back as in True or False?

Yes, it returns it to the caller.  It doesn't print it.

When using the interactive mode, the program that creates that mode
prints the return.  When you are using it your program, the caller
decides what to do.

The flip side is that printing a value does NOT return it.

-- 
Brett Ritter / SwiftOne
swift...@swiftone.org
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Return T/F vs print T/F

2012-02-04 Thread Steven D'Aprano

Sivaram Neelakantan wrote:


def palin(text):
if first(text) == last(text):
# print first(text), last(text), middle(text), len(middle(text))
if len(middle(text)) == 0:
print True
else:
palin(middle(text))
else:
print False



Every branch of the function must include a return, or Python will just return 
None by default. You have three branches. Two of them print a flag. It is easy 
enough to fix them by changing print to return. The third branch has no 
return. It needs one.




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Return T/F vs print T/F

2012-02-04 Thread Steven D'Aprano

Sivaram Neelakantan wrote:
While trying out code, I have trouble following the difference between 
return True vs print True and the same with False.  If I use return
for the True/False statements, nothing gets printed.  Why? 


Probably because you aren't printing anything. Python can't read your mind and 
know what you want printed and what you don't want printed, you have to tell 
it what to print.


It is not clear how you are "trying out code". Are you running code as a 
script? Using IDLE or iPython? Using the standard interactive interpreter? The 
environment will make a difference in the behaviour.


In the interactive interpreter, as a convenience, the result of each line is 
automatically printed for you:



>>> 1+3
4
>>> len("hello world")
11


but this is only in the interactive environment. In a script, nothing is 
printed unless you call print.


Functions should have a return result -- the value they calculate. You can 
then store the value in a variable for later use:


>>> length = len("hello world")  # store the value
>>> print "The length is", length  # and use it later
The length is 11


The print command does not return a value, it just prints the argument. If you 
use print in the function, you cannot capture the result and use it in 
additional calculations.


When writing your own functions, you should always aim for this same 
behaviour: use return inside the function, print outside.




And if I add a print before the function call I get an output like 


True

None


I can't see any possible way your code will give output including ">>>". Would 
you care to explain more carefully what you mean?





--
Steven


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Return T/F vs print T/F

2012-02-04 Thread Sivaram Neelakantan
On Sat, Feb 04 2012,bob gailer wrote:

> On 2/4/2012 9:38 AM, Sivaram Neelakantan wrote:
>> While trying out code, I have trouble following the difference between
>>
>> return True vs print True and the same with False.  If I use return
>> for the True/False statements, nothing gets printed.  Why?
>
> Why did you expect something to be printed?

err...return sends something back as in True or False?


>
> Perhaps you are confused between what happens in the interactive
> window vs running a program?

Possibly.  But I was coding in an Emacs buffer and sending it to the
python subprocess from within Emacs.  Does that have anything to do
with it?

>
> Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
 def f():
> ...   return True
> ...
 f()
> True
 def g():
> ... print True
> ...
 g()
> True
 print f()
> True
 print g()
> True
> None
>
> In the interactive window, when you call a function, Python displays
> the returned value.  When you run a program with a call to a
> function, Python does NOT display the returned value.
>
> A function that does not execute a return will return None; in the
> interactive window nothing is displayed.
>
> Is that sufficient?

Right, thanks for this, will check this thing out again.

 sivaram
 -- 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Return T/F vs print T/F

2012-02-04 Thread bob gailer

On 2/4/2012 9:38 AM, Sivaram Neelakantan wrote:

While trying out code, I have trouble following the difference between

return True vs print True and the same with False.  If I use return
for the True/False statements, nothing gets printed.  Why?


Why did you expect something to be printed?

Perhaps you are confused between what happens in the interactive window 
vs running a program?


Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit 
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...   return True
...
>>> f()
True
>>> def g():
... print True
...
>>> g()
True
>>> print f()
True
>>> print g()
True
None

In the interactive window, when you call a function,  Python displays 
the returned value.
When you run a program with a call to a function, Python does NOT 
display the returned value.


A function that does not execute a return will return None; in the 
interactive window nothing is displayed.


Is that sufficient?

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Return T/F vs print T/F

2012-02-04 Thread Sivaram Neelakantan

While trying out code, I have trouble following the difference between 

return True vs print True and the same with False.  If I use return
for the True/False statements, nothing gets printed.  Why? And if I
add a print before the function call I get an output like 

>>>True
None

--8<---cut here---start->8---
def first(word):
return word[0]
def last(word):
return word[-1]
def middle(word):
return word[1:-1]

def palin(text):
if first(text) == last(text):
# print first(text), last(text), middle(text), len(middle(text))
if len(middle(text)) == 0:
print True
else:
palin(middle(text))
else:
print False


palin("o") 
palin("nxon")
palin("nooxooxoon")
print palin("nabban") 
--8<---cut here---end--->8---



 sivaram
 -- 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor