Difference between list and tuple in python
๐ธList is mutable ( you can modify the original list) and it's values are written in sqare brackets [ ]
๐ธTuple is immutable ( you can't modify it) and it's values are written in parentheses ( ) delimited by comma( , )
๐ธTo convert list to tuple - we use tuple() function
list1 = [1,2,3]
print(tuple(list1)) Output : (1,2,3)
๐ธ For single element list
list1 = [1]
print(tuple(list1)) Output : (1, )
โช๏ธa tuple is a tuple because of comma not because of parentheses
Post #2875
1.46K
- โค 4