[Tutor] Python Variable Addition

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Tutor] Python Variable Addition

viral shah
Hi

I'm new in the learning of python

I want to make simple code for two variable addition

Please help me in the following code

x = 12
print x
y =20
print y
z = x+y
print 'Addition of above two numbers are : ' + int.z

when I run this same I got an error message :

AttributeError: type object 'int' has no attribute 'z'

Please help me to solve the same error

--
Viral Shah
IT Department,
E-mail : [hidden email]
Mobile : (+91) 9722312220




_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] Python Variable Addition

Kapil Shukla
Viral
You should be doing this

"Addition of two numbers is" + str(x+y)

+ operator works on same datatypes and int being one of the built in objects in python does not have a method z

U shld use str() or int() to do type conversion

-Kapil
कपिल शुक्‍ला

-----Original Message-----
From: viral shah <[hidden email]>
Sender: tutor-bounces+shukla.kapil=[hidden email]
Date: Mon, 30 Apr 2012 15:20:21
To: <[hidden email]>
Subject: [Tutor] Python Variable Addition

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] Python Variable Addition

Dave Angel-3
In reply to this post by viral shah
On 04/30/2012 05:50 AM, viral shah wrote:

> Hi
>
> I'm new in the learning of python
>
> I want to make simple code for two variable addition
>
> Please help me in the following code
>
> *x = 12
> print x
> y =20
> print y
> z = x+y
> print 'Addition of above two numbers are : ' + int.z
> *
> when I run this same I got an error message :
>
> *AttributeError: type object 'int' has no attribute 'z'
>
> Please help me to solve the same error
>
>

Welcome to Python tutor.

Normally, the print statement accepts a list of items, separated by
commas.  Each item is implicitly converted to a string, and the results
are sent to standard out, with a space between each item.

So the most straightforward way to print z would be something like:

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

Now, sometimes you want to combine two strings yourself, so you might use:

    print 'Addition of above two numbers are:' + str(z)

Here we are using the str type as a conversion function.  Note that we
used parentheses, not the dot operator.  And notice that since print
only gets one item, it does not add a space.





--

DaveA

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] Python Variable Addition

Alan Gauld
In reply to this post by Kapil Shukla
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/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] Python Variable Addition

Mark Lawrence
On 30/04/2012 18:36, Alan Gauld wrote:

> 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

Except that you'll get two spaces after the colon :)

>
>
>> + 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.
>


--
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] Python Variable Addition

Alan Gauld
On 30/04/12 19:27, Mark Lawrence wrote:

>> print 'Addition of above two numbers are : ', z
>
> Except that you'll get two spaces after the colon :)

OK thats true,
Try this:

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

for one. :-)

But if the number of spaces is critical string formatting is better
still. And better than string addition.

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

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] Python Variable Addition

Mark Lawrence
On 30/04/2012 19:40, Alan Gauld wrote:

> On 30/04/12 19:27, Mark Lawrence wrote:
>
>>> print 'Addition of above two numbers are : ', z
>>
>> Except that you'll get two spaces after the colon :)
>
> OK thats true,
> Try this:
>
> print 'Addition of above two numbers are :', z
>
> for one. :-)
>
> But if the number of spaces is critical string formatting is better
> still. And better than string addition.
>

True indeed, but which of the three versions of string formatting that
I'm aware of?

--
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] Python Variable Addition

Steven D'Aprano-8
Mark Lawrence wrote:
> On 30/04/2012 19:40, Alan Gauld wrote:

>> But if the number of spaces is critical string formatting is better
>> still. And better than string addition.
>>
>
> True indeed, but which of the three versions of string formatting that
> I'm aware of?

Any of them.


--
Steven
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] Python Variable Addition

Mark Lawrence
On 01/05/2012 00:35, Steven D'Aprano wrote:

> Mark Lawrence wrote:
>> On 30/04/2012 19:40, Alan Gauld wrote:
>
>>> But if the number of spaces is critical string formatting is better
>>> still. And better than string addition.
>>>
>>
>> True indeed, but which of the three versions of string formatting that
>> I'm aware of?
>
> Any of them.
>
>

Alright you **** antipodean :)

--
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Loading...