1# A tuple is a sequence of immutable Python objects. Tuples are
2# sequences, just like lists. The differences between tuples
3# and lists are, the tuples cannot be changed unlike lists and
4# tuples use parentheses, whereas lists use square brackets.
5tup1 = ('physics', 'chemistry', 1997, 2000);
6tup2 = "a", "b", "c", "d";
7
8# To access values in tuple, use the square brackets for
9# slicing along with the index or indices to obtain value
10# available at that index.
11tup1[0] # Output: 'physics'
1my_tuple = ("hello")
2print(type(my_tuple)) # <class 'str'>
3
4# Creating a tuple having one element
5my_tuple = ("hello",)
6print(type(my_tuple)) # <class 'tuple'>
7
8# Parentheses is optional
9my_tuple = "hello",
10print(type(my_tuple)) # <class 'tuple'>
1t = 12345, 54321, 'hello!'
2print(t[0])
3# output 12345
4print(t)
5# output (12345, 54321, 'hello!')
6# Tuples may be nested:
7u = t, (1, 2, 3, 4, 5)
8print(u)
9# output ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
10# Tuples are immutable:
11# assigning value of 12345 to 88888
12t[0] = 88888
13Traceback (most recent call last):
14 File "<stdin>", line 1, in <module>
15TypeError: 'tuple' object does not support item assignment
16# but they can contain mutable objects:
17v = ([1, 2, 3], [3, 2, 1])
18print(v)
19# output ([1, 2, 3], [3, 2, 1])
1#It's like a list, but unchangeable
2tup = ("var1","var2","var3")
3tup = (1,2,3)
4#Error
1# Different types of tuples
2
3# Empty tuple
4my_tuple = ()
5print(my_tuple) # ()
6
7# Tuple having integers
8my_tuple = (1, 2, 3)
9print(my_tuple) # (1, 2, 3)
10
11# tuple with mixed datatypes
12my_tuple = (1, "Hello", 3.4)
13print(my_tuple) # (1, 'Hello', 3.4)
14
15# nested tuple
16my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
17print(my_tuple) # ('mouse', [8, 4, 6], (1, 2, 3))