Tuple concatenation help ?

This is the place for queries that don't fit in any of the other categories.

Tuple concatenation help ?

Postby Freaky101 » Sun Apr 07, 2013 4:06 pm

Hello,
I'm a new programmer to python and i'm writing simple programs. when i tried to concatenate a touple from a book that i am reading, it says im not allowed to do that. i'm not sure why but could someone please help? :x :x :oops:
Last edited by Yoriz on Sun Apr 07, 2013 4:43 pm, edited 1 time in total.
Reason: Edited title
Freaky101
 
Posts: 1
Joined: Sun Apr 07, 2013 3:58 pm

Re: Touple Help

Postby Yoriz » Sun Apr 07, 2013 4:30 pm

Hi,

Please read the following link
http://python-forum.org/viewtopic.php?f=6&t=145

Then show a small sample of the code you are trying and the error it gives.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1019
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Touple Help

Postby setrofim » Sun Apr 07, 2013 4:31 pm

Tuples are immutable and cannot be modified. Use a list instead.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Tuple concatenation help ?

Postby Yoriz » Sun Apr 07, 2013 4:54 pm

You can concatenate them but you cant alter them in place
Code: Select all
data1 = (1, 2, 3)
data2 = (3, 4, 5)
data3 = data1 + data2
print data3
(1, 2, 3, 3, 4, 5)
data1 += data2
print data1
(1, 2, 3, 3, 4, 5)
data1[0] = 10
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1019
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 6 guests