Tuples in python

Lets have a look what are tuples in python.

Tuples are just like arrays, however the information stored in tuple can’t be modified.

Lets have a look at below python program.

Program

### Tuples can't be changed.
tuple1=(1,2,3)
print(tuple1[1])
tuple1[1]="10"   ## generates an error as it can't be modified.

 

Output
We can see below that we encountered an error because we have tried to change the value of a tuple cell.
The print command ran fine.

/Users/saket/PycharmProjects/1helloworld/venv/bin/python /Users/saket/PycharmProjects/1helloworld/tuples.py
2
Traceback (most recent call last):
  File "/Users/saket1447583/PycharmProjects/1helloworld/tuples.py", line 8, in 
    tuple1[1]="10"   ## generates an error as it can't be modified.
TypeError: 'tuple' object does not support item assignment

Process finished with exit code 1

 

Leave a Reply

Your email address will not be published.