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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With