Python Tuple

Asked 2 years ago, Updated 2 years ago, 72 views

I heard that you can't change the tuple

Why does this code work? Didn't you change the tuple because the variable name is t?

tuple python

2022-09-22 20:32

1 Answers

You cannot change a tuple that has been initialized once, but you can assign a new value to the variable that contains the tuple. What this means is

t = ('A', 'B', 'C')

('A', 'B', 'C') cannot be changed in . However, you can assign a new value to t. For example,

t = ('A', 'B', 'C')
t += ('D')

('A', 'B', 'C') cannot be changed like this, but

t = ('A', 'B', 'C')
t = t + ('D',)

In this way, t can be assigned a new tuple called ('A', 'B', 'C', 'D').


2022-09-22 20:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.