[Tutor] The magic parentheses

Shashwat Anand anand.shashwat at gmail.com
Sun Jan 24 09:24:37 CET 2010


@David,

Alan and rest have clarified your doubt I guess. However let me point you
towards some other mistakes.

def area1(radius):
    area1r = 3.14159 * mainvar1**2
    return area1r
    print area1r

def area2(radius):
    area2r = 3.14159 * mainvar2**2
    return area2r
    print area2r

Here you created two functions for the single purpose of calculating area.
Why not just do,
def area(radius):
     return math.pi * radius * radius

Functions are created for doing repetitive tasks IMHO. But you just created
them for the heck of it. Ofcourse there is issue of writing dead-code (i.e.
lines that will never be executed), the print after return as Alan already
pointed out.
Also you created, a function that displays the difference between the two
results.

def return_difference_of12(var1,var2):
    if var1 - var2 > 0:
        y = v1,var1,v3,v2,var2,period
        print y
    elif var2 - var1 > 0:
        z = v2,var2,v3,v2,var1,period
        print z


Why not create something simple like this :

def return_difference(var1, var2):
    if var1 > var2:
        print 'Variable 1', var1, 'is greater than Variable 2', var2, '.'
    elif var2 > var1:
        print 'Variable 2', var2, 'is greater than Variable 1', var1, '.'
    else:
        print 'Variable 1', var1, 'is equal Variable 2', var2, '.'

No need for explicit formation of tuple if all you wish is just a print
statement.

Regards,
~Shashwat


On Sun, Jan 24, 2010 at 1:23 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "David Hutto" <dwightdhutto at yahoo.com> wrote
>
>  This is my first post to the list, so tell me if I'm posting incorrectly.
>>
>
> You are doing fine. Welcome.
>
>  My problem is when the results are printed, I get this:
>>
>> >>>
>> ('Variable 2,', 490.0, 'is greater than', 'Variable 2,', 8.0, '.')
>>
>
>  The parentheses, as well as the apostrophes and commas.
>> I'm sure it's the way I'm having the results printed after it's
>> through, but not sure how to correct it.
>>
>
> You have two problems in your code(at least!)
> ---------------def area2(radius):
>   area2r = 3.14159 * mainvar2**2
>   return area2r
>   print area2r
> --------The print statement will never be called because the return
> statement forces an exit from the function.
> -------------------------
> def return_difference_of12(var1,var2):
>   if var1 - var2 > 0:
>
>       y = v1,var1,v3,v2,var2,period
>       print y
>   elif var2 - var1 > 0:
>       z = v2,var2,v3,v2,var1,period
>       print z
> ----------------------------The assignments to y and z create tuples
> (a,b,c...)So you are asking to print a tuple and Python represents tuples by
> putting parens around the contents.To print them as a string use the join()
> method of a string using an empty string:print ''.join([str(s) for s in
> y])However this is an unusual way to print this type of output,It would be
> more normal to use a format string:print "Variable2: %f id greater than
> Variable1: %f." % (var1,var2)This reduces the number of variables needed and
> also gives you much more control over layout because the %f markers can be
> augmented with width specifiers, justificationhints etc.You can store the
> entire format string in a variable if you wish - especiaslly if you want to
> use it multiple times - but in your case the strings only appear once so I
> wouldn't bother.HTH,-- Alan GauldAuthor of the Learn to Program web
> sitehttp://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100124/cfa8259d/attachment.htm>


More information about the Tutor mailing list