Shri
Shri

Reputation: 731

python: multiple variables using tuple

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?

Upvotes: 14

Views: 41046

Answers (5)

Unnikrishnan
Unnikrishnan

Reputation: 148

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

Upvotes: 0

SparkAndShine
SparkAndShine

Reputation: 18007

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

Upvotes: 8

Tim
Tim

Reputation: 2592

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.

Upvotes: 1

Philip Feldmann
Philip Feldmann

Reputation: 8375

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.

Upvotes: 18

Mosfetdaha
Mosfetdaha

Reputation: 11

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.

Upvotes: 1

Related Questions