16

I was reading about tuple:

Once created, the values of a tuple cannot change.

If you want to assign multiple variables at once, you can use tuples:

name,age,country,career = ('Diana',32,'Canada','CompSci')
print(country)

I did this..

country = 'India'
print(country)

and it's modified. How come?

3
  • 13
    You are not changing the values of a tuple but only reassigning the value of a variable. The tuple ('Diana',32,'Canada','CompSci') is unpacked to 4 different str variables. Hence you can reassign the variable country. However if you want to do something like ('Diana',32,'Canada','CompSci')[2] = "India", you cannot as you are trying to change the value of the immutable tuple. Commented May 13, 2016 at 13:26
  • 2
    ^ should probably post that as an answer Commented May 13, 2016 at 13:28
  • Ok. It will consider tuple only if i assign it to single variable? Commented May 13, 2016 at 13:30

5 Answers 5

19

The way you used the tuple was only to assign the single values to single variables in one line. This doesn't store the tuple anywhere, so you'll be left with 4 variables with 4 different values. When you change the value of country, you change the value of this single variable, not of the tuple, as string variables are "call by value" in python.

If you want to store a tuple you'd do it this way:

tup = ('Diana',32,'Canada','CompSci')

Then you can access the values via the index:

print tup[1] #32

Edit: What I forgot to mention was that tuples are not mutable, so you can access the values, but you can't set them like you could with arrays. You can still do :

name, age, country, job = tup

But the values will be copies of the tuple - so changing these wont change the tuple.

Sign up to request clarification or add additional context in comments.

Comments

8

The following snippet code might be helpful to understand the reason. Here, name, age, country and career are single variables and therefore can be modified.

t = (name, age, country, career) = ('Diana',32,'Canada','CompSci')

print(t)            # ('Diana', 32, 'Canada', 'CompSci')
print(country)      # Canada

country = 'India'

print(t)            # ('Diana', 32, 'Canada', 'CompSci')
print(country)      # India

t[2] = 'India'
# The error occurs as expected
TypeError: 'tuple' object does not support item assignment

Comments

1

You are just changing the value of the variable Country not the tuple. you can test it this way:

 tup=('Diana',32,'Canada','CompSci')
 name,age,country,career = tup
 print(country)
 country = 'India'
 print(tup)

The output should be as follows:

 Canada
 ('Diana', 32, 'Canada', 'CompSci')

you can't change the value of an element inside a tuple they are immutable. its one the key features of tuples. Lists on the other hand are mutable.

Comments

1

This code:

name, age, country, career = ('Diana',32,'Canada','CompSci')
print(country)

does this:

name = 'Diana'
age = 32
country = 'Canada'
career = 'CompSci'

Tuples are not mutable, but you haven't made a tuple. To make a tuple, try this:

nameAge = ('Diana', 32)

now you change it:

>>> nameAge[1] = 33
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    nameAge[1] = 'a'
TypeError: 'tuple' object does not support item assignment

As you can see, this tuple doesn't change.

Comments

0

Here your requirement is to add the tuple tup values into given variables like name,age,country,career using a single line code isn't?

Question:

  tup = ('Diana',32,'Canada','CompSci')

the required answer is:

  name,age,country,career = tup

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.