creating a tuple in python code example
Example 1: what is a tuple in python
# A tuple is a sequence of immutable Python objects. Tuples are
# sequences, just like lists. The differences between tuples
# and lists are, the tuples cannot be changed unlike lists and
# tuples use parentheses, whereas lists use square brackets.
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = "a", "b", "c", "d";
# To access values in tuple, use the square brackets for
# slicing along with the index or indices to obtain value
# available at that index.
tup1[0] # Output: 'physics'
Example 2: changing tuple values
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Example 3: create tuples python
values = [a, b, c, 1, 2, 3]
values = tuple(values)
print(values)
Example 4: tuples
#!/usr/bin/python
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');
# Following action is not valid for tuples
# tup1[0] = 100;
# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;
Example 5: tuple in python
#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')
Example 6: pyhton tuple
#It's like a list, but unchangeable
tup = ("var1","var2","var3")
tup = (1,2,3)
#Error