[Tutor] Python Variable Addition

Alan Gauld alan.gauld at btinternet.com
Mon Apr 30 19:36:10 CEST 2012


On 30/04/12 11:00, Kapil Shukla कपिल शुक्‍ला wrote:
> Viral
> You should be doing this
>
> "Addition of two numbers is" + str(x+y)

There is no need for str() since print implicitly calls string to 
convert objects to string format. Also the addition is not needed since 
print takes a comma separated list of arguments. So it would normally be:

print 'Addition of above two numbers are : ', z


> + operator works on same datatypes and

Using addition on strings is quite expensive and there are usually 
better ways to do the same job.


> int being one of the built in objects in python does not have a method z

This however is true(ish - int is a type not strictly an object, except 
that everything in Python is an object, including types! :-) and it is 
the source of the original error message.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list