Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

If I created a tuple with the line tup=() and now it's technically empty. How do I add a value (let's say the value of c) to the tuple ?

OpenStudy (anonymous):

try tup = tup + (c,)

OpenStudy (anonymous):

Thanks !!!! I tried tup.append . what is append ?

OpenStudy (anonymous):

? the readings (data structures) say it means to add to the end of a list. What i told you is about the extent of my tuple knowledge lol

OpenStudy (anonymous):

ahh. anyway it worked, I guess I got confused between a tuple and a list ! :) aaaaaaaanyway thanks a lot

OpenStudy (anonymous):

You cannot append to a tuple. tuples are immutable and cannot be changed. You can however construct a new tuple that contains elements from another tuple. For example t1 = (1,) # Create an empty tuple t2 = (2,) # Create another tuple t3 = t1 + t2 # Create a new tuple that has elements from t1 and t2 t3 = t3 + (3,) # Create a new tuple that has elements from t3 and another (literal) tuple r1 = t3 # Make r1 refer to the same tuple t3 refers to t3 = t3 + (4,) # Create a new tuple that has elements from t3 and another literal tuple print t3 print r1 #r1 and t3 no longer reference the same tuple because we reassigned it. l1 = [1] # Create a new list that has a 1 in it l2 = [2] # Create a new list that has a 2 in it l3 = l1 + l2 # Create a new list that has the elements from l1 and l2 l3.append(3) # Add 3 to the list referenced by l3 r2 = l3 # Make r2 reference the same list l3 does l3.append(4) # Add a 4 to the list referenced by l3 print l3 print r2 #r2 and l3 refer to the same list so they display the same numbers l3 = l3 + [5] # Create a new list containing items from l3 and a 5 print l3 print r2 # r2 and l3 no longer refer to the same lists.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!