python: multiple variables using tuple
Asked Answered
K

5

14

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?

Keelboat answered 13/5, 2016 at 13:24 Comment(3)
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.Sherilyn
^ should probably post that as an answerDownhaul
Ok. It will consider tuple only if i assign it to single variable?Keelboat
E
18

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.

Enunciate answered 13/5, 2016 at 13:34 Comment(0)
A
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
Auroora answered 13/5, 2016 at 13:35 Comment(0)
K
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.

Kaine answered 13/5, 2016 at 13:37 Comment(0)
C
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.

Cumbersome answered 13/5, 2016 at 13:39 Comment(0)
B
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
Braunschweig answered 29/9, 2020 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.